Project

General

Profile

Download (5.97 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.stereotype.Component;
7

    
8
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
9
import eu.etaxonomy.cdm.api.config.EhCacheConfiguration;
10
import eu.etaxonomy.cdm.cache.CacheLoader;
11
import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
12
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
13
import eu.etaxonomy.cdm.config.ConfigFileUtil;
14
import eu.etaxonomy.cdm.model.common.CdmBase;
15
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
16
import eu.etaxonomy.cdm.model.term.TermBase;
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;
25

    
26
/**
27
 * Class which uses CDM services to cache cdm entities
28
 *
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.
32
 *
33
 * @author cmathew
34
 */
35
@Component //FIXME This indicates that the CdmServiceCacher is initialized as Spring Component but it seems only to be instantiated directly
36
public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
37

    
38

    
39
    private ICdmEntitySessionManager cdmEntitySessionManager;
40

    
41
    private CdmTransientEntityCacher cdmTransientEntityCacher;
42

    
43
    private CacheLoader cacheLoader;
44

    
45
    @Override
46
    protected void setup() {
47

    
48
        setUpCacheManager();
49

    
50
        DefinedTermBase.setCacher(this);
51
        CdmTransientEntityCacher.setPermanentCacher(this);
52
        TermServiceRequestExecutor.setDefaultCacher(this);
53

    
54
        cacheLoader = new CacheLoader(this);
55
    }
56

    
57
    private void setUpCacheManager() {
58

    
59
        EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
60

    
61
        DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
62
        File ehcacheFolder;
63
        try {
64
            ehcacheFolder = ConfigFileUtil.getCdmHomeSubDir("taxeditor-ehcache");
65
        } catch (Exception e){
66
            ehcacheFolder = ConfigFileUtil.getCdmHomeSubDirFallback("taxeditor-ehcache");
67
        }
68

    
69
        // FIXME use subfolder per taxeditor version to allow running multiple installations in parallel
70
        // String taxEditorVersion = ..;
71
        // File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
72
        diskStoreConfiguration.setPath(ehcacheFolder.getAbsolutePath());
73

    
74
        cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
75
        addCacheManager(cacheConfig.cacheManager());
76

    
77
    }
78

    
79

    
80
    @Override
81
    protected CacheConfiguration getDefaultCacheConfiguration() {
82
        // For a better understanding on how to size caches, refer to
83
        // http://ehcache.org/documentation/configuration/cache-size
84

    
85
        SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
86
        sizeOfConfig.setMaxDepth(100);
87
        sizeOfConfig.setMaxDepthExceededBehavior("abort");
88

    
89
        return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
90
        	.eternal(true)
91
        	.statistics(true)
92
        	.sizeOfPolicy(sizeOfConfig)
93
        	.overflowToOffHeap(false);
94

    
95
    }
96

    
97
    @Override
98
    protected CdmBase findByUuid(UUID uuid) {
99
        CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
100
        return load(term);
101
    }
102

    
103
    @Override
104
    public boolean isCachable(CdmBase cdmEntity) {
105
     //   if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
106
    	   if(cdmEntity != null && cdmEntity instanceof TermBase) {
107
            return true;
108
        }
109
        return false;
110
    }
111

    
112

    
113
    public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
114
        this.cdmEntitySessionManager = cdmEntitySessionManager;
115
        if(cdmEntitySessionManager != null) {
116
            cdmEntitySessionManager.addSessionObserver(this);
117
        }
118
    }
119

    
120

    
121
    public CdmTransientEntityCacher getCurrentCacher() {
122
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
123
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
124
            return ((CdmEntitySession) cdmEntitySession).getCacher();
125
        }
126
        return null;
127
    }
128

    
129
    @Override
130
    public <T extends CdmBase> T  getFromCache(T cdmBase) {
131
        T cachedCdmEntity = null;
132
        // first we check in the active session cache if the
133
        // entity has been loaded there
134
        // FIXME:Remoting do we really need the cdmTransientEntityCacher
135
        // here. Is it not guarenteed that all every entity which 'isCachable'
136
        // by this cacher is cached only in this cacher ?
137
        if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
138
            CdmEntityCacheKey<T> key = CdmTransientEntityCacher.generateKey(cdmBase);
139
            cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
140

    
141
        }
142
        if(cachedCdmEntity == null) {
143
            cachedCdmEntity = super.getFromCache(cdmBase);
144
        }
145
        return cachedCdmEntity;
146
    }
147

    
148
    @Override
149
    public <T extends CdmBase> T load(T cdmEntity) {
150
        T cachedCdmEntity = (T)getFromCache(cdmEntity.getUuid());
151

    
152
        if(isCachable(cdmEntity) && cachedCdmEntity == null) {
153
            cachedCdmEntity =  cacheLoader.load(cdmEntity, false, true);
154
        }
155
        return cachedCdmEntity;
156
    }
157

    
158
    @Override
159
    public void changed() {
160
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
161
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
162
            this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
163
        } else {
164
            this.cdmTransientEntityCacher = null;
165
        }
166
    }
167
}
    (1-1/1)