test
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationState.java
index cf47b3cfce64fce2bf7da00713e2f6b4bbbac9e3..b8754d1cf69b3679983fd05926b5963bb4ed561a 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2015 EDIT
 * European Distributed Institute of Taxonomy
@@ -9,27 +8,57 @@
 */
 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 eu.etaxonomy.cdm.common.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
  * @date 17 Jun 2015
- *
  */
 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();
@@ -37,32 +66,30 @@ public class CdmApplicationState {
         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();
     }
 
-    /**
-     * @return the dataChangeService
-     */
     public ICdmDataChangeService getDataChangeService() {
         return dataChangeService;
     }
-
-    /**
-     * @param dataChangeService the dataChangeService to set
-     */
     public void setDataChangeService(ICdmDataChangeService dataChangeService) {
         this.dataChangeService = dataChangeService;
     }
@@ -75,13 +102,32 @@ public class CdmApplicationState {
         getInstance().setDataChangeService(dataChangeService);
     }
 
+    public SecurityContext getSecurityContext() {
+        return securityContext;
+    }
+    public void setSecurityContext(SecurityContext securityContext) {
+        this.securityContext = securityContext;
+    }
+
+    public static SecurityContext getCurrentSecurityContext() {
+        return getInstance().getSecurityContext();
+    }
+    public static void setCurrentSecurityContext(SecurityContext securityContext) {
+        getInstance().setSecurityContext(securityContext);
+    }
+
     public static void dispose() {
-        getInstance().setCurrentDataChangeService(null);
+        setCurrentDataChangeService(null);
         getInstance().setAppConfig(null);
+        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>.
      *
@@ -91,30 +137,24 @@ 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();
-
-        T service = null;
+        Method[] methods = ICdmRepository.class.getDeclaredMethods();
 
         for (Method method : methods) {
             Type type = method.getGenericReturnType();
 
             if (type.equals(serviceClass)) {
                 try {
-                    service = (T) method.invoke(configuration, null);
-                    break;
-                } catch (IllegalArgumentException iae) {
-                    throw new CdmApplicationException(iae);
-                } catch (IllegalAccessException iae) {
+                    @SuppressWarnings("unchecked")
+                    T service = (T) method.invoke(configuration);
+                    return service;
+                } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException iae) {
                     throw new CdmApplicationException(iae);
-                } catch (InvocationTargetException ite) {
-                    throw new CdmApplicationException(ite);
                 }
             }
         }
-
-        return service;
+        return null;
     }
 
     /**
@@ -122,17 +162,100 @@ 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() {
+        ICdmRepository configuration = getCurrentAppConfig();
 
+        return ((CdmApplicationRemoteController)configuration).getIOService();
     }
 
-    public static ITestService getTestService() {
-        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
+    public static ILongRunningTasksService getLongRunningTasksService() {
+        ICdmRepository configuration = getCurrentAppConfig();
+
+        return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
+    }
 
+    public static ITestService getTestService() {
+        ICdmRepository configuration = getCurrentAppConfig();
         return ((CdmApplicationRemoteController)configuration).getTestService();
+    }
+
+    public static ICachedCommonService getCachedCommonService() {
+        ICdmRepository configuration = getCurrentAppConfig();
+        return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
+    }
+
+    public static CdmServiceCacher getCdmServiceCacher() {
+        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).getJavaUri());
+                        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 (IllegalArgumentException iae) {
+                       String message = iae.getMessage().concat("uri" + jar);
+                       throw new IllegalStateException(message);
+                       }
+                }
+            }
+        } 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;
+    }
 }