added new project for remoting
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationRemoteController.java
diff --git a/eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteController.java b/eu.etaxonomy.taxeditor.remoting/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteController.java
new file mode 100644 (file)
index 0000000..26a5ba9
--- /dev/null
@@ -0,0 +1,194 @@
+/**
+* Copyright (C) 2014 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+
+package eu.etaxonomy.cdm.api.application;
+
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.log4j.Logger;
+import org.hibernate.collection.internal.AbstractPersistentCollection;
+import org.hibernate.proxy.AbstractLazyInitializer;
+import org.springframework.beans.MutablePropertyValues;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
+import eu.etaxonomy.cdm.common.monitor.NullProgressMonitor;
+import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
+import eu.etaxonomy.cdm.remote.ICdmRemoteSource;
+
+/**
+ * CDM Application Controller class for remoting clients
+ *
+ * FIXME:Remoting extending {@link CdmApplicationController} is a temporary workaround.
+ * The {@link CdmApplicationController} should be split into a CdmApplicationControllerBase
+ * class with {@link CdmApplicationController} and this class as subclasses
+ *
+ */
+public class CdmApplicationRemoteController  extends CdmApplicationController {
+
+    private static final Logger logger = Logger.getLogger(CdmApplicationRemoteController.class);
+
+    public static final Resource DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE =
+               new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
+    private final Resource applicationContextResource;
+    private final IProgressMonitor progressMonitor;
+
+    /**
+     * Creates new instance of CdmApplicationRemoteController
+     *
+     * @param applicationContextResource
+     * @param remoteSource
+     * @param omitTermLoading
+     * @param progressMonitor
+     * @param listeners
+     * @return
+     */
+    public static CdmApplicationRemoteController NewInstance(Resource applicationContextResource,
+                       ICdmRemoteSource remoteSource,
+                       boolean omitTermLoading,
+                       IProgressMonitor progressMonitor,
+                       List<ApplicationListener> listeners) {
+       return new CdmApplicationRemoteController(applicationContextResource,
+                       remoteSource,
+                       omitTermLoading,
+                       progressMonitor,
+                               listeners);
+
+    }
+    /**
+     * Creates new instance of CdmApplicationRemoteController
+     *
+     * @param applicationContextResource
+     * @param remoteSource
+     * @param omitTermLoading
+     * @param progressMonitor
+     * @param listeners
+     * @return
+     */
+    public static CdmApplicationRemoteController NewInstance(ICdmRemoteSource remoteSource,
+                       boolean omitTermLoading,
+                       IProgressMonitor progressMonitor,
+                       List<ApplicationListener> listeners) {
+
+       return new CdmApplicationRemoteController(DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE,
+                       remoteSource,
+                       omitTermLoading,
+                       progressMonitor,
+                               listeners);
+
+    }
+
+       /**
+        * Constructs CdmApplicationRemoteController
+        *
+        * @param applicationContextResource
+        * @param remoteSource
+        * @param omitTermLoading
+        * @param progressMonitor
+        * @param listeners
+        */
+       private CdmApplicationRemoteController(Resource applicationContextResource,
+                               ICdmRemoteSource remoteSource,
+                               boolean omitTermLoading,
+                               IProgressMonitor progressMonitor,
+                               List<ApplicationListener> listeners){
+               logger.info("Start CdmApplicationRemoteController with remote source: " + remoteSource.getName());
+        this.applicationContextResource =
+                       applicationContextResource != null ? applicationContextResource : DEFAULT_REMOTE_APPLICATION_CONTEXT_RESOURCE;
+        this.progressMonitor = progressMonitor != null ? progressMonitor : new NullProgressMonitor();
+
+        setNewRemoteSource(remoteSource, omitTermLoading, listeners);
+
+       }
+
+
+
+
+    /**
+     * Creates and starts a new spring application context
+     *
+     * @param remoteSource object for connecting to an http-invoker server
+     * @param omitTermLoading
+     * @param listeners
+     * @return
+     */
+    protected boolean setNewRemoteSource(ICdmRemoteSource remoteSource,
+               boolean omitTermLoading,
+               List<ApplicationListener> listeners){
+
+        logger.info("Connecting to '" + remoteSource.getName() + "'");
+
+        MonitoredGenericApplicationContext applicationContext =  new MonitoredGenericApplicationContext();
+        int refreshTasks = 45;
+        int nTasks = 5 + refreshTasks;
+
+        progressMonitor.beginTask("Connecting to '" + remoteSource.getName() + "'", nTasks);
+
+        progressMonitor.subTask("Registering remote source.");
+        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
+        Properties properties = new Properties();
+        properties.setProperty("remoteServer", remoteSource.getServer());
+        properties.setProperty("remotePort", String.valueOf(remoteSource.getPort()));
+        properties.setProperty("remoteContext", remoteSource.getContextPath());
+        pspc.setProperties(properties);
+        applicationContext.addBeanFactoryPostProcessor(pspc);
+        applicationContext.getEnvironment().setActiveProfiles("remoting");
+        progressMonitor.worked(1);
+
+        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext);
+        //xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
+        progressMonitor.subTask("Registering resources.");
+        xmlReader.loadBeanDefinitions(applicationContextResource);
+        progressMonitor.worked(1);
+
+        //omitTerms
+        if (omitTermLoading == true){
+            String initializerName = "persistentTermInitializer";
+            BeanDefinition beanDef = applicationContext.getBeanDefinition(initializerName);
+            MutablePropertyValues values = beanDef.getPropertyValues();
+            values.addPropertyValue("omit", omitTermLoading);
+        }
+
+        if (listeners != null){
+            for(ApplicationListener listener : listeners){
+                applicationContext.addApplicationListener(listener);
+            }
+        }
+
+
+        applicationContext.refresh(new SubProgressMonitor(progressMonitor, refreshTasks));
+        applicationContext.start();
+
+        progressMonitor.subTask("Cleaning up.");
+        setApplicationContext(applicationContext);
+        progressMonitor.worked(1);
+
+        progressMonitor.done();
+        return true;
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.cdm.api.application.CdmApplicationController#init()
+     */
+    @Override
+    protected void init(){
+        configuration = (ICdmApplicationConfiguration)applicationContext.getBean("cdmApplicationRemoteConfiguration");
+               AbstractLazyInitializer.setConfiguration(this);
+               AbstractPersistentCollection.setConfiguration(this);
+
+    }
+
+}