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
.api
.cms
.CmsApp
;
11 import org
.argeo
.api
.cms
.CmsAppListener
;
12 import org
.argeo
.api
.cms
.CmsTheme
;
13 import org
.argeo
.api
.cms
.CmsView
;
14 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
15 import org
.argeo
.util
.LangUtils
;
16 import org
.eclipse
.rap
.rwt
.RWT
;
17 import org
.eclipse
.rap
.rwt
.application
.Application
;
18 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
19 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
20 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
21 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
22 import org
.eclipse
.swt
.widgets
.Display
;
23 import org
.osgi
.framework
.BundleContext
;
24 import org
.osgi
.framework
.Constants
;
25 import org
.osgi
.framework
.ServiceRegistration
;
26 import org
.osgi
.service
.event
.EventAdmin
;
28 /** An RWT web app integrating with a {@link CmsApp}. */
29 public class CmsWebApp
implements ApplicationConfiguration
, ExceptionHandler
, CmsAppListener
{
30 private final static Log log
= LogFactory
.getLog(CmsWebApp
.class);
32 private BundleContext bundleContext
;
33 private CmsApp cmsApp
;
34 private String cmsAppId
;
35 private EventAdmin eventAdmin
;
37 private ServiceRegistration
<ApplicationConfiguration
> rwtAppReg
;
39 private final static String CONTEXT_NAME
= "contextName";
40 private String contextName
;
42 private final static String FAVICON_PNG
= "favicon.png";
44 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
45 this.bundleContext
= bundleContext
;
46 contextName
= properties
.get(CONTEXT_NAME
);
48 if (cmsApp
.allThemesAvailable())
53 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
55 cmsApp
.removeCmsAppListener(this);
61 public void configure(Application application
) {
62 // TODO make it configurable?
63 // SWT compatibility is required for:
64 // - Browser.execute()
66 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
67 for (String uiName
: cmsApp
.getUiNames()) {
68 CmsTheme theme
= cmsApp
.getTheme(uiName
);
70 WebThemeUtils
.apply(application
, theme
);
73 Map
<String
, String
> properties
= new HashMap
<>();
74 addEntryPoints(application
, properties
);
75 application
.setExceptionHandler(this);
79 public void handleException(Throwable throwable
) {
80 Display display
= Display
.getCurrent();
81 if (display
!= null && !display
.isDisposed()) {
82 CmsView cmsView
= CmsSwtUtils
.getCmsView(display
.getActiveShell());
83 cmsView
.exception(throwable
);
85 log
.error("Unexpected exception outside an UI thread", throwable
);
90 protected void addEntryPoints(Application application
, Map
<String
, String
> commonProperties
) {
91 for (String uiName
: cmsApp
.getUiNames()) {
92 Map
<String
, String
> properties
= new HashMap
<>(commonProperties
);
93 CmsTheme theme
= cmsApp
.getTheme(uiName
);
95 properties
.put(WebClient
.THEME_ID
, theme
.getThemeId());
96 properties
.put(WebClient
.HEAD_HTML
, theme
.getHtmlHeaders());
97 properties
.put(WebClient
.BODY_HTML
, theme
.getBodyHtml());
98 Set
<String
> imagePaths
= theme
.getImagesPaths();
99 if (imagePaths
.contains(FAVICON_PNG
)) {
100 properties
.put(WebClient
.FAVICON
, FAVICON_PNG
);
103 properties
.put(WebClient
.THEME_ID
, RWT
.DEFAULT_THEME_ID
);
105 String entryPointName
= !uiName
.equals("") ?
"/" + uiName
: "/";
106 application
.addEntryPoint(entryPointName
, () -> {
107 CmsWebEntryPoint entryPoint
= new CmsWebEntryPoint(this, uiName
);
108 entryPoint
.setEventAdmin(eventAdmin
);
111 if (log
.isDebugEnabled())
112 log
.info("Added web entry point " + (contextName
!= null ?
"/" + contextName
: "") + entryPointName
);
114 // if (log.isDebugEnabled())
115 // log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
122 BundleContext
getBundleContext() {
123 return bundleContext
;
126 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
127 this.cmsApp
= cmsApp
;
128 this.cmsAppId
= properties
.get(Constants
.SERVICE_PID
);
129 this.cmsApp
.addCmsAppListener(this);
132 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
133 String cmsAppId
= properties
.get(Constants
.SERVICE_PID
);
134 if (!cmsAppId
.equals(this.cmsAppId
))
136 if (this.cmsApp
!= null) {
137 this.cmsApp
.removeCmsAppListener(this);
139 if (rwtAppReg
!= null)
140 rwtAppReg
.unregister();
145 public void themingUpdated() {
146 if (cmsApp
!= null && cmsApp
.allThemesAvailable())
150 protected void publishWebApp() {
151 Dictionary
<String
, Object
> regProps
= LangUtils
.dict(CONTEXT_NAME
, contextName
);
152 if (rwtAppReg
!= null)
153 rwtAppReg
.unregister();
154 if (bundleContext
!= null) {
155 rwtAppReg
= bundleContext
.registerService(ApplicationConfiguration
.class, this, regProps
);
156 if (log
.isDebugEnabled())
157 log
.debug("Publishing CMS web app /" + (contextName
!= null ? contextName
: "") + " ...");
161 public void setEventAdmin(EventAdmin eventAdmin
) {
162 this.eventAdmin
= eventAdmin
;