ref #8842 claning up - removing obsolete classes and renaming
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationState.java
index 07dfa81afb40f441eaa6cd3f5b86d927c68fe52d..5d73b7ad27c83c8da39d3681222f1f4254ae15f5 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2015 EDIT
 * European Distributed Institute of Taxonomy
@@ -9,18 +8,36 @@
 */
 package eu.etaxonomy.cdm.api.application;
 
+import java.io.File;
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
-
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.osgi.util.ManifestElement;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
 import org.springframework.security.core.context.SecurityContext;
 
 import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
 import eu.etaxonomy.cdm.api.service.ICommonService;
 import eu.etaxonomy.cdm.api.service.IService;
 import eu.etaxonomy.cdm.api.service.ITestService;
+import eu.etaxonomy.cdm.api.service.longrunningService.ILongRunningTasksService;
 import eu.etaxonomy.cdm.io.service.IIOService;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
+import eu.etaxonomy.taxeditor.session.DefaultNewEntityListener;
 
 /**
  * @author cmathew
@@ -31,37 +48,44 @@ public class CdmApplicationState {
 
     private static CdmApplicationState cdmApplicationState;
 
-    private ICdmApplicationConfiguration appConfig;
+    private ICdmRepository appConfig;
 
     private ICdmDataChangeService dataChangeService;
 
+    //FIXME SecurityContextHolder.getContext()
     private SecurityContext securityContext;
 
     private static CdmServiceCacher cdmServiceCacher;
 
+    private static String cdmlibVersion = null;
+    private static String cdmlibLastModified = null;
 
     public static CdmApplicationState getInstance() {
         if(cdmApplicationState == null) {
             cdmApplicationState = new CdmApplicationState();
-            cdmServiceCacher = new CdmServiceCacher();
         }
 
         return cdmApplicationState;
     }
 
-    public void setAppConfig(ICdmApplicationConfiguration appConfig) {
+    public void setAppConfig(ICdmRepository appConfig) {
         this.appConfig = appConfig;
+        if(appConfig instanceof CdmApplicationRemoteController) {
+            CdmBase.setNewEntityListener(new DefaultNewEntityListener());
+        } else {
+            CdmBase.setNewEntityListener(null);
+        }
     }
 
-    public ICdmApplicationConfiguration getAppConfig() {
+    public ICdmRepository getAppConfig() {
         return appConfig;
     }
 
-    public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
+    public static void setCurrentAppConfig(ICdmRepository appConfig) {
         getInstance().setAppConfig(appConfig);
     }
 
-    public static ICdmApplicationConfiguration getCurrentAppConfig() {
+    public static ICdmRepository getCurrentAppConfig() {
         return getInstance().getAppConfig();
     }
 
@@ -123,11 +147,13 @@ public class CdmApplicationState {
         getInstance().setSecurityContext(null);
         cdmApplicationState = null;
         cdmServiceCacher = null;
+        cdmlibVersion = null;
+        cdmlibLastModified = null;
     }
 
 
     /**
-     * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
+     * Generic method that will scan the getters of {@link ICdmRepository} for the given service
      * interface. If a matching getter is found the according service implementation is returned by
      * invoking the getter otherwise the method returns <code>null</code>.
      *
@@ -137,9 +163,9 @@ public class CdmApplicationState {
      * @throws CdmApplicationException
      */
     public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
-        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
+        ICdmRepository configuration = getCurrentAppConfig();
 
-        Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
+        Method[] methods = ICdmRepository.class.getDeclaredMethods();
 
         T service = null;
 
@@ -169,29 +195,37 @@ public class CdmApplicationState {
      * As ICommonService is not extending IService we need a specific request here
      */
     public static ICommonService getCommonService() {
-        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
+        ICdmRepository configuration = getCurrentAppConfig();
 
         return configuration.getCommonService();
 
     }
 
     public static IIOService getIOService() {
-        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
+        ICdmRepository configuration = getCurrentAppConfig();
 
         return ((CdmApplicationRemoteController)configuration).getIOService();
 
     }
 
+    public static ILongRunningTasksService getLongRunningTasksService() {
+        ICdmRepository configuration = getCurrentAppConfig();
+
+        return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
+
+    }
+
+
 
     public static ITestService getTestService() {
-        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
+        ICdmRepository configuration = getCurrentAppConfig();
 
         return ((CdmApplicationRemoteController)configuration).getTestService();
 
     }
 
     public static ICachedCommonService getCachedCommonService() {
-        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
+        ICdmRepository configuration = getCurrentAppConfig();
 
         return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
 
@@ -201,5 +235,65 @@ public class CdmApplicationState {
         return cdmServiceCacher;
     }
 
+    public static void setCdmServiceCacher(CdmServiceCacher cacher) {
+        cdmServiceCacher = cacher;
+    }
+
+    public static void updateCdmlibManifestInfo() {
+        cdmlibVersion = null;
+        cdmlibLastModified = null;
+        String cdmlibPathPrefix = "lib/cdmlib-services-";
+        String jarSuffix = ".jar";
+        Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
+        Dictionary<String, String> headers = bundle.getHeaders();
+        String bundleClasspath = headers.get(Constants.BUNDLE_CLASSPATH);
+        try {
+            ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, bundleClasspath);
+            for (ManifestElement manifestElement : elements) {
+                String jar =  manifestElement.getValue();
+                if(jar.startsWith(cdmlibPathPrefix) && jar.endsWith(jarSuffix)) {
+                    URL fileURL = bundle.getEntry(jar);
+                    File file = null;
+                    try {
+                        String urlString = FileLocator.resolve(fileURL).toExternalForm().replace(" ", "%20");
+                        file = new File(new URI(urlString));
+                        JarFile jarFile = new JarFile(file);
+                        Manifest manifest = jarFile.getManifest();
+                        Attributes attributes = manifest.getMainAttributes();
+                        // from the OSGI spec the LastModified value is " the number of milliseconds
+                        // since midnight Jan. 1, 1970 UTC with the condition that a change must
+                        // always result in a higher value than the previous last modified time
+                        // of any bundle"
+                        cdmlibVersion = attributes.getValue("Bundle-Version");
+                        cdmlibLastModified = attributes.getValue("Bnd-LastModified");
+
+                        jarFile.close();
+                        if(cdmlibVersion == null || cdmlibLastModified == null) {
+                            throw new IllegalStateException("Invalid cdmlib manifest info");
+                        }
+                    } catch (URISyntaxException urise) {
+                        throw new IllegalStateException(urise);
+                    } catch (IOException ioe) {
+                        throw new IllegalStateException(ioe);
+                    }
+                }
+            }
+        } catch (BundleException e) {
+            throw new IllegalStateException(e);
+        }
+    }
 
+    public static String getCdmlibVersion() {
+        if(cdmlibVersion == null) {
+            updateCdmlibManifestInfo();
+        }
+        return cdmlibVersion;
+    }
+
+    public static String getCdmlibLastModified() {
+        if(cdmlibLastModified == null) {
+            updateCdmlibManifestInfo();
+        }
+        return cdmlibLastModified;
+    }
 }