Improve extensibility and logging.
[lgpl/argeo-commons.git] / org.argeo.cms.e4.rap / src / org / argeo / cms / e4 / rap / AbstractRapE4App.java
index c70e381c392442e91661fc7aa7084e9b3a885892..17c85ffb46630ec79d0073938446e06045af6250 100644 (file)
 package org.argeo.cms.e4.rap;
 
-import java.security.PrivilegedAction;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.security.auth.Subject;
-
+import org.argeo.cms.ui.dialogs.CmsFeedback;
 import org.eclipse.rap.e4.E4ApplicationConfig;
-import org.eclipse.rap.e4.E4EntryPointFactory;
 import org.eclipse.rap.rwt.application.Application;
 import org.eclipse.rap.rwt.application.Application.OperationMode;
 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
-import org.eclipse.rap.rwt.application.EntryPoint;
+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 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";
 
-       public void configure(Application application) {
-               String lifeCycleUri = "bundleclass://" + bc.getBundle().getSymbolicName() + "/"
-                               + CmsLoginLifecycle.class.getName();
+       private Map<String, String> baseProperties = new HashMap<String, String>();
+
+       private BundleContext bundleContext;
+       public final static String CONTEXT_NAME_PROPERTY = "contextName";
+       private String contextName;
 
-               Map<String, String> properties = new HashMap<String, String>();
-               properties.put(WebClient.PAGE_TITLE, pageTitle);
-               E4ApplicationConfig config = new E4ApplicationConfig(e4Xmi, lifeCycleUri, null, false, true, true);
-               config.isClearPersistedState();
-               E4EntryPointFactory entryPointFactory = new E4EntryPointFactory(config) {
+       /**
+        * To be overridden in order to add multiple entry points, directly or using
+        * {@link #addE4EntryPoint(Application, String, String, Map)}.
+        */
+       protected void addEntryPoints(Application application) {
+       }
+
+       public void configure(Application application) {
+               application.setExceptionHandler(new ExceptionHandler() {
 
                        @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;
+                       public void handleException(Throwable throwable) {
+                               CmsFeedback.show("Unexpected RWT exception", throwable);
                        }
+               });
 
-                       protected EntryPoint createEntryPoint() {
-                               return super.create();
-                       }
+               if (e4Xmi != null) {// backward compatibility
+                       addE4EntryPoint(application, path, e4Xmi, getBaseProperties());
+               } else {
+                       addEntryPoints(application);
+               }
+       }
+
+       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 = createE4ApplicationConfig(e4Xmi);
+               CmsE4EntryPointFactory entryPointFactory = new CmsE4EntryPointFactory(config);
                application.addEntryPoint(path, entryPointFactory, properties);
                application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
        }
 
+       /**
+        * To be overridden for further configuration.
+        * 
+        * @see E4ApplicationConfig
+        */
+       protected E4ApplicationConfig createE4ApplicationConfig(String e4Xmi) {
+               return new E4ApplicationConfig(e4Xmi, lifeCycleUri, null, null, false, true, true);
+       }
+
+       @Deprecated
        public void setPageTitle(String pageTitle) {
-               this.pageTitle = pageTitle;
+               if (pageTitle != null)
+                       baseProperties.put(WebClient.PAGE_TITLE, pageTitle);
        }
 
+       /** Returns a new map used to customise and entry point. */
+       public Map<String, String> customise(String pageTitle) {
+               Map<String, String> custom = new HashMap<>(getBaseProperties());
+               if (pageTitle != null)
+                       custom.put(WebClient.PAGE_TITLE, pageTitle);
+               return custom;
+       }
+
+       @Deprecated
        public void setE4Xmi(String e4Xmi) {
                this.e4Xmi = e4Xmi;
        }
 
+       @Deprecated
        public void setPath(String path) {
                this.path = path;
        }
 
+       public void setLifeCycleUri(String lifeCycleUri) {
+               this.lifeCycleUri = lifeCycleUri;
+       }
+
+       protected BundleContext getBundleContext() {
+               return bundleContext;
+       }
+
+       public String getContextName() {
+               return contextName;
+       }
+
+       public void setContextName(String contextName) {
+               this.contextName = contextName;
+       }
+
+       public void init(BundleContext bundleContext, Map<String, Object> properties) {
+               this.bundleContext = bundleContext;
+               for (String key : properties.keySet()) {
+                       Object value = properties.get(key);
+                       if (value != null)
+                               baseProperties.put(key, value.toString());
+               }
+
+               if (properties.containsKey(CONTEXT_NAME_PROPERTY)) {
+                       assert properties.get(CONTEXT_NAME_PROPERTY) != null;
+                       contextName = properties.get(CONTEXT_NAME_PROPERTY).toString();
+               } else {
+                       contextName = "<unknown context>";
+               }
+       }
+
+       public void destroy(Map<String, Object> properties) {
+
+       }
 }