1 package eu
.etaxonomy
.cdm
.api
.cache
;
6 import org
.springframework
.stereotype
.Component
;
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
;
27 * Class which uses CDM services to cache cdm entities
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.
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
{
41 private ICdmEntitySessionManager cdmEntitySessionManager
;
43 private CdmTransientEntityCacher cdmTransientEntityCacher
;
45 private CacheLoader cacheLoader
;
48 protected void setup() {
52 DefinedTermBase
.setCacher(this);
53 CdmTransientEntityCacher
.setDefaultCacher(this);
54 TermServiceRequestExecutor
.setDefaultCacher(this);
56 cacheLoader
= new CacheLoader(this);
64 private void setUpCacheManager() {
66 EhCacheConfiguration cacheConfig
= new EhCacheConfiguration();
68 DiskStoreConfiguration diskStoreConfiguration
= new DiskStoreConfiguration();
69 File ehcacheFolder
= CdmUtils
.getCdmSubDir("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());
75 cacheConfig
.setDiskStoreConfiguration(diskStoreConfiguration
);
76 addCacheManager(cacheConfig
.cacheManager());
82 protected CacheConfiguration
getDefaultCacheConfiguration() {
83 // For a better understanding on how to size caches, refer to
84 // http://ehcache.org/documentation/configuration/cache-size
86 SizeOfPolicyConfiguration sizeOfConfig
= new SizeOfPolicyConfiguration();
87 sizeOfConfig
.setMaxDepth(100);
88 sizeOfConfig
.setMaxDepthExceededBehavior("abort");
90 return new CacheConfiguration(DEFAULT_CACHE_NAME
, 0)
93 .sizeOfPolicy(sizeOfConfig
)
94 .overflowToOffHeap(false);
99 protected CdmBase
findByUuid(UUID uuid
) {
100 CdmBase term
= CdmApplicationState
.getCurrentAppConfig().getTermService().findWithoutFlush(uuid
);
105 public boolean isCachable(CdmBase cdmEntity
) {
106 // if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
107 if(cdmEntity
!= null && cdmEntity
instanceof TermBase
) {
114 public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager
) {
115 this.cdmEntitySessionManager
= cdmEntitySessionManager
;
116 if(cdmEntitySessionManager
!= null) {
117 cdmEntitySessionManager
.addSessionObserver(this);
122 public CdmTransientEntityCacher
getCurrentCacher() {
123 ICdmEntitySession cdmEntitySession
= cdmEntitySessionManager
.getActiveSession();
124 if(cdmEntitySession
!= null && cdmEntitySession
instanceof CdmEntitySession
) {
125 return ((CdmEntitySession
) cdmEntitySession
).getCacher();
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
);
143 if(cachedCdmEntity
== null) {
144 cachedCdmEntity
= super.getFromCache(cdmBase
);
146 return cachedCdmEntity
;
151 * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
154 public CdmBase
load(CdmBase cdmEntity
) {
155 CdmBase cachedCdmEntity
= getFromCache(cdmEntity
.getUuid());
157 if(isCachable(cdmEntity
) && cachedCdmEntity
== null) {
158 cachedCdmEntity
= cacheLoader
.load(cdmEntity
, false, true);
160 return cachedCdmEntity
;
164 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver#changed()
167 public void changed() {
168 ICdmEntitySession cdmEntitySession
= cdmEntitySessionManager
.getActiveSession();
169 if(cdmEntitySession
!= null && cdmEntitySession
instanceof CdmEntitySession
) {
170 this.cdmTransientEntityCacher
= ((CdmEntitySession
) cdmEntitySession
).getCacher();
172 this.cdmTransientEntityCacher
= null;