Make RAP Apps more extensible by entroducing multiple entry points.
[lgpl/argeo-commons.git] / org.argeo.cms.e4.rap / src / org / argeo / cms / e4 / rap / AbstractRapE4App.java
index da71e90ad43cad4d98466a0570c98c55eed0b859..1d70044b3e5d4edb800721dc311ad811aa1cec7f 100644 (file)
@@ -3,8 +3,6 @@ package org.argeo.cms.e4.rap;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.ui.dialogs.CmsFeedback;
 import org.eclipse.rap.e4.E4ApplicationConfig;
 import org.eclipse.rap.rwt.application.Application;
@@ -12,17 +10,14 @@ import org.eclipse.rap.rwt.application.Application.OperationMode;
 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
 import org.eclipse.rap.rwt.application.ExceptionHandler;
 import org.eclipse.rap.rwt.client.WebClient;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
 
+/** Base class for CMS RAP applications. */
 public abstract class AbstractRapE4App implements ApplicationConfiguration {
-       private final static Log log = LogFactory.getLog(AbstractRapE4App.class);
-
-       private final BundleContext bc = FrameworkUtil.getBundle(AbstractRapE4App.class).getBundleContext();
-
-       private String pageTitle;
        private String e4Xmi;
        private String path;
+       private String lifeCycleUri = "bundleclass://org.argeo.cms.e4.rap/org.argeo.cms.e4.rap.CmsLoginLifecycle";
+
+       Map<String, String> baseProperties = new HashMap<String, String>();
 
        public void configure(Application application) {
                application.setExceptionHandler(new ExceptionHandler() {
@@ -30,57 +25,44 @@ public abstract class AbstractRapE4App implements ApplicationConfiguration {
                        @Override
                        public void handleException(Throwable throwable) {
                                CmsFeedback.show("Unexpected RWT exception", throwable);
-                               // log.error("Unexpected RWT exception", throwable);
-
                        }
                });
 
-               String lifeCycleUri = "bundleclass://" + bc.getBundle().getSymbolicName() + "/"
-                               + CmsLoginLifecycle.class.getName();
-
-               Map<String, String> properties = new HashMap<String, String>();
-               properties.put(WebClient.PAGE_TITLE, pageTitle);
-               E4ApplicationConfig config = new E4ApplicationConfig(e4Xmi, lifeCycleUri, null, false, true, true);
-               addEntryPoint(application, config, properties);
-               // config.isClearPersistedState();
-               // E4EntryPointFactory entryPointFactory = new E4EntryPointFactory(config) {
-               //
-               // @Override
-               // public EntryPoint create() {
-               // Subject subject = new Subject();
-               // EntryPoint ep = createEntryPoint();
-               // EntryPoint authEp = new EntryPoint() {
-               //
-               // @Override
-               // public int createUI() {
-               // return Subject.doAs(subject, new PrivilegedAction<Integer>() {
-               //
-               // @Override
-               // public Integer run() {
-               // return ep.createUI();
-               // }
-               //
-               // });
-               // }
-               // };
-               // return authEp;
-               // }
-               //
-               // protected EntryPoint createEntryPoint() {
-               // return super.create();
-               // }
-               //
-               // };
+               if (e4Xmi != null) {// backward compatibility
+                       addE4EntryPoint(application, path, e4Xmi, getBaseProperties());
+               } else {
+                       addEntryPoints(application);
+               }
+       }
+
+       /**
+        * To be overridden in order to add multiple entry points, directly or using
+        * {@link #addE4EntryPoint(Application, String, String, Map)}.
+        */
+       protected void addEntryPoints(Application application) {
+
        }
 
-       protected void addEntryPoint(Application application, E4ApplicationConfig config, Map<String, String> properties) {
+       protected Map<String, String> getBaseProperties() {
+               return baseProperties;
+       }
+
+//     protected void addEntryPoint(Application application, E4ApplicationConfig config, Map<String, String> properties) {
+//             CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(config);
+//             application.addEntryPoint(path, entryPointFactory, properties);
+//             application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
+//     }
+
+       protected void addE4EntryPoint(Application application, String path, String e4Xmi, Map<String, String> properties) {
+               E4ApplicationConfig config = new E4ApplicationConfig(e4Xmi, lifeCycleUri, null, null, false, true, true);
                CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(config);
                application.addEntryPoint(path, entryPointFactory, properties);
                application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
        }
 
        public void setPageTitle(String pageTitle) {
-               this.pageTitle = pageTitle;
+               if (pageTitle != null)
+                       baseProperties.put(WebClient.PAGE_TITLE, pageTitle);
        }
 
        public void setE4Xmi(String e4Xmi) {
@@ -91,4 +73,15 @@ public abstract class AbstractRapE4App implements ApplicationConfiguration {
                this.path = path;
        }
 
+       public void setLifeCycleUri(String lifeCycleUri) {
+               this.lifeCycleUri = lifeCycleUri;
+       }
+
+       public void init(Map<String, Object> properties) {
+               for (String key : properties.keySet()) {
+                       Object value = properties.get(key);
+                       if (value != null)
+                               baseProperties.put(key, value.toString());
+               }
+       }
 }