Project

General

Profile

Download (3.74 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.remoting.cache;
2

    
3
import net.sf.ehcache.Cache;
4
import net.sf.ehcache.CacheException;
5
import net.sf.ehcache.CacheManager;
6
import net.sf.ehcache.config.CacheConfiguration;
7
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
8

    
9
import org.apache.log4j.Logger;
10

    
11

    
12
public class CdmRemoteCacheManager {
13

    
14
    private static final Logger logger = Logger.getLogger(CdmRemoteCacheManager.class);
15

    
16

    
17
    private Cache cdmlibModelCache;
18

    
19
    private static CdmRemoteCacheManager cdmRemoteCacheManager = null;
20

    
21
    public static final String CDM_MODEL_CACHE_NAME = "cdmModelGetMethodsCache";
22

    
23
    private static Thread initThread;
24

    
25
    private static boolean cacheInitialised = false;
26

    
27
    public enum CdmCacheManagerType {
28
        CDMLIB_MODEL,
29
        DEFAULT
30
    }
31

    
32
    public static CdmRemoteCacheManager getInstance(){
33

    
34
        if(cdmRemoteCacheManager == null) {
35
            cdmRemoteCacheManager = new CdmRemoteCacheManager();
36
        }
37
        return cdmRemoteCacheManager;
38
    }
39
    private CdmRemoteCacheManager() {
40

    
41

    
42
        try {
43
            // NOTE:Programmatically creating the cache manager may solve the problem of
44
            //      recreating data written to disk on startup
45
            //      see https://stackoverflow.com/questions/1729605/ehcache-persist-to-disk-issues
46
            //String cacheFilePath = CDMLIB_CACHE_MANAGER_CONFIG_RESOURCE.getFile().getAbsolutePath();
47
            //InputStream in = this.getClass().getClassLoader().getResourceAsStream("cdmlib-ehcache.xml");
48

    
49
            SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
50
            sizeOfConfig.setMaxDepth(1000);
51
            sizeOfConfig.setMaxDepthExceededBehavior("abort");
52

    
53
            CacheConfiguration modelcc = new CacheConfiguration(CDM_MODEL_CACHE_NAME, 0)
54
            .eternal(true)
55
            .statistics(true)
56
            .sizeOfPolicy(sizeOfConfig)
57
            .overflowToOffHeap(false);
58

    
59
            cdmlibModelCache = new Cache(modelcc);
60

    
61
            CacheManager.create().addCache(cdmlibModelCache);
62
            initCdmModelCache(cdmlibModelCache);
63

    
64
        } catch (CacheException e) {
65
            throw new CdmClientCacheException(e);
66
        }
67

    
68
    }
69

    
70
    private void initCdmModelCache(final Cache cache) {
71

    
72
        initThread = new Thread() {
73
            @Override
74
            public void run(){
75
                synchronized (cdmlibModelCache) {
76
                    CdmModelCacher cmdmc = new CdmModelCacher();
77
                    cmdmc.cacheGetterFields(cache);
78
                    cacheInitialised = true;
79
                    logger.info("Initialisation of CDM getter fields complete");
80
                    cdmlibModelCache.notify();
81
                }
82
            }
83

    
84
        };
85
        initThread.start();
86
    }
87

    
88
    public Cache getCdmModelGetMethodsCache(){
89
        //Note : Even though we synchronize this method, the cache can be simply
90
        //       retrieved using CacheManager.create().getCache(CDM_MODEL_CACHE_NAME)
91
        //       in which case the cache may not be fully initialised
92
        synchronized (cdmlibModelCache) {
93
            while(!cacheInitialised) {
94
                try {
95
                    logger.info("Waiting for initialisation of CDM getter fields to complete ...");
96
                    cdmlibModelCache.wait();
97
                } catch (InterruptedException e) {}
98
            }
99
        }
100
        logger.info("CDM getter fields cache initialised");
101
        return cdmlibModelCache;
102
    }
103

    
104
    public static void removeEntityCaches() {
105
        CacheManager cm = CacheManager.create();
106
        String[] cacheNames = CacheManager.create().getCacheNames();
107
        for(String cacheName : cacheNames) {
108
            if(!cacheName.equals(CDM_MODEL_CACHE_NAME)) {
109
                cm.removeCache(cacheName);
110
            }
111
        }
112
    }
113

    
114

    
115

    
116
}
(6-6/9)