1 package org
.argeo
.cms
.script
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.HashMap
;
7 import java
.util
.Hashtable
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.Property
;
13 import javax
.jcr
.PropertyIterator
;
14 import javax
.jcr
.PropertyType
;
15 import javax
.jcr
.Repository
;
16 import javax
.jcr
.RepositoryException
;
17 import javax
.script
.ScriptEngine
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.ui
.CmsConstants
;
23 import org
.argeo
.cms
.ui
.CmsUiProvider
;
24 import org
.argeo
.cms
.util
.BundleResourceLoader
;
25 import org
.argeo
.cms
.util
.CmsUtils
;
26 import org
.argeo
.cms
.util
.SimpleErgonomics
;
27 import org
.eclipse
.rap
.rwt
.application
.Application
;
28 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
29 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
30 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
31 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
32 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
33 import org
.osgi
.framework
.BundleContext
;
34 import org
.osgi
.framework
.ServiceRegistration
;
36 public class CmsScriptApp
implements Branding
{
37 public final static String CONTEXT_NAME
= "contextName";
39 ServiceRegistration
<ApplicationConfiguration
> appConfigReg
;
41 private ScriptEngine scriptEngine
;
43 private final static Log log
= LogFactory
.getLog(CmsScriptApp
.class);
45 private String webPath
;
46 private String repo
= "(cn=node)";
48 // private Branding branding = new Branding();
51 private List
<String
> resources
= new ArrayList
<>();
53 private Map
<String
, AppUi
> ui
= new HashMap
<>();
55 private CmsUiProvider header
;
56 private Integer headerHeight
= null;
57 private CmsUiProvider lead
;
58 private CmsUiProvider end
;
59 private CmsUiProvider footer
;
62 private String themeId
;
63 private String additionalHeaders
;
64 private String bodyHtml
;
65 private String pageTitle
;
66 private String pageOverflow
;
67 private String favicon
;
69 public CmsScriptApp(ScriptEngine scriptEngine
) {
71 this.scriptEngine
= scriptEngine
;
74 public void apply(BundleContext bundleContext
, Repository repository
, Application application
) {
75 BundleResourceLoader bundleRL
= new BundleResourceLoader(bundleContext
.getBundle());
77 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
78 // application.setOperationMode(OperationMode.JEE_COMPATIBILITY);
80 application
.setExceptionHandler(new CmsExceptionHandler());
82 // loading animated gif
83 application
.addResource(CmsConstants
.LOADING_IMAGE
, createResourceLoader(CmsConstants
.LOADING_IMAGE
));
85 application
.addResource(CmsConstants
.NO_IMAGE
, createResourceLoader(CmsConstants
.NO_IMAGE
));
87 for (String resource
: resources
) {
88 application
.addResource(resource
, bundleRL
);
89 if (log
.isTraceEnabled())
90 log
.trace("Resource " + resource
);
94 theme
.apply(application
);
95 String themeHeaders
= theme
.getAdditionalHeaders();
96 if (themeHeaders
!= null) {
97 if (additionalHeaders
== null)
98 additionalHeaders
= themeHeaders
;
100 additionalHeaders
= themeHeaders
+ "\n" + additionalHeaders
;
102 themeId
= theme
.getThemeId();
105 for (String appUiName
: ui
.keySet()) {
106 AppUi appUi
= ui
.get(appUiName
);
107 appUi
.apply(repository
, application
, this, appUiName
);
112 public void applySides(SimpleErgonomics simpleErgonomics
) {
113 simpleErgonomics
.setHeader(header
);
114 simpleErgonomics
.setLead(lead
);
115 simpleErgonomics
.setEnd(end
);
116 simpleErgonomics
.setFooter(footer
);
119 public void register(BundleContext bundleContext
, ApplicationConfiguration appConfig
) {
120 Hashtable
<String
, String
> props
= new Hashtable
<>();
121 props
.put(CONTEXT_NAME
, webPath
);
122 appConfigReg
= bundleContext
.registerService(ApplicationConfiguration
.class, appConfig
, props
);
125 public void reload() {
126 BundleContext bundleContext
= appConfigReg
.getReference().getBundle().getBundleContext();
127 ApplicationConfiguration appConfig
= bundleContext
.getService(appConfigReg
.getReference());
128 appConfigReg
.unregister();
129 register(bundleContext
, appConfig
);
131 // BundleContext bundleContext = (BundleContext)
132 // getScriptEngine().get("bundleContext");
134 // Bundle bundle = bundleContext.getBundle();
137 // } catch (BundleException e) {
138 // // TODO Auto-generated catch block
139 // e.printStackTrace();
143 private static ResourceLoader
createResourceLoader(final String resourceName
) {
144 return new ResourceLoader() {
145 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
146 return getClass().getClassLoader().getResourceAsStream(resourceName
);
151 public List
<String
> getResources() {
155 public AppUi
newUi(String name
) {
156 if (ui
.containsKey(name
))
157 throw new IllegalArgumentException("There is already an UI named " + name
);
158 AppUi appUi
= new AppUi(this);
159 // appUi.setApp(this);
164 public void addUi(String name
, AppUi appUi
) {
165 if (ui
.containsKey(name
))
166 throw new IllegalArgumentException("There is already an UI named " + name
);
167 // appUi.setApp(this);
171 public void applyBranding(Map
<String
, String
> properties
) {
173 properties
.put(WebClient
.THEME_ID
, themeId
);
174 if (additionalHeaders
!= null)
175 properties
.put(WebClient
.HEAD_HTML
, additionalHeaders
);
176 if (bodyHtml
!= null)
177 properties
.put(WebClient
.BODY_HTML
, bodyHtml
);
178 if (pageTitle
!= null)
179 properties
.put(WebClient
.PAGE_TITLE
, pageTitle
);
180 if (pageOverflow
!= null)
181 properties
.put(WebClient
.PAGE_OVERFLOW
, pageOverflow
);
183 properties
.put(WebClient
.FAVICON
, favicon
);
186 class CmsExceptionHandler
implements ExceptionHandler
{
189 public void handleException(Throwable throwable
) {
191 CmsUtils
.getCmsView().exception(throwable
);
196 // public Branding getBranding() {
200 ScriptEngine
getScriptEngine() {
204 public static String
toJson(Node node
) {
206 StringBuilder sb
= new StringBuilder();
208 PropertyIterator pit
= node
.getProperties();
210 while (pit
.hasNext()) {
211 Property p
= pit
.nextProperty();
212 int type
= p
.getType();
213 if (type
== PropertyType
.REFERENCE
|| type
== PropertyType
.WEAKREFERENCE
|| type
== PropertyType
.PATH
) {
214 Node ref
= p
.getNode();
218 sb
.append(toJson(ref
));
220 } else if (!p
.isMultiple()) {
223 sb
.append('\"').append(p
.getName()).append("\":\"").append(p
.getString()).append('\"');
228 return sb
.toString();
229 } catch (RepositoryException e
) {
230 throw new CmsException("Cannot convert " + node
+ " to JSON", e
);
234 public void fromJson(Node node
, String json
) {
238 public Theme
getTheme() {
242 public void setTheme(Theme theme
) {
246 public String
getWebPath() {
250 public void setWebPath(String context
) {
251 this.webPath
= context
;
254 public String
getRepo() {
258 public void setRepo(String repo
) {
262 public Map
<String
, AppUi
> getUi() {
266 public void setUi(Map
<String
, AppUi
> ui
) {
271 public String
getThemeId() {
275 public void setThemeId(String themeId
) {
276 this.themeId
= themeId
;
279 public String
getAdditionalHeaders() {
280 return additionalHeaders
;
283 public void setAdditionalHeaders(String additionalHeaders
) {
284 this.additionalHeaders
= additionalHeaders
;
287 public String
getBodyHtml() {
291 public void setBodyHtml(String bodyHtml
) {
292 this.bodyHtml
= bodyHtml
;
295 public String
getPageTitle() {
299 public void setPageTitle(String pageTitle
) {
300 this.pageTitle
= pageTitle
;
303 public String
getPageOverflow() {
307 public void setPageOverflow(String pageOverflow
) {
308 this.pageOverflow
= pageOverflow
;
311 public String
getFavicon() {
315 public void setFavicon(String favicon
) {
316 this.favicon
= favicon
;
319 public CmsUiProvider
getHeader() {
323 public void setHeader(CmsUiProvider header
) {
324 this.header
= header
;
327 public Integer
getHeaderHeight() {
331 public void setHeaderHeight(Integer headerHeight
) {
332 this.headerHeight
= headerHeight
;
335 public CmsUiProvider
getLead() {
339 public void setLead(CmsUiProvider lead
) {
343 public CmsUiProvider
getEnd() {
347 public void setEnd(CmsUiProvider end
) {
351 public CmsUiProvider
getFooter() {
355 public void setFooter(CmsUiProvider footer
) {
356 this.footer
= footer
;