Project

General

Profile

Download (6.83 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.cdm.api.cache;
10

    
11
import java.io.File;
12
import java.util.UUID;
13

    
14
import org.springframework.beans.factory.annotation.Autowired;
15

    
16
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
17
import eu.etaxonomy.cdm.api.config.EhCacheConfiguration;
18
import eu.etaxonomy.cdm.cache.CacheLoader;
19
import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
20
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
21
import eu.etaxonomy.cdm.config.ConfigFileUtil;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
24
import eu.etaxonomy.cdm.model.term.Representation;
25
import eu.etaxonomy.cdm.model.term.TermBase;
26
import eu.etaxonomy.taxeditor.service.RemoteInvocationTermCacher;
27
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
28
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
29
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
30
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
31
import net.sf.ehcache.CacheManager;
32
import net.sf.ehcache.config.CacheConfiguration;
33
import net.sf.ehcache.config.DiskStoreConfiguration;
34
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
35

    
36
/**
37
 * Class which uses CDM services to cache cdm entities
38
 *
39
 * FIXME: Currently only handles term entities. It would be
40
 *        interesting to have a generic method which finds the
41
 *        correct service to load / cache the entity.
42
 * TODO by AM: compare with {@link CdmTermCacher} and merge if possible
43
 *
44
 * @author cmathew
45
 */
46
public class CdmServiceCachingProxy
47
        extends CdmCacherBase
48
        implements ICdmEntitySessionManagerObserver {
49

    
50
    private ICdmEntitySessionManager cdmEntitySessionManager;
51

    
52
    private CdmTransientEntityCacher cdmTransientEntityCacher;
53

    
54
    private CacheLoader cacheLoader;
55

    
56
    @Autowired
57
    private ConfigFileUtil configFileUtil = null;
58

    
59
    @Override
60
    protected void setup() {
61

    
62
        setUpCacheManager();
63

    
64
        DefinedTermBase.setCacher(this);
65
        CdmTransientEntityCacher.setPermanentCacher(this);
66
        //TermServiceRequestExecutor.setDefaultCacher(this);
67
        RemoteInvocationTermCacher.setDefaultCacher(this);
68

    
69
        cacheLoader = new CacheLoader(this);
70
    }
71

    
72
    private void setUpCacheManager() {
73

    
74
        DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
75
        File ehcacheFolder = null;
76
        if(configFileUtil != null){
77
            try {
78
                ehcacheFolder = configFileUtil.getCdmHomeSubDir("taxeditor-ehcache");
79
            } catch (Exception e){
80
                logger.warn("Cannot determine CdmHomeSubDir for ./taxeditor-ehcache, will use fallback method.", e);
81
            }
82
        }
83
        if(ehcacheFolder == null){
84
            ehcacheFolder = ConfigFileUtil.getCdmHomeSubDirFallback("taxeditor-ehcache");
85
        }
86

    
87
        // FIXME use subfolder per taxeditor version to allow running multiple
88
        //   installations in parallel
89
        // String taxEditorVersion = ..;
90
        // File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
91
        diskStoreConfiguration.setPath(ehcacheFolder.getAbsolutePath());
92

    
93
        EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
94
        cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
95
        CacheManager cacheManager = cacheConfig.cacheManager();
96
        setCacheManager(cacheManager);
97
    }
98

    
99
    @Override
100
    protected CacheConfiguration getDefaultCacheConfiguration() {
101
        // For a better understanding on how to size caches, refer to
102
        // http://ehcache.org/documentation/configuration/cache-size
103

    
104
        SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
105
        sizeOfConfig.setMaxDepth(100);
106
        sizeOfConfig.setMaxDepthExceededBehavior("abort");
107

    
108
        return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
109
        	.eternal(true)
110
        	.statistics(true)
111
        	.sizeOfPolicy(sizeOfConfig)
112
        	.overflowToOffHeap(false);
113
    }
114

    
115
    @Override
116
    protected CdmBase findByUuid(UUID uuid) {
117
        CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
118
        return load(term);
119
    }
120

    
121
    /**
122
     * <code>true</code> if the parameter is a term or a representation
123
     */
124
    @Override
125
    public boolean isCachable(CdmBase cdmEntity) {
126
     	if(cdmEntity == null){
127
     	    return false;
128
     	}else if (cdmEntity instanceof TermBase || cdmEntity instanceof Representation){
129
            return true;
130
        }else{
131
            return false;
132
        }
133
    }
134

    
135
    public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
136
        this.cdmEntitySessionManager = cdmEntitySessionManager;
137
        if(cdmEntitySessionManager != null) {
138
            cdmEntitySessionManager.addSessionObserver(this);
139
        }
140
    }
141

    
142
    public CdmTransientEntityCacher getCurrentCacher() {
143
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
144
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
145
            return ((CdmEntitySession) cdmEntitySession).getCacher();
146
        }
147
        return null;
148
    }
149

    
150
    @Override
151
    public <T extends CdmBase> T  getFromCache(T cdmBase) {
152
        T cachedCdmEntity = null;
153
        // first we check in the active session cache if the
154
        // entity has been loaded there
155
        // FIXME:Remoting do we really need the cdmTransientEntityCacher
156
        // here. Is it not guaranteed that every entity which 'isCachable'
157
        // by this cacher is cached only in this cacher ?
158
        if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
159
            CdmEntityCacheKey<T> key = CdmTransientEntityCacher.generateKey(cdmBase);
160
            cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
161
        }
162
        if(cachedCdmEntity == null) {
163
            cachedCdmEntity = super.getFromCache(cdmBase);
164
        }
165
        return cachedCdmEntity;
166
    }
167

    
168
    @Override
169
    public <T extends CdmBase> T load(T cdmEntity) {
170
        @SuppressWarnings("unchecked")
171
        T cachedCdmEntity = (T)getFromCache(cdmEntity.getUuid());
172

    
173
        if(isCachable(cdmEntity) && cachedCdmEntity == null) {
174
            cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
175
        }
176
        return cachedCdmEntity;
177
    }
178

    
179
    @Override
180
    public void changed() {
181
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
182
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
183
            this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
184
        } else {
185
            this.cdmTransientEntityCacher = null;
186
        }
187
    }
188
}
    (1-1/1)