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 a1982c5c8b5d38313e0fd953ee90bffc835fe0c7..5d73b7ad27c83c8da39d3681222f1f4254ae15f5 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2015 EDIT
 * European Distributed Institute of Taxonomy
@@ -9,6 +8,37 @@
 */
 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
  * @date 17 Jun 2015
@@ -18,29 +48,252 @@ 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();
         }
+
         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;
+    }
+
+    public static ICdmDataChangeService getCurrentDataChangeService() {
+        return getInstance().getDataChangeService();
+    }
+
+    public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
+        getInstance().setDataChangeService(dataChangeService);
+    }
+
+
+
+    /**
+     * @return the securityContext
+     */
+    public SecurityContext getSecurityContext() {
+        return securityContext;
+    }
+
+    /**
+     * @param securityContext the securityContext to set
+     */
+    public void setSecurityContext(SecurityContext securityContext) {
+        this.securityContext = securityContext;
+    }
+
+    /**
+     * @return the securityContext
+     */
+    public static SecurityContext getCurrentSecurityContext() {
+        return getInstance().getSecurityContext();
+    }
+
+    /**
+     * @param securityContext the securityContext to set
+     */
+    public static void setCurrentSecurityContext(SecurityContext securityContext) {
+        getInstance().setSecurityContext(securityContext);
+    }
+
+    public static void dispose() {
+        getInstance().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 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>.
+     *
+     * @param <T>
+     * @param serviceClass
+     * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
+     * @throws CdmApplicationException
+     */
+    public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
+        ICdmRepository configuration = getCurrentAppConfig();
+
+        Method[] methods = ICdmRepository.class.getDeclaredMethods();
+
+        T service = null;
+
+        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) {
+                    throw new CdmApplicationException(iae);
+                } catch (InvocationTargetException ite) {
+                    throw new CdmApplicationException(ite);
+                }
+            }
+        }
+
+        return service;
+    }
+
+
+    /**
+     * @see #getService(Class)
+     * As ICommonService is not extending IService we need a specific request here
+     */
+    public static ICommonService getCommonService() {
+        ICdmRepository configuration = getCurrentAppConfig();
+
+        return configuration.getCommonService();
+
+    }
+
+    public static IIOService getIOService() {
+        ICdmRepository configuration = getCurrentAppConfig();
+
+        return ((CdmApplicationRemoteController)configuration).getIOService();
+
+    }
+
+    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));
+                        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;
+    }
 }