1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.ehcache.jcache;
17
18 import net.sf.ehcache.Ehcache;
19 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
20 import net.sf.ehcache.util.ClassLoaderUtil;
21 import net.sf.ehcache.util.PropertyUtil;
22 import net.sf.jsr107cache.Cache;
23 import net.sf.jsr107cache.CacheException;
24 import net.sf.jsr107cache.CacheFactory;
25 import net.sf.jsr107cache.CacheLoader;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.util.Map;
30
31
32
33
34
35
36
37
38
39 public class JCacheFactory implements CacheFactory {
40
41
42 private static final Log LOG = LogFactory.getLog(JCacheFactory.class.getName());
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public Cache createCache(Map environment) throws CacheException {
78
79
80 CacheLoader cacheLoader = null;
81 Ehcache cache = null;
82 try {
83 String name = PropertyUtil.extractAndLogProperty("name", environment);
84
85 String maxElementsInMemoryString = PropertyUtil.extractAndLogProperty("maxElementsInMemory", environment);
86 int maxElementsInMemory = Integer.parseInt(maxElementsInMemoryString);
87
88 String memoryStoreEvictionPolicyString = PropertyUtil.extractAndLogProperty("memoryStoreEvictionPolicy", environment);
89 MemoryStoreEvictionPolicy memoryStoreEvictionPolicy =
90 MemoryStoreEvictionPolicy.fromString(memoryStoreEvictionPolicyString);
91
92 String overflowToDiskString = PropertyUtil.extractAndLogProperty("overflowToDisk", environment);
93 boolean overflowToDisk = PropertyUtil.parseBoolean(overflowToDiskString);
94
95 String eternalString = PropertyUtil.extractAndLogProperty("eternal", environment);
96 boolean eternal = PropertyUtil.parseBoolean(eternalString);
97
98 String timeToLiveSecondsString = PropertyUtil.extractAndLogProperty("timeToLiveSeconds", environment);
99 long timeToLiveSeconds = Long.parseLong(timeToLiveSecondsString);
100
101 String timeToIdleSecondsString = PropertyUtil.extractAndLogProperty("timeToIdleSeconds", environment);
102 long timeToIdleSeconds = Long.parseLong(timeToIdleSecondsString);
103
104 String diskPersistentString = PropertyUtil.extractAndLogProperty("diskPersistentSeconds", environment);
105 boolean diskPersistent = PropertyUtil.parseBoolean(diskPersistentString);
106
107 long diskExpiryThreadIntervalSeconds = 0;
108 String diskExpiryThreadIntervalSecondsString =
109 PropertyUtil.extractAndLogProperty("diskExpiryThreadIntervalSeconds", environment);
110 if (diskExpiryThreadIntervalSecondsString != null) {
111 diskExpiryThreadIntervalSeconds = Long.parseLong(diskExpiryThreadIntervalSecondsString);
112 }
113
114 int maxElementsOnDisk = 0;
115 String maxElementsOnDiskString =
116 PropertyUtil.extractAndLogProperty("maxElementsOnDisk", environment);
117 if (maxElementsOnDiskString != null) {
118 maxElementsOnDisk = Integer.parseInt(maxElementsOnDiskString);
119 }
120
121 cacheLoader = null;
122 String cacheLoaderFactoryClassName =
123 PropertyUtil.extractAndLogProperty("cacheLoaderFactoryClassName", environment);
124 if (cacheLoaderFactoryClassName == null) {
125 LOG.debug("cacheLoaderFactoryClassName not configured. Skipping...");
126 } else {
127 CacheLoaderFactory factory = (CacheLoaderFactory)
128 ClassLoaderUtil.createNewInstance(cacheLoaderFactoryClassName);
129 cacheLoader = factory.createCacheLoader(environment);
130 }
131
132
133 cache = new net.sf.ehcache.Cache(name, maxElementsInMemory, memoryStoreEvictionPolicy,
134 overflowToDisk, null, eternal,
135 timeToLiveSeconds, timeToIdleSeconds, diskPersistent, diskExpiryThreadIntervalSeconds,
136 null, null, maxElementsOnDisk);
137
138 net.sf.ehcache.CacheManager.getInstance().addCache(cache);
139 } catch (net.sf.ehcache.CacheException e) {
140 throw new CacheException(e.getMessage(), e);
141 }
142
143
144 return new JCache(cache, cacheLoader);
145
146 }
147 }