Project

General

Profile

Download (6.37 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.cache;
2

    
3
import java.io.File;
4
import java.util.UUID;
5

    
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.stereotype.Component;
8

    
9
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
10
import eu.etaxonomy.cdm.api.config.EhCacheConfiguration;
11
import eu.etaxonomy.cdm.cache.CacheLoader;
12
import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
13
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
14
import eu.etaxonomy.cdm.config.ConfigFileUtil;
15
import eu.etaxonomy.cdm.model.common.CdmBase;
16
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
17
import eu.etaxonomy.cdm.model.term.TermBase;
18
import eu.etaxonomy.taxeditor.service.RemoteInvocationTermCacher;
19
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
20
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
21
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
22
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
23
import net.sf.ehcache.config.CacheConfiguration;
24
import net.sf.ehcache.config.DiskStoreConfiguration;
25
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
26

    
27
/**
28
 * Class which uses CDM services to cache cdm entities
29
 *
30
 * FIXME: Currently only handles term entities. It would be
31
 *        interesting to have a generic method which finds the
32
 *        correct service to load / cache the entity.
33
 *
34
 * @author cmathew
35
 */
36
@Component //FIXME This indicates that the CdmServiceCacher is initialized as Spring Component but it seems only to be instantiated directly
37
public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
38

    
39

    
40
    private ICdmEntitySessionManager cdmEntitySessionManager;
41

    
42
    private CdmTransientEntityCacher cdmTransientEntityCacher;
43

    
44
    private CacheLoader cacheLoader;
45

    
46
    @Autowired
47
    ConfigFileUtil configFileUtil = null;
48

    
49
    @Override
50
    protected void setup() {
51

    
52
        setUpCacheManager();
53

    
54
        DefinedTermBase.setCacher(this);
55
        CdmTransientEntityCacher.setPermanentCacher(this);
56
        //TermServiceRequestExecutor.setDefaultCacher(this);
57
        RemoteInvocationTermCacher.setDefaultCacher(this);
58

    
59
        cacheLoader = new CacheLoader(this);
60
    }
61

    
62
    private void setUpCacheManager() {
63

    
64
        EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
65

    
66
        DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
67
        File ehcacheFolder = null;
68
        if(configFileUtil != null){
69
            try {
70
                ehcacheFolder = configFileUtil.getCdmHomeSubDir("taxeditor-ehcache");
71
            } catch (Exception e){
72
                logger.warn("Cannot determine CdmHomeSubDir for ./taxeditor-ehcache, will use fallback method.", e);
73
            }
74
        }
75
        if(ehcacheFolder == null){
76
            ehcacheFolder = ConfigFileUtil.getCdmHomeSubDirFallback("taxeditor-ehcache");
77
        }
78

    
79
        // FIXME use subfolder per taxeditor version to allow running multiple installations in parallel
80
        // String taxEditorVersion = ..;
81
        // File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
82
        diskStoreConfiguration.setPath(ehcacheFolder.getAbsolutePath());
83

    
84
        cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
85
        addCacheManager(cacheConfig.cacheManager());
86

    
87
    }
88

    
89

    
90
    @Override
91
    protected CacheConfiguration getDefaultCacheConfiguration() {
92
        // For a better understanding on how to size caches, refer to
93
        // http://ehcache.org/documentation/configuration/cache-size
94

    
95
        SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
96
        sizeOfConfig.setMaxDepth(100);
97
        sizeOfConfig.setMaxDepthExceededBehavior("abort");
98

    
99
        return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
100
        	.eternal(true)
101
        	.statistics(true)
102
        	.sizeOfPolicy(sizeOfConfig)
103
        	.overflowToOffHeap(false);
104

    
105
    }
106

    
107
    @Override
108
    protected CdmBase findByUuid(UUID uuid) {
109
        CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
110
        return load(term);
111
    }
112

    
113
    @Override
114
    public boolean isCachable(CdmBase cdmEntity) {
115
     //   if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
116
    	   if(cdmEntity != null && cdmEntity instanceof TermBase) {
117
            return true;
118
        }
119
        return false;
120
    }
121

    
122

    
123
    public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
124
        this.cdmEntitySessionManager = cdmEntitySessionManager;
125
        if(cdmEntitySessionManager != null) {
126
            cdmEntitySessionManager.addSessionObserver(this);
127
        }
128
    }
129

    
130

    
131
    public CdmTransientEntityCacher getCurrentCacher() {
132
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
133
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
134
            return ((CdmEntitySession) cdmEntitySession).getCacher();
135
        }
136
        return null;
137
    }
138

    
139
    @Override
140
    public <T extends CdmBase> T  getFromCache(T cdmBase) {
141
        T cachedCdmEntity = null;
142
        // first we check in the active session cache if the
143
        // entity has been loaded there
144
        // FIXME:Remoting do we really need the cdmTransientEntityCacher
145
        // here. Is it not guarenteed that all every entity which 'isCachable'
146
        // by this cacher is cached only in this cacher ?
147
        if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
148
            CdmEntityCacheKey<T> key = CdmTransientEntityCacher.generateKey(cdmBase);
149
            cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
150

    
151
        }
152
        if(cachedCdmEntity == null) {
153
            cachedCdmEntity = super.getFromCache(cdmBase);
154
        }
155
        return cachedCdmEntity;
156
    }
157

    
158
    @Override
159
    public <T extends CdmBase> T load(T cdmEntity) {
160
        T cachedCdmEntity = (T)getFromCache(cdmEntity.getUuid());
161

    
162
        if(isCachable(cdmEntity) && cachedCdmEntity == null) {
163
            cachedCdmEntity =  cacheLoader.load(cdmEntity, false, true);
164
        }
165
        return cachedCdmEntity;
166
    }
167

    
168
    @Override
169
    public void changed() {
170
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
171
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
172
            this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
173
        } else {
174
            this.cdmTransientEntityCacher = null;
175
        }
176
    }
177
}
    (1-1/1)