Merge branch 'hotfix/4.5.1'
[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.util.UUID;
4
5 import net.sf.ehcache.config.CacheConfiguration;
6 import net.sf.ehcache.config.SizeOfPolicyConfiguration;
7
8 import org.springframework.stereotype.Component;
9
10 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
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
23 /**
24 * Class which uses CDM services to cache cdm entities
25 *
26 * FIXME: Currently only handles term entities. It would be
27 * interesting to have a generic method which finds the
28 * correct service to load / cache the entity.
29 *
30 * @author cmathew
31 *
32 * @param <T>
33 */
34 @Component
35 public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
36
37
38
39 private ICdmEntitySessionManager cdmEntitySessionManager;
40
41 private CdmTransientEntityCacher cdmTransientEntityCacher;
42
43 private CacheLoader cacheLoader;
44
45 @Override
46 protected void setup() {
47 DefinedTermBase.setCacher(this);
48 CdmTransientEntityCacher.setDefaultCacher(this);
49 TermServiceRequestExecutor.setDefaultCacher(this);
50
51 cacheLoader = new CacheLoader(this);
52 }
53
54
55 @Override
56 protected CacheConfiguration getDefaultCacheConfiguration() {
57 // For a better understanding on how to size caches, refer to
58 // http://ehcache.org/documentation/configuration/cache-size
59
60 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
61 sizeOfConfig.setMaxDepth(100);
62 sizeOfConfig.setMaxDepthExceededBehavior("abort");
63
64 return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
65 .eternal(true)
66 .statistics(true)
67 .sizeOfPolicy(sizeOfConfig)
68 .overflowToOffHeap(false);
69
70 }
71
72 @Override
73 protected CdmBase findByUuid(UUID uuid) {
74 CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
75 return load(term);
76 }
77
78 @Override
79 public boolean isCachable(CdmBase cdmEntity) {
80 // if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
81 if(cdmEntity != null && cdmEntity instanceof TermBase) {
82 return true;
83 }
84 return false;
85 }
86
87
88 public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
89 this.cdmEntitySessionManager = cdmEntitySessionManager;
90 if(cdmEntitySessionManager != null) {
91 cdmEntitySessionManager.addSessionObserver(this);
92 }
93 }
94
95
96 public CdmTransientEntityCacher getCurrentCacher() {
97 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
98 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
99 return ((CdmEntitySession) cdmEntitySession).getCacher();
100 }
101 return null;
102 }
103
104 @Override
105 public CdmBase getFromCache(CdmBase cdmBase) {
106 CdmBase cachedCdmEntity = null;
107 // first we check in the active session cache if the
108 // entity has been loaded there
109 // FIXME:Remoting do we really need the cdmTransientEntityCacher
110 // here. Is it not guarenteed that all every entity which 'isCachable'
111 // by this cacher is cached only in this cacher ?
112 if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
113 CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
114 cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
115
116 }
117 if(cachedCdmEntity == null) {
118 cachedCdmEntity = super.getFromCache(cdmBase);
119 }
120 return cachedCdmEntity;
121 }
122
123
124 /* (non-Javadoc)
125 * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
126 */
127 @Override
128 public CdmBase load(CdmBase cdmEntity) {
129 CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
130
131 if(isCachable(cdmEntity) && cachedCdmEntity == null) {
132 cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
133 }
134 return cachedCdmEntity;
135 }
136
137 /* (non-Javadoc)
138 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver#changed()
139 */
140 @Override
141 public void changed() {
142 ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
143 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
144 this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
145 } else {
146 this.cdmTransientEntityCacher = null;
147 }
148 }
149 }