adapt taxeditor cache to cdmlib-cache
[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.cache.CacheLoader;
11 import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
12 import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
13 import eu.etaxonomy.cdm.common.CdmUtils;
14 import eu.etaxonomy.cdm.model.common.CdmBase;
15 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
16 import eu.etaxonomy.cdm.model.common.TermBase;
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 /**
65 *
66 */
67 private void setUpCacheManager() {
68
69 EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
70
71 DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
72 File ehcacheFolder = CdmUtils.getCdmHomeSubDir("taxeditor-ehcache");
73 // FIXME use subfolder per taxeditor version to allow running multiple installations in parallel
74 // String taxEditorVersion = ..;
75 // File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
76 diskStoreConfiguration.setPath(ehcacheFolder.getAbsolutePath());
77
78 cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
79 addCacheManager(cacheConfig.cacheManager());
80
81 }
82
83
84 @Override
85 protected CacheConfiguration getDefaultCacheConfiguration() {
86 // For a better understanding on how to size caches, refer to
87 // http://ehcache.org/documentation/configuration/cache-size
88
89 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
90 sizeOfConfig.setMaxDepth(100);
91 sizeOfConfig.setMaxDepthExceededBehavior("abort");
92
93 return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
94 .eternal(true)
95 .statistics(true)
96 .sizeOfPolicy(sizeOfConfig)
97 .overflowToOffHeap(false);
98
99 }
100
101 @Override
102 protected CdmBase findByUuid(UUID uuid) {
103 CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
104 return load(term);
105 }
106
107 @Override
108 public boolean isCachable(CdmBase cdmEntity) {
109 // if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
110 if(cdmEntity != null && cdmEntity instanceof TermBase) {
111 return true;
112 }
113 return false;
114 }
115
116
117 public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
118 this.cdmEntitySessionManager = cdmEntitySessionManager;
119 if(cdmEntitySessionManager != null) {
120 cdmEntitySessionManager.addSessionObserver(this);
121 }
122 }
123
124
125 public CdmTransientEntityCacher getCurrentCacher() {
126 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
127 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
128 return ((CdmEntitySession) cdmEntitySession).getCacher();
129 }
130 return null;
131 }
132
133 @Override
134 public CdmBase getFromCache(CdmBase cdmBase) {
135 CdmBase cachedCdmEntity = null;
136 // first we check in the active session cache if the
137 // entity has been loaded there
138 // FIXME:Remoting do we really need the cdmTransientEntityCacher
139 // here. Is it not guarenteed that all every entity which 'isCachable'
140 // by this cacher is cached only in this cacher ?
141 if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
142 CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
143 cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
144
145 }
146 if(cachedCdmEntity == null) {
147 cachedCdmEntity = super.getFromCache(cdmBase);
148 }
149 return cachedCdmEntity;
150 }
151
152 @Override
153 public CdmBase load(CdmBase cdmEntity) {
154 CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
155
156 if(isCachable(cdmEntity) && cachedCdmEntity == null) {
157 cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
158 }
159 return cachedCdmEntity;
160 }
161
162 @Override
163 public void changed() {
164 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
165 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
166 this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
167 } else {
168 this.cdmTransientEntityCacher = null;
169 }
170 }
171 }