X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Futil%2FSimpleApp.java;h=39e75070b450c032cc3665b427925802b680b82d;hb=c873a0359345503b8e3ca07828bd99d525ec7cc0;hp=9f0e32037f7c9c78c3080ba8c97325f4a1d0a1ec;hpb=429e967572de77a6144bd3d789b3a2f8002a590e;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/util/SimpleApp.java b/org.argeo.cms/src/org/argeo/cms/util/SimpleApp.java index 9f0e32037..39e75070b 100644 --- a/org.argeo.cms/src/org/argeo/cms/util/SimpleApp.java +++ b/org.argeo.cms/src/org/argeo/cms/util/SimpleApp.java @@ -5,6 +5,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -22,7 +23,7 @@ import org.argeo.cms.CmsException; import org.argeo.cms.CmsUiProvider; import org.argeo.cms.LifeCycleUiProvider; import org.argeo.jcr.JcrUtils; -import org.eclipse.gemini.blueprint.context.BundleContextAware; +import org.eclipse.rap.rwt.RWT; import org.eclipse.rap.rwt.application.Application; import org.eclipse.rap.rwt.application.Application.OperationMode; import org.eclipse.rap.rwt.application.ApplicationConfiguration; @@ -30,14 +31,23 @@ import org.eclipse.rap.rwt.application.EntryPoint; import org.eclipse.rap.rwt.application.EntryPointFactory; import org.eclipse.rap.rwt.application.ExceptionHandler; import org.eclipse.rap.rwt.client.WebClient; +import org.eclipse.rap.rwt.client.service.JavaScriptExecutor; import org.eclipse.rap.rwt.service.ResourceLoader; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; /** A basic generic app based on {@link SimpleErgonomics}. */ -public class SimpleApp implements CmsConstants, ApplicationConfiguration, - BundleContextAware { +public class SimpleApp implements CmsConstants, ApplicationConfiguration { private final static Log log = LogFactory.getLog(SimpleApp.class); + private String contextName = null; + private Map> branding = new HashMap>(); private Map> styleSheets = new HashMap>(); @@ -47,7 +57,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, private Repository repository; private String workspace = null; - private String basePath = "/"; + private String jcrBasePath = "/"; private List roPrincipals = Arrays.asList("anonymous", "everyone"); private List rwPrincipals = Arrays.asList("everyone"); @@ -56,8 +66,15 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, private Integer headerHeight = 40; + private ServiceRegistration appReg; + public void configure(Application application) { try { + StyleSheetResourceLoader styleSheetRL = new StyleSheetResourceLoader( + bundleContext); + BundleResourceLoader bundleRL = new BundleResourceLoader( + bundleContext); + application.setOperationMode(OperationMode.SWT_COMPATIBILITY); // application.setOperationMode(OperationMode.JEE_COMPATIBILITY); @@ -70,10 +87,9 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE)); for (String resource : resources) { - application.addResource(resource, new BundleResourceLoader( - bundleContext)); - if (log.isDebugEnabled()) - log.debug("Registered resource " + resource); + application.addResource(resource, bundleRL); + if (log.isTraceEnabled()) + log.trace("Resource " + resource); } Map defaultBranding = null; @@ -93,7 +109,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, application.addResource(faviconRelPath, new BundleResourceLoader(bundleContext)); if (log.isTraceEnabled()) - log.trace("Registered favicon " + faviconRelPath); + log.trace("Favicon " + faviconRelPath); } @@ -116,15 +132,18 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, application.addEntryPoint("/" + page, new CmsEntryPointFactory( pages.get(page), repository, workspace, properties), properties); - log.info("Registered entry point /" + page); + log.info("Page /" + page); } // stylesheets for (String themeId : styleSheets.keySet()) { List cssLst = styleSheets.get(themeId); + if (log.isDebugEnabled()) + log.debug("Theme " + themeId); for (String css : cssLst) { - application.addStyleSheet(themeId, css, - new BundleResourceLoader(bundleContext)); + application.addStyleSheet(themeId, css, styleSheetRL); + if (log.isTraceEnabled()) + log.trace(" CSS " + css); } } @@ -143,12 +162,12 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, VersionManager vm = session.getWorkspace().getVersionManager(); if (!vm.isCheckedOut("/")) vm.checkout("/"); - JcrUtils.mkdirs(session, basePath); + JcrUtils.mkdirs(session, jcrBasePath); for (String principal : rwPrincipals) - JcrUtils.addPrivilege(session, basePath, principal, + JcrUtils.addPrivilege(session, jcrBasePath, principal, Privilege.JCR_WRITE); for (String principal : roPrincipals) - JcrUtils.addPrivilege(session, basePath, principal, + JcrUtils.addPrivilege(session, jcrBasePath, principal, Privilege.JCR_READ); for (String pageName : pages.keySet()) { @@ -164,6 +183,9 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, } finally { JcrUtils.logoutQuietly(session); } + + // publish to OSGi + register(); } protected void initPage(Session adminSession, CmsUiProvider page) @@ -184,6 +206,23 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, } } + protected void register() { + Hashtable props = new Hashtable(); + if (contextName != null) + props.put("contextName", contextName); + appReg = bundleContext.registerService(ApplicationConfiguration.class, + this, props); + if (log.isDebugEnabled()) + log.debug("Registered " + (contextName == null ? "/" : contextName)); + } + + protected void unregister() { + appReg.unregister(); + if (log.isDebugEnabled()) + log.debug("Unregistered " + + (contextName == null ? "/" : contextName)); + } + public void setRepository(Repository repository) { this.repository = repository; } @@ -200,8 +239,8 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, this.pages = pages; } - public void setBasePath(String basePath) { - this.basePath = basePath; + public void setJcrBasePath(String basePath) { + this.jcrBasePath = basePath; } public void setRoPrincipals(List roPrincipals) { @@ -232,10 +271,15 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, this.resources = resources; } + public void setContextName(String contextName) { + this.contextName = contextName; + } + class CmsExceptionHandler implements ExceptionHandler { @Override public void handleException(Throwable throwable) { + // TODO be smarter CmsUtils.getCmsView().exception(throwable); } @@ -258,7 +302,32 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, @Override public EntryPoint create() { SimpleErgonomics entryPoint = new SimpleErgonomics(repository, - workspace, basePath, page, properties); + workspace, jcrBasePath, page, properties) { + + @Override + protected void createAdminArea(Composite parent) { + Composite adminArea = new Composite(parent, SWT.NONE); + adminArea.setLayout(new FillLayout()); + Button refresh = new Button(adminArea, SWT.PUSH); + refresh.setText("Reload App"); + refresh.addSelectionListener(new SelectionAdapter() { + private static final long serialVersionUID = -7671999525536351366L; + + @Override + public void widgetSelected(SelectionEvent e) { + long timeBeforeReload = 1000; + RWT.getClient() + .getService(JavaScriptExecutor.class) + .execute( + "setTimeout(function() { " + + "location.reload();" + + "}," + timeBeforeReload + + ");"); + reloadApp(); + } + }); + } + }; // entryPoint.setState(""); entryPoint.setHeader(header); entryPoint.setHeaderHeight(headerHeight); @@ -266,6 +335,15 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration, return entryPoint; } + private void reloadApp() { + new Thread("Refresh app") { + @Override + public void run() { + unregister(); + register(); + } + }.start(); + } } private static ResourceLoader createResourceLoader(final String resourceName) {