1 package org
.argeo
.cms
.web
;
3 import java
.util
.Dictionary
;
4 import java
.util
.HashMap
;
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
.cms
.ui
.CmsView
;
13 import org
.argeo
.util
.LangUtils
;
14 import org
.eclipse
.rap
.rwt
.RWT
;
15 import org
.eclipse
.rap
.rwt
.application
.Application
;
16 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
17 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
18 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
19 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.osgi
.framework
.BundleContext
;
22 import org
.osgi
.framework
.ServiceRegistration
;
23 import org
.osgi
.service
.event
.EventAdmin
;
25 /** An RWT web app integrating with a {@link CmsApp}. */
26 public class CmsWebApp
implements ApplicationConfiguration
, ExceptionHandler
, CmsAppListener
{
27 private final static Log log
= LogFactory
.getLog(CmsWebApp
.class);
29 private BundleContext bundleContext
;
30 private CmsApp cmsApp
;
31 private EventAdmin eventAdmin
;
33 private ServiceRegistration
<ApplicationConfiguration
> rwtAppReg
;
35 private final static String CONTEXT_NAME
= "contextName";
36 private String contextName
;
38 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
39 this.bundleContext
= bundleContext
;
40 contextName
= properties
.get(CONTEXT_NAME
);
45 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
47 cmsApp
.removeCmsAppListener(this);
51 public void configure(Application application
) {
52 // TODO make it configurable?
53 // SWT compatibility is required for:
54 // - Browser.execute()
56 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
57 for (String uiName
: cmsApp
.getUiNames()) {
58 CmsTheme theme
= cmsApp
.getTheme(uiName
);
60 WebThemeUtils
.apply(application
, theme
);
63 Map
<String
, String
> properties
= new HashMap
<>();
64 addEntryPoints(application
, properties
);
65 application
.setExceptionHandler(this);
69 public void handleException(Throwable throwable
) {
70 Display display
= Display
.getCurrent();
71 if (display
!= null && !display
.isDisposed()) {
72 CmsView cmsView
= CmsView
.getCmsView(display
.getActiveShell());
73 cmsView
.exception(throwable
);
75 log
.error("Unexpected exception outside an UI thread", throwable
);
80 protected void addEntryPoints(Application application
, Map
<String
, String
> commonProperties
) {
81 for (String uiName
: cmsApp
.getUiNames()) {
82 Map
<String
, String
> properties
= new HashMap
<>(commonProperties
);
83 CmsTheme theme
= cmsApp
.getTheme(uiName
);
85 properties
.put(WebClient
.THEME_ID
, theme
.getThemeId());
86 properties
.put(WebClient
.HEAD_HTML
, theme
.getHtmlHeaders());
88 properties
.put(WebClient
.THEME_ID
, RWT
.DEFAULT_THEME_ID
);
90 String entryPointName
= !uiName
.equals("") ?
"/" + uiName
: "/";
91 application
.addEntryPoint(entryPointName
, () -> {
92 CmsWebEntryPoint entryPoint
= new CmsWebEntryPoint(this, uiName
);
93 entryPoint
.setEventAdmin(eventAdmin
);
96 if (log
.isDebugEnabled())
97 log
.info("Added web entry point " + (contextName
!= null ?
"/" + contextName
: "") + entryPointName
);
99 if (log
.isDebugEnabled())
100 log
.debug("Published CMS web app /" + (contextName
!= null ? contextName
: ""));
107 BundleContext
getBundleContext() {
108 return bundleContext
;
111 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
112 this.cmsApp
= cmsApp
;
113 this.cmsApp
.addCmsAppListener(this);
116 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
117 if (rwtAppReg
!= null)
118 rwtAppReg
.unregister();
123 public void themingUpdated() {
124 Dictionary
<String
, Object
> regProps
= LangUtils
.dict(CONTEXT_NAME
, contextName
);
125 if (rwtAppReg
!= null)
126 rwtAppReg
.unregister();
127 if (bundleContext
!= null) {
128 rwtAppReg
= bundleContext
.registerService(ApplicationConfiguration
.class, this, regProps
);
129 if (log
.isDebugEnabled())
130 log
.debug("Publishing CMS web app /" + (contextName
!= null ? contextName
: "") + " ...");
134 public void setEventAdmin(EventAdmin eventAdmin
) {
135 this.eventAdmin
= eventAdmin
;