adapting to chages in CdmUtils
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / cache / CdmServiceCacher.java
1 package eu.etaxonomy.cdm.api.cache;
2
3 import java.io.File;
4 import java.util.UUID;
5
6 import org.springframework.stereotype.Component;
7
8 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
9 import eu.etaxonomy.cdm.api.config.EhCacheConfiguration;
10 import eu.etaxonomy.cdm.common.CdmUtils;
11 import eu.etaxonomy.cdm.model.common.CdmBase;
12 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
13 import eu.etaxonomy.cdm.model.common.TermBase;
14 import eu.etaxonomy.taxeditor.remoting.cache.CacheLoader;
15 import eu.etaxonomy.taxeditor.remoting.cache.CdmEntityCacheKey;
16 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
17 import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
18 import eu.etaxonomy.taxeditor.session.CdmEntitySession;
19 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
20 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
21 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
22 import net.sf.ehcache.config.CacheConfiguration;
23 import net.sf.ehcache.config.DiskStoreConfiguration;
24 import net.sf.ehcache.config.SizeOfPolicyConfiguration;
25
26 /**
27 * Class which uses CDM services to cache cdm entities
28 *
29 * FIXME: Currently only handles term entities. It would be
30 * interesting to have a generic method which finds the
31 * correct service to load / cache the entity.
32 *
33 * @author cmathew
34 *
35 * @param <T>
36 */
37 @Component //FIXME This indicates that the CdmServiceCacher is initialized as Spring Component but it seems only to be instantiated directly
38 public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
39
40
41 private ICdmEntitySessionManager cdmEntitySessionManager;
42
43 private CdmTransientEntityCacher cdmTransientEntityCacher;
44
45 private CacheLoader cacheLoader;
46
47 @Override
48 protected void setup() {
49
50 setUpCacheManager();
51
52 DefinedTermBase.setCacher(this);
53 CdmTransientEntityCacher.setDefaultCacher(this);
54 TermServiceRequestExecutor.setDefaultCacher(this);
55
56 cacheLoader = new CacheLoader(this);
57
58 }
59
60
61 /**
62 *
63 */
64 private void setUpCacheManager() {
65
66 EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
67
68 DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
69 File ehcacheFolder = CdmUtils.getCdmHomeSubDir("taxeditor-ehcache");
70 // FIXME use subfolder per taxeditor version to allow running multiple installations in parallel
71 // String taxEditorVersion = ..;
72 // File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
73 diskStoreConfiguration.setPath(ehcacheFolder.getAbsolutePath());
74
75 cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
76 addCacheManager(cacheConfig.cacheManager());
77
78 }
79
80
81 @Override
82 protected CacheConfiguration getDefaultCacheConfiguration() {
83 // For a better understanding on how to size caches, refer to
84 // http://ehcache.org/documentation/configuration/cache-size
85
86 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
87 sizeOfConfig.setMaxDepth(100);
88 sizeOfConfig.setMaxDepthExceededBehavior("abort");
89
90 return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
91 .eternal(true)
92 .statistics(true)
93 .sizeOfPolicy(sizeOfConfig)
94 .overflowToOffHeap(false);
95
96 }
97
98 @Override
99 protected CdmBase findByUuid(UUID uuid) {
100 CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
101 return load(term);
102 }
103
104 @Override
105 public boolean isCachable(CdmBase cdmEntity) {
106 // if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
107 if(cdmEntity != null && cdmEntity instanceof TermBase) {
108 return true;
109 }
110 return false;
111 }
112
113
114 public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
115 this.cdmEntitySessionManager = cdmEntitySessionManager;
116 if(cdmEntitySessionManager != null) {
117 cdmEntitySessionManager.addSessionObserver(this);
118 }
119 }
120
121
122 public CdmTransientEntityCacher getCurrentCacher() {
123 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
124 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
125 return ((CdmEntitySession) cdmEntitySession).getCacher();
126 }
127 return null;
128 }
129
130 @Override
131 public CdmBase getFromCache(CdmBase cdmBase) {
132 CdmBase cachedCdmEntity = null;
133 // first we check in the active session cache if the
134 // entity has been loaded there
135 // FIXME:Remoting do we really need the cdmTransientEntityCacher
136 // here. Is it not guarenteed that all every entity which 'isCachable'
137 // by this cacher is cached only in this cacher ?
138 if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
139 CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
140 cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
141
142 }
143 if(cachedCdmEntity == null) {
144 cachedCdmEntity = super.getFromCache(cdmBase);
145 }
146 return cachedCdmEntity;
147 }
148
149 @Override
150 public CdmBase load(CdmBase cdmEntity) {
151 CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
152
153 if(isCachable(cdmEntity) && cachedCdmEntity == null) {
154 cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
155 }
156 return cachedCdmEntity;
157 }
158
159 @Override
160 public void changed() {
161 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
162 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
163 this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
164 } else {
165 this.cdmTransientEntityCacher = null;
166 }
167 }
168 }