Project

General

Profile

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

    
3
import java.util.UUID;
4

    
5
import net.sf.ehcache.config.CacheConfiguration;
6
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
7

    
8
import org.springframework.stereotype.Component;
9

    
10
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
11
import eu.etaxonomy.cdm.model.common.CdmBase;
12
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
13
import eu.etaxonomy.taxeditor.remoting.cache.CacheLoader;
14
import eu.etaxonomy.taxeditor.remoting.cache.CdmEntityCacheKey;
15
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
16
import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
17
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
18
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
19
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
20
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
21

    
22
/**
23
 * Class which uses CDM services to cache cdm entities
24
 *
25
 * FIXME: Currently only handles terms entities. It would be
26
 *        interesting to have a generic method which finds the
27
 *        correct service to load / cache the entity.
28
 *
29
 * @author cmathew
30
 *
31
 * @param <T>
32
 */
33
@Component
34
public class CdmServiceCacher extends CdmCacher  implements ICdmEntitySessionManagerObserver {
35

    
36

    
37

    
38
    private ICdmEntitySessionManager cdmEntitySessionManager;
39

    
40
    private CdmTransientEntityCacher cdmTransientEntityCacher;
41

    
42
    private CacheLoader cacheLoader;
43

    
44
    @Override
45
    protected void setup() {
46
        DefinedTermBase.setCacher(this);
47
        CdmTransientEntityCacher.setDefaultCacher(this);
48
        TermServiceRequestExecutor.setDefaultCacher(this);
49

    
50
        cacheLoader = new CacheLoader(this);
51
    }
52

    
53

    
54
    @Override
55
    protected CacheConfiguration getDefaultCacheConfiguration() {
56
        // For a better understanding on how to size caches, refer to
57
        // http://ehcache.org/documentation/configuration/cache-size
58

    
59
        SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
60
        sizeOfConfig.setMaxDepth(100);
61
        sizeOfConfig.setMaxDepthExceededBehavior("abort");
62

    
63
        return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
64
        .eternal(true)
65
        .statistics(true)
66
        .sizeOfPolicy(sizeOfConfig)
67
        .overflowToOffHeap(false);
68

    
69
    }
70

    
71
    @Override
72
    protected CdmBase findByUuid(UUID uuid) {
73
        CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
74
        return load(term);
75
    }
76

    
77
    /* (non-Javadoc)
78
     * @see eu.etaxonomy.cdm.model.ICdmCacher#isCachable(eu.etaxonomy.cdm.model.common.CdmBase)
79
     */
80
    @Override
81
    public boolean isCachable(CdmBase cdmEntity) {
82
        if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
83
            return true;
84
        }
85
        return false;
86
    }
87

    
88

    
89
    public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
90
        this.cdmEntitySessionManager = cdmEntitySessionManager;
91
        if(cdmEntitySessionManager != null) {
92
            cdmEntitySessionManager.addSessionObserver(this);
93
        }
94
    }
95

    
96

    
97
    public CdmTransientEntityCacher getCurrentCacher() {
98
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
99
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
100
            return ((CdmEntitySession) cdmEntitySession).getCacher();
101
        }
102
        return null;
103
    }
104

    
105
    @Override
106
    public CdmBase getFromCache(CdmBase cdmBase) {
107
        CdmBase cachedCdmEntity = null;
108
        // first we check in the active session cache if the
109
        // entity has been loaded there
110
        // FIXME:Remoting do we really need the cdmTransientEntityCacher
111
        // here. Is it not guarenteed that all every entity which 'isCachable'
112
        // by this cacher is cached only in this cacher ?
113
        if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
114
            CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
115
            cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
116

    
117
        }
118
        if(cachedCdmEntity == null) {
119
            cachedCdmEntity = super.getFromCache(cdmBase);
120
        }
121
        return cachedCdmEntity;
122
    }
123

    
124

    
125
    /* (non-Javadoc)
126
     * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
127
     */
128
    @Override
129
    public CdmBase load(CdmBase cdmEntity) {
130
        CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
131

    
132
        if(isCachable(cdmEntity) && cachedCdmEntity == null) {
133
            cachedCdmEntity =  cacheLoader.load(cdmEntity, false, true);
134
        }
135
        return cachedCdmEntity;
136
    }
137

    
138
    /* (non-Javadoc)
139
     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver#changed()
140
     */
141
    @Override
142
    public void changed() {
143
        ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
144
        if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
145
            this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
146
        } else {
147
            this.cdmTransientEntityCacher = null;
148
        }
149
    }
150
}
    (1-1/1)