1 package org
.argeo
.cms
.web
;
3 import java
.util
.Dictionary
;
4 import java
.util
.HashMap
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.cms
.ui
.CmsApp
;
11 import org
.argeo
.cms
.ui
.CmsAppListener
;
12 import org
.argeo
.cms
.ui
.CmsTheme
;
13 import org
.argeo
.cms
.ui
.CmsView
;
14 import org
.argeo
.util
.LangUtils
;
15 import org
.eclipse
.rap
.rwt
.RWT
;
16 import org
.eclipse
.rap
.rwt
.application
.Application
;
17 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
18 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
19 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
20 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
21 import org
.eclipse
.swt
.widgets
.Display
;
22 import org
.osgi
.framework
.BundleContext
;
23 import org
.osgi
.framework
.Constants
;
24 import org
.osgi
.framework
.ServiceRegistration
;
25 import org
.osgi
.service
.event
.EventAdmin
;
27 /** An RWT web app integrating with a {@link CmsApp}. */
28 public class CmsWebApp
implements ApplicationConfiguration
, ExceptionHandler
, CmsAppListener
{
29 private final static Log log
= LogFactory
.getLog(CmsWebApp
.class);
31 private BundleContext bundleContext
;
32 private CmsApp cmsApp
;
33 private String cmsAppId
;
34 private EventAdmin eventAdmin
;
36 private ServiceRegistration
<ApplicationConfiguration
> rwtAppReg
;
38 private final static String CONTEXT_NAME
= "contextName";
39 private String contextName
;
41 private final static String FAVICON_PNG
= "favicon.png";
43 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
44 this.bundleContext
= bundleContext
;
45 contextName
= properties
.get(CONTEXT_NAME
);
47 if (cmsApp
.allThemesAvailable())
52 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
54 cmsApp
.removeCmsAppListener(this);
60 public void configure(Application application
) {
61 // TODO make it configurable?
62 // SWT compatibility is required for:
63 // - Browser.execute()
65 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
66 for (String uiName
: cmsApp
.getUiNames()) {
67 CmsTheme theme
= cmsApp
.getTheme(uiName
);
69 WebThemeUtils
.apply(application
, theme
);
72 Map
<String
, String
> properties
= new HashMap
<>();
73 addEntryPoints(application
, properties
);
74 application
.setExceptionHandler(this);
78 public void handleException(Throwable throwable
) {
79 Display display
= Display
.getCurrent();
80 if (display
!= null && !display
.isDisposed()) {
81 CmsView cmsView
= CmsView
.getCmsView(display
.getActiveShell());
82 cmsView
.exception(throwable
);
84 log
.error("Unexpected exception outside an UI thread", throwable
);
89 protected void addEntryPoints(Application application
, Map
<String
, String
> commonProperties
) {
90 for (String uiName
: cmsApp
.getUiNames()) {
91 Map
<String
, String
> properties
= new HashMap
<>(commonProperties
);
92 CmsTheme theme
= cmsApp
.getTheme(uiName
);
94 properties
.put(WebClient
.THEME_ID
, theme
.getThemeId());
95 properties
.put(WebClient
.HEAD_HTML
, theme
.getHtmlHeaders());
96 properties
.put(WebClient
.BODY_HTML
, theme
.getBodyHtml());
97 Set
<String
> imagePaths
= theme
.getImagesPaths();
98 if (imagePaths
.contains(FAVICON_PNG
)) {
99 properties
.put(WebClient
.FAVICON
, FAVICON_PNG
);
102 properties
.put(WebClient
.THEME_ID
, RWT
.DEFAULT_THEME_ID
);
104 String entryPointName
= !uiName
.equals("") ?
"/" + uiName
: "/";
105 application
.addEntryPoint(entryPointName
, () -> {
106 CmsWebEntryPoint entryPoint
= new CmsWebEntryPoint(this, uiName
);
107 entryPoint
.setEventAdmin(eventAdmin
);
110 if (log
.isDebugEnabled())
111 log
.info("Added web entry point " + (contextName
!= null ?
"/" + contextName
: "") + entryPointName
);
113 // if (log.isDebugEnabled())
114 // log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
121 BundleContext
getBundleContext() {
122 return bundleContext
;
125 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
126 this.cmsApp
= cmsApp
;
127 this.cmsAppId
= properties
.get(Constants
.SERVICE_PID
);
128 this.cmsApp
.addCmsAppListener(this);
131 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
132 String cmsAppId
= properties
.get(Constants
.SERVICE_PID
);
133 if (!cmsAppId
.equals(this.cmsAppId
))
135 if (this.cmsApp
!= null) {
136 this.cmsApp
.removeCmsAppListener(this);
138 if (rwtAppReg
!= null)
139 rwtAppReg
.unregister();
144 public void themingUpdated() {
145 if (cmsApp
!= null && cmsApp
.allThemesAvailable())
149 protected void publishWebApp() {
150 Dictionary
<String
, Object
> regProps
= LangUtils
.dict(CONTEXT_NAME
, contextName
);
151 if (rwtAppReg
!= null)
152 rwtAppReg
.unregister();
153 if (bundleContext
!= null) {
154 rwtAppReg
= bundleContext
.registerService(ApplicationConfiguration
.class, this, regProps
);
155 if (log
.isDebugEnabled())
156 log
.debug("Publishing CMS web app /" + (contextName
!= null ? contextName
: "") + " ...");
160 public void setEventAdmin(EventAdmin eventAdmin
) {
161 this.eventAdmin
= eventAdmin
;