1 package org
.argeo
.cms
.web
;
3 import java
.util
.Dictionary
;
4 import java
.util
.HashMap
;
8 import org
.argeo
.api
.cms
.CmsApp
;
9 import org
.argeo
.api
.cms
.CmsAppListener
;
10 import org
.argeo
.api
.cms
.CmsLog
;
11 import org
.argeo
.api
.cms
.CmsTheme
;
12 import org
.argeo
.api
.cms
.CmsView
;
13 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
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
.ServiceRegistration
;
24 import org
.osgi
.service
.event
.EventAdmin
;
26 /** An RWT web app integrating with a {@link CmsApp}. */
27 public class CmsWebApp
implements ApplicationConfiguration
, ExceptionHandler
, CmsAppListener
{
28 private final static CmsLog log
= CmsLog
.getLog(CmsWebApp
.class);
30 private BundleContext bundleContext
;
31 private CmsApp cmsApp
;
32 // private String cmsAppId;
33 private EventAdmin eventAdmin
;
35 private ServiceRegistration
<ApplicationConfiguration
> rwtAppReg
;
37 private final static String CONTEXT_NAME
= "contextName";
38 private String contextName
;
40 private final static String FAVICON_PNG
= "favicon.png";
42 public void init(BundleContext bundleContext
, Map
<String
, String
> properties
) {
43 this.bundleContext
= bundleContext
;
44 contextName
= properties
.get(CONTEXT_NAME
);
46 if (cmsApp
.allThemesAvailable())
51 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
53 cmsApp
.removeCmsAppListener(this);
59 public void configure(Application application
) {
60 // TODO make it configurable?
61 // SWT compatibility is required for:
62 // - Browser.execute()
64 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
65 for (String uiName
: cmsApp
.getUiNames()) {
66 CmsTheme theme
= cmsApp
.getTheme(uiName
);
68 WebThemeUtils
.apply(application
, theme
);
71 Map
<String
, String
> properties
= new HashMap
<>();
72 addEntryPoints(application
, properties
);
73 application
.setExceptionHandler(this);
77 public void handleException(Throwable throwable
) {
78 Display display
= Display
.getCurrent();
79 if (display
!= null && !display
.isDisposed()) {
80 CmsView cmsView
= CmsSwtUtils
.getCmsView(display
.getActiveShell());
81 cmsView
.exception(throwable
);
83 log
.error("Unexpected exception outside an UI thread", throwable
);
88 protected void addEntryPoints(Application application
, Map
<String
, String
> commonProperties
) {
89 for (String uiName
: cmsApp
.getUiNames()) {
90 Map
<String
, String
> properties
= new HashMap
<>(commonProperties
);
91 CmsTheme theme
= cmsApp
.getTheme(uiName
);
93 properties
.put(WebClient
.THEME_ID
, theme
.getThemeId());
94 properties
.put(WebClient
.HEAD_HTML
, theme
.getHtmlHeaders());
95 properties
.put(WebClient
.BODY_HTML
, theme
.getBodyHtml());
96 Set
<String
> imagePaths
= theme
.getImagesPaths();
97 if (imagePaths
.contains(FAVICON_PNG
)) {
98 properties
.put(WebClient
.FAVICON
, FAVICON_PNG
);
101 properties
.put(WebClient
.THEME_ID
, RWT
.DEFAULT_THEME_ID
);
103 String entryPointName
= !uiName
.equals("") ?
"/" + uiName
: "/";
104 application
.addEntryPoint(entryPointName
, () -> {
105 CmsWebEntryPoint entryPoint
= new CmsWebEntryPoint(this, uiName
);
106 entryPoint
.setEventAdmin(eventAdmin
);
109 if (log
.isDebugEnabled())
110 log
.info("Added web entry point " + (contextName
!= null ?
"/" + contextName
: "") + entryPointName
);
112 // if (log.isDebugEnabled())
113 // log.debug("Published CMS web app /" + (contextName != null ? contextName : ""));
120 BundleContext
getBundleContext() {
121 return bundleContext
;
124 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
125 this.cmsApp
= cmsApp
;
126 // this.cmsAppId = properties.get(Constants.SERVICE_PID);
127 this.cmsApp
.addCmsAppListener(this);
130 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
131 String contextName
= properties
.get(CmsApp
.CONTEXT_NAME_PROPERTY
);
132 if (!contextName
.equals(this.contextName
))
134 if (this.cmsApp
!= null) {
135 this.cmsApp
.removeCmsAppListener(this);
137 if (rwtAppReg
!= null)
138 rwtAppReg
.unregister();
143 public void themingUpdated() {
144 if (cmsApp
!= null && cmsApp
.allThemesAvailable())
148 protected void publishWebApp() {
149 Dictionary
<String
, Object
> regProps
= LangUtils
.dict(CONTEXT_NAME
, contextName
);
150 if (rwtAppReg
!= null)
151 rwtAppReg
.unregister();
152 if (bundleContext
!= null) {
153 rwtAppReg
= bundleContext
.registerService(ApplicationConfiguration
.class, this, regProps
);
154 if (log
.isDebugEnabled())
155 log
.debug("Publishing CMS web app /" + (contextName
!= null ? contextName
: "") + " ...");
159 public void setEventAdmin(EventAdmin eventAdmin
) {
160 this.eventAdmin
= eventAdmin
;