Project

General

Profile

Download (6.44 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.Representation;
18
import eu.etaxonomy.cdm.model.term.TermBase;
19
import eu.etaxonomy.taxeditor.service.RemoteInvocationTermCacher;
20
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
21
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
22
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
23
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
24
import net.sf.ehcache.config.CacheConfiguration;
25
import net.sf.ehcache.config.DiskStoreConfiguration;
26
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
27

    
28
/**
29
 * Class which uses CDM services to cache cdm entities
30
 *
31
 * FIXME: Currently only handles term entities. It would be
32
 *        interesting to have a generic method which finds the
33
 *        correct service to load / cache the entity.
34
 *
35
 * @author cmathew
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 CdmCacherBase implements ICdmEntitySessionManagerObserver {
39

    
40
    private ICdmEntitySessionManager cdmEntitySessionManager;
41

    
42
    private CdmTransientEntityCacher cdmTransientEntityCacher;
43

    
44
    private CacheLoader cacheLoader;
45

    
46
    @Autowired
47
    private 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
    @Override
89
    protected CacheConfiguration getDefaultCacheConfiguration() {
90
        // For a better understanding on how to size caches, refer to
91
        // http://ehcache.org/documentation/configuration/cache-size
92

    
93
        SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
94
        sizeOfConfig.setMaxDepth(100);
95
        sizeOfConfig.setMaxDepthExceededBehavior("abort");
96

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

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

    
110
    @Override
111
    public boolean isCachable(CdmBase cdmEntity) {
112
     	if(cdmEntity == null){
113
     	    return false;
114
     	}else if (cdmEntity instanceof TermBase || cdmEntity instanceof Representation){
115
            return true;
116
        }else{
117
            return false;
118
        }
119
    }
120

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

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

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

    
154
    @Override
155
    public <T extends CdmBase> T load(T cdmEntity) {
156
        T cachedCdmEntity = (T)getFromCache(cdmEntity.getUuid());
157

    
158
        if(isCachable(cdmEntity) && cachedCdmEntity == null) {
159
            cachedCdmEntity =  cacheLoader.load(cdmEntity, false, true);
160
        }
161
        return cachedCdmEntity;
162
    }
163

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