cleanup
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / cache / CdmServiceCacher.java
index f903aaceef30b52cd28d4ee01dca82cdac57ec94..cbfe7d630466189744ac0f8a7d6aa00e46212d8f 100644 (file)
@@ -1,20 +1,29 @@
+/**
+ * Copyright (C) 2015 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.cache;
 
 import java.io.File;
 import java.util.UUID;
 
-import org.springframework.stereotype.Component;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.config.EhCacheConfiguration;
 import eu.etaxonomy.cdm.cache.CacheLoader;
 import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
 import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
-import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.config.ConfigFileUtil;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.term.Representation;
 import eu.etaxonomy.cdm.model.term.TermBase;
-import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
+import eu.etaxonomy.taxeditor.service.RemoteInvocationTermCacher;
 import eu.etaxonomy.taxeditor.session.CdmEntitySession;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
@@ -32,9 +41,7 @@ import net.sf.ehcache.config.SizeOfPolicyConfiguration;
  *
  * @author cmathew
  */
-@Component //FIXME This indicates that the CdmServiceCacher is initialized as Spring Component but it seems only to be instantiated directly
-public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
-
+public class CdmServiceCacher extends CdmCacherBase implements ICdmEntitySessionManagerObserver {
 
     private ICdmEntitySessionManager cdmEntitySessionManager;
 
@@ -42,6 +49,9 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
 
     private CacheLoader cacheLoader;
 
+    @Autowired
+    private ConfigFileUtil configFileUtil = null;
+
     @Override
     protected void setup() {
 
@@ -49,7 +59,8 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
 
         DefinedTermBase.setCacher(this);
         CdmTransientEntityCacher.setPermanentCacher(this);
-        TermServiceRequestExecutor.setDefaultCacher(this);
+        //TermServiceRequestExecutor.setDefaultCacher(this);
+        RemoteInvocationTermCacher.setDefaultCacher(this);
 
         cacheLoader = new CacheLoader(this);
     }
@@ -59,7 +70,18 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
         EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
 
         DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
-        File ehcacheFolder = CdmUtils.getCdmHomeSubDir("taxeditor-ehcache");
+        File ehcacheFolder = null;
+        if(configFileUtil != null){
+            try {
+                ehcacheFolder = configFileUtil.getCdmHomeSubDir("taxeditor-ehcache");
+            } catch (Exception e){
+                logger.warn("Cannot determine CdmHomeSubDir for ./taxeditor-ehcache, will use fallback method.", e);
+            }
+        }
+        if(ehcacheFolder == null){
+            ehcacheFolder = ConfigFileUtil.getCdmHomeSubDirFallback("taxeditor-ehcache");
+        }
+
         // FIXME use subfolder per taxeditor version to allow running multiple installations in parallel
         // String taxEditorVersion = ..;
         // File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
@@ -67,10 +89,8 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
 
         cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
         addCacheManager(cacheConfig.cacheManager());
-
     }
 
-
     @Override
     protected CacheConfiguration getDefaultCacheConfiguration() {
         // For a better understanding on how to size caches, refer to
@@ -85,7 +105,6 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
                .statistics(true)
                .sizeOfPolicy(sizeOfConfig)
                .overflowToOffHeap(false);
-
     }
 
     @Override
@@ -96,14 +115,15 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
 
     @Override
     public boolean isCachable(CdmBase cdmEntity) {
-     //   if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
-          if(cdmEntity != null && cdmEntity instanceof TermBase) {
+       if(cdmEntity == null){
+           return false;
+       }else if (cdmEntity instanceof TermBase || cdmEntity instanceof Representation){
             return true;
+        }else{
+            return false;
         }
-        return false;
     }
 
-
     public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
         this.cdmEntitySessionManager = cdmEntitySessionManager;
         if(cdmEntitySessionManager != null) {
@@ -111,7 +131,6 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
         }
     }
 
-
     public CdmTransientEntityCacher getCurrentCacher() {
         ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
         if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
@@ -126,12 +145,11 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
         // first we check in the active session cache if the
         // entity has been loaded there
         // FIXME:Remoting do we really need the cdmTransientEntityCacher
-        // here. Is it not guarenteed that all every entity which 'isCachable'
+        // here. Is it not guaranteed that all every entity which 'isCachable'
         // by this cacher is cached only in this cacher ?
         if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
             CdmEntityCacheKey<T> key = CdmTransientEntityCacher.generateKey(cdmBase);
             cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
-
         }
         if(cachedCdmEntity == null) {
             cachedCdmEntity = super.getFromCache(cdmBase);
@@ -158,4 +176,4 @@ public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionMana
             this.cdmTransientEntityCacher = null;
         }
     }
-}
+}
\ No newline at end of file