]> git.argeo.org Git - lgpl/argeo-commons.git/blob - SimpleApp.java
be764e975b10ca8bb9f978ae6b2e715e15ba9079
[lgpl/argeo-commons.git] / SimpleApp.java
1 package org.argeo.cms.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.LinkedHashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.jcr.Repository;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.Session;
15 import javax.jcr.security.Privilege;
16 import javax.jcr.version.VersionManager;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.cms.CmsConstants;
21 import org.argeo.cms.CmsException;
22 import org.argeo.cms.CmsSession;
23 import org.argeo.cms.CmsUiProvider;
24 import org.argeo.cms.LifeCycleUiProvider;
25 import org.argeo.jcr.JcrUtils;
26 import org.eclipse.gemini.blueprint.context.BundleContextAware;
27 import org.eclipse.rap.rwt.application.Application;
28 import org.eclipse.rap.rwt.application.Application.OperationMode;
29 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
30 import org.eclipse.rap.rwt.application.EntryPoint;
31 import org.eclipse.rap.rwt.application.EntryPointFactory;
32 import org.eclipse.rap.rwt.application.ExceptionHandler;
33 import org.eclipse.rap.rwt.client.WebClient;
34 import org.eclipse.rap.rwt.service.ResourceLoader;
35 import org.osgi.framework.BundleContext;
36
37 /** A basic generic app based on {@link SimpleErgonomics}. */
38 public class SimpleApp implements CmsConstants, ApplicationConfiguration,
39 BundleContextAware {
40 private final static Log log = LogFactory.getLog(SimpleApp.class);
41
42 private Map<String, Map<String, String>> branding = new HashMap<String, Map<String, String>>();
43 private Map<String, List<String>> styleSheets = new HashMap<String, List<String>>();
44
45 private List<String> resources = new ArrayList<String>();
46
47 private BundleContext bundleContext;
48
49 private Repository repository;
50 private String workspace = null;
51 private String basePath = "/";
52 private List<String> roPrincipals = Arrays.asList("anonymous", "everyone");
53 private List<String> rwPrincipals = Arrays.asList("everyone");
54
55 private CmsUiProvider header;
56 private Map<String, CmsUiProvider> pages = new LinkedHashMap<String, CmsUiProvider>();
57
58 private Integer headerHeight = 40;
59
60 public void configure(Application application) {
61 try {
62 application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
63 application.setExceptionHandler(new CmsExceptionHandler());
64
65 // loading animated gif
66 application.addResource(LOADING_IMAGE,
67 createResourceLoader(LOADING_IMAGE));
68 // empty image
69 application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE));
70
71 for (String resource : resources) {
72 application.addResource(resource, new BundleResourceLoader(
73 bundleContext));
74 if (log.isDebugEnabled())
75 log.debug("Registered resource " + resource);
76 }
77
78 Map<String, String> defaultBranding = null;
79 if (branding.containsKey("*"))
80 defaultBranding = branding.get("*");
81
82 // entry points
83 for (String page : pages.keySet()) {
84 Map<String, String> properties = defaultBranding != null ? new HashMap<String, String>(
85 defaultBranding) : new HashMap<String, String>();
86 if (branding.containsKey(page)) {
87 properties.putAll(branding.get(page));
88 }
89 // favicon
90 if (properties.containsKey(WebClient.FAVICON)) {
91 String faviconRelPath = properties.get(WebClient.FAVICON);
92 application.addResource(faviconRelPath,
93 new BundleResourceLoader(bundleContext));
94 if (log.isTraceEnabled())
95 log.trace("Registered favicon " + faviconRelPath);
96
97 }
98
99 // page title
100 if (!properties.containsKey(WebClient.PAGE_TITLE))
101 properties.put(
102 WebClient.PAGE_TITLE,
103 Character.toUpperCase(page.charAt(0))
104 + page.substring(1));
105
106 // default body HTML
107 if (!properties.containsKey(WebClient.BODY_HTML))
108 properties.put(WebClient.BODY_HTML, DEFAULT_LOADING_BODY);
109
110 //
111 // ADD ENTRY POINT
112 //
113 application.addEntryPoint("/" + page, new CmsEntryPointFactory(
114 pages.get(page), repository, workspace, properties),
115 properties);
116 log.info("Registered entry point /" + page);
117 }
118
119 // stylesheets
120 for (String themeId : styleSheets.keySet()) {
121 List<String> cssLst = styleSheets.get(themeId);
122 for (String css : cssLst) {
123 application.addStyleSheet(themeId, css,
124 new BundleResourceLoader(bundleContext));
125 }
126
127 }
128 } catch (RuntimeException e) {
129 // Easier access to initialisation errors
130 log.error("Unexpected exception when configuring RWT application.",
131 e);
132 throw e;
133 }
134 }
135
136 public void init() throws RepositoryException {
137 Session session = null;
138 try {
139 session = JcrUtils.loginOrCreateWorkspace(repository, workspace);
140 VersionManager vm = session.getWorkspace().getVersionManager();
141 if (!vm.isCheckedOut("/"))
142 vm.checkout("/");
143 JcrUtils.mkdirs(session, basePath);
144 for (String principal : rwPrincipals)
145 JcrUtils.addPrivilege(session, basePath, principal,
146 Privilege.JCR_WRITE);
147 for (String principal : roPrincipals)
148 JcrUtils.addPrivilege(session, basePath, principal,
149 Privilege.JCR_READ);
150
151 for (String pageName : pages.keySet()) {
152 try {
153 initPage(session, pages.get(pageName));
154 session.save();
155 } catch (Exception e) {
156 throw new CmsException(
157 "Cannot initialize page " + pageName, e);
158 }
159 }
160
161 } finally {
162 JcrUtils.logoutQuietly(session);
163 }
164 }
165
166 protected void initPage(Session adminSession, CmsUiProvider page)
167 throws RepositoryException {
168 if (page instanceof LifeCycleUiProvider)
169 ((LifeCycleUiProvider) page).init(adminSession);
170 }
171
172 public void destroy() {
173 for (String pageName : pages.keySet()) {
174 try {
175 CmsUiProvider page = pages.get(pageName);
176 if (page instanceof LifeCycleUiProvider)
177 ((LifeCycleUiProvider) page).destroy();
178 } catch (Exception e) {
179 log.error("Cannot destroy page " + pageName, e);
180 }
181 }
182 }
183
184 public void setRepository(Repository repository) {
185 this.repository = repository;
186 }
187
188 public void setWorkspace(String workspace) {
189 this.workspace = workspace;
190 }
191
192 public void setHeader(CmsUiProvider header) {
193 this.header = header;
194 }
195
196 public void setPages(Map<String, CmsUiProvider> pages) {
197 this.pages = pages;
198 }
199
200 public void setBasePath(String basePath) {
201 this.basePath = basePath;
202 }
203
204 public void setRoPrincipals(List<String> roPrincipals) {
205 this.roPrincipals = roPrincipals;
206 }
207
208 public void setRwPrincipals(List<String> rwPrincipals) {
209 this.rwPrincipals = rwPrincipals;
210 }
211
212 public void setHeaderHeight(Integer headerHeight) {
213 this.headerHeight = headerHeight;
214 }
215
216 public void setBranding(Map<String, Map<String, String>> branding) {
217 this.branding = branding;
218 }
219
220 public void setStyleSheets(Map<String, List<String>> styleSheets) {
221 this.styleSheets = styleSheets;
222 }
223
224 public void setBundleContext(BundleContext bundleContext) {
225 this.bundleContext = bundleContext;
226 }
227
228 public void setResources(List<String> resources) {
229 this.resources = resources;
230 }
231
232 class CmsExceptionHandler implements ExceptionHandler {
233
234 @Override
235 public void handleException(Throwable throwable) {
236 CmsSession.current.get().exception(throwable);
237 }
238
239 }
240
241 private class CmsEntryPointFactory implements EntryPointFactory {
242 private final CmsUiProvider page;
243 private final Repository repository;
244 private final String workspace;
245 private final Map<String, String> properties;
246
247 public CmsEntryPointFactory(CmsUiProvider page, Repository repository,
248 String workspace, Map<String, String> properties) {
249 this.page = page;
250 this.repository = repository;
251 this.workspace = workspace;
252 this.properties = properties;
253 }
254
255 @Override
256 public EntryPoint create() {
257 SimpleErgonomics entryPoint = new SimpleErgonomics(repository,
258 workspace, basePath, page, properties);
259 // entryPoint.setState("");
260 entryPoint.setHeader(header);
261 entryPoint.setHeaderHeight(headerHeight);
262 CmsSession.current.set(entryPoint);
263 return entryPoint;
264 }
265
266 }
267
268 private static ResourceLoader createResourceLoader(final String resourceName) {
269 return new ResourceLoader() {
270 public InputStream getResourceAsStream(String resourceName)
271 throws IOException {
272 return getClass().getClassLoader().getResourceAsStream(
273 resourceName);
274 }
275 };
276 }
277
278 // private static ResourceLoader createUrlResourceLoader(final URL url) {
279 // return new ResourceLoader() {
280 // public InputStream getResourceAsStream(String resourceName)
281 // throws IOException {
282 // return url.openStream();
283 // }
284 // };
285 // }
286
287 /*
288 * TEXTS
289 */
290 private static String DEFAULT_LOADING_BODY = "<div"
291 + " style=\"position: absolute; left: 50%; top: 50%; margin: -32px -32px; width: 64px; height:64px\">"
292 + "<img src=\"./rwt-resources/" + LOADING_IMAGE
293 + "\" width=\"32\" height=\"32\" style=\"margin: 16px 16px\"/>"
294 + "</div>";
295 }