]> git.argeo.org Git - lgpl/argeo-commons.git/blob - web/CmsWebApp.java
Prepare next development cycle
[lgpl/argeo-commons.git] / web / CmsWebApp.java
1 package org.argeo.cms.web;
2
3 import java.util.Dictionary;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.cms.ui.CmsApp;
10 import org.argeo.cms.ui.CmsAppListener;
11 import org.argeo.cms.ui.CmsTheme;
12 import org.argeo.util.LangUtils;
13 import org.eclipse.rap.rwt.RWT;
14 import org.eclipse.rap.rwt.application.Application;
15 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
16 import org.eclipse.rap.rwt.client.WebClient;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceRegistration;
19 import org.osgi.service.event.EventAdmin;
20
21 /** An RWT web app integrating with a {@link CmsApp}. */
22 public class CmsWebApp implements ApplicationConfiguration, CmsAppListener {
23 private final static Log log = LogFactory.getLog(CmsWebApp.class);
24
25 private BundleContext bundleContext;
26 private CmsApp cmsApp;
27 private EventAdmin eventAdmin;
28
29 private ServiceRegistration<ApplicationConfiguration> rwtAppReg;
30
31 private final static String CONTEXT_NAME = "contextName";
32 private String contextName;
33
34 public void init(BundleContext bundleContext, Map<String, String> properties) {
35 this.bundleContext = bundleContext;
36 contextName = properties.get(CONTEXT_NAME);
37 if (cmsApp != null)
38 themingUpdated();
39 // registerIfAllThemesAvailable();
40 }
41
42 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
43 if (cmsApp != null)
44 cmsApp.removeCmsAppListener(this);
45 }
46
47 @Override
48 public void configure(Application application) {
49 for (String uiName : cmsApp.getUiNames()) {
50 CmsTheme theme = cmsApp.getTheme(uiName);
51 if (theme != null)
52 WebThemeUtils.apply(application, theme);
53 }
54 // for (CmsTheme theme : themes.values())
55 // WebThemeUtils.apply(application, theme);
56
57 Map<String, String> properties = new HashMap<>();
58 addEntryPoints(application, properties);
59
60 }
61
62 protected void addEntryPoints(Application application, Map<String, String> commonProperties) {
63 for (String uiName : cmsApp.getUiNames()) {
64 Map<String, String> properties = new HashMap<>(commonProperties);
65 CmsTheme theme = cmsApp.getTheme(uiName);
66 if (theme != null) {
67 properties.put(WebClient.THEME_ID, theme.getThemeId());
68 properties.put(WebClient.HEAD_HTML, theme.getHtmlHeaders());
69 } else {
70 properties.put(WebClient.THEME_ID, RWT.DEFAULT_THEME_ID);
71 // if (themeId != null)
72 // log.warn("Theme id " + themeId + " was specified but it was not found, using default RWT theme.");
73 }
74 application.addEntryPoint("/" + uiName, () -> {
75 CmsWebEntryPoint entryPoint = new CmsWebEntryPoint(this, uiName);
76 entryPoint.setEventAdmin(eventAdmin);
77 return entryPoint;
78 }, properties);
79 if (log.isDebugEnabled())
80 log.info("Added web entry point /" + (contextName != null ? contextName : "") + "/" + uiName);
81 }
82 log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
83 }
84
85 // private void registerIfAllThemesAvailable() {
86 // boolean themeMissing = false;
87 // uiNames: for (String uiName : cmsApp.getUiNames()) {
88 // String themeId = cmsApp.getThemeId(uiName);
89 // if (RWT.DEFAULT_THEME_ID.equals(themeId))
90 // continue uiNames;
91 // if (!themes.containsKey(themeId)) {
92 // themeMissing = true;
93 // break uiNames;
94 // }
95 // }
96 // if (!themeMissing) {
97 // Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
98 // if (bundleContext != null) {
99 // rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
100 // log.info("Published CMS web app /" + (contextName != null ? contextName : ""));
101 // }
102 // }
103 // }
104
105 CmsApp getCmsApp() {
106 return cmsApp;
107 }
108
109 public void setCmsApp(CmsApp cmsApp, Map<String, String> properties) {
110 this.cmsApp = cmsApp;
111 this.cmsApp.addCmsAppListener(this);
112 // registerIfAllThemesAvailable();
113 }
114
115 public void unsetCmsApp(CmsApp cmsApp, Map<String, String> properties) {
116 if (rwtAppReg != null)
117 rwtAppReg.unregister();
118 this.cmsApp = null;
119 }
120
121 @Override
122 public void themingUpdated() {
123 Dictionary<String, Object> regProps = LangUtils.dict(CONTEXT_NAME, contextName);
124 if (rwtAppReg != null)
125 rwtAppReg.unregister();
126 if (bundleContext != null) {
127 rwtAppReg = bundleContext.registerService(ApplicationConfiguration.class, this, regProps);
128 if (log.isDebugEnabled())
129 log.debug("Publishing CMS web app /" + (contextName != null ? contextName : "") + " ...");
130 }
131 }
132
133 public void setEventAdmin(EventAdmin eventAdmin) {
134 this.eventAdmin = eventAdmin;
135 }
136
137 }