1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs.jcache.openjpa;
20
21 import org.apache.openjpa.conf.OpenJPAConfiguration;
22 import org.apache.openjpa.datacache.DataCacheManagerImpl;
23 import org.apache.openjpa.lib.conf.ObjectValue;
24
25 import javax.cache.Cache;
26 import javax.cache.CacheManager;
27 import javax.cache.Caching;
28 import javax.cache.configuration.FactoryBuilder;
29 import javax.cache.configuration.MutableConfiguration;
30 import javax.cache.expiry.CreatedExpiryPolicy;
31 import javax.cache.expiry.Duration;
32 import javax.cache.expiry.ExpiryPolicy;
33 import javax.cache.integration.CacheLoader;
34 import javax.cache.integration.CacheWriter;
35 import javax.cache.spi.CachingProvider;
36 import java.net.URI;
37 import java.util.Map;
38 import java.util.Properties;
39
40 public class OpenJPAJCacheDataCacheManager extends DataCacheManagerImpl
41 {
42 private CachingProvider provider;
43 private CacheManager cacheManager;
44
45 @Override
46 public void initialize(final OpenJPAConfiguration conf, final ObjectValue dataCache, final ObjectValue queryCache)
47 {
48 super.initialize(conf, dataCache, queryCache);
49 provider = Caching.getCachingProvider();
50
51 final Properties properties = new Properties();
52 final Map<String, Object> props = conf.toProperties(false);
53 if (props != null)
54 {
55 for (final Map.Entry<?, ?> entry : props.entrySet())
56 {
57 if (entry.getKey() != null && entry.getValue() != null)
58 {
59 properties.setProperty(entry.getKey().toString(), entry.getValue().toString());
60 }
61 }
62 }
63
64 final String uri = properties.getProperty("jcache.uri", provider.getDefaultURI().toString());
65 cacheManager = provider.getCacheManager(URI.create(uri), provider.getDefaultClassLoader(), properties);
66 }
67
68 @Override
69 public void close()
70 {
71 super.close();
72 if (!cacheManager.isClosed())
73 {
74 cacheManager.close();
75 }
76 provider.close();
77 }
78
79 Cache<Object, Object> getOrCreateCache(final String prefix, final String entity)
80 {
81 final String internalName = prefix + entity;
82 Cache<Object, Object> cache = cacheManager.getCache(internalName);
83 if (cache == null)
84 {
85 final Properties properties = cacheManager.getProperties();
86 final MutableConfiguration<Object, Object> configuration = new MutableConfiguration<Object, Object>()
87 .setStoreByValue("true".equalsIgnoreCase(properties.getProperty("jcache.store-by-value", "false")));
88
89 configuration.setReadThrough("true".equals(properties.getProperty("jcache.read-through", "false")));
90 configuration.setWriteThrough("true".equals(properties.getProperty("jcache.write-through", "false")));
91 if (configuration.isReadThrough())
92 {
93 configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<Object, Object>>(properties.getProperty("jcache.cache-loader-factory")));
94 }
95 if (configuration.isWriteThrough())
96 {
97 configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<Object, Object>>(properties.getProperty("jcache.cache-writer-factory")));
98 }
99 final String expirtyPolicy = properties.getProperty("jcache.expiry-policy-factory");
100 if (expirtyPolicy != null)
101 {
102 configuration.setExpiryPolicyFactory(new FactoryBuilder.ClassFactory<ExpiryPolicy>(expirtyPolicy));
103 }
104 else
105 {
106 configuration.setExpiryPolicyFactory(new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.FIVE_MINUTES)));
107 }
108 configuration.setManagementEnabled("true".equals(properties.getProperty("jcache.management-enabled", "false")));
109 configuration.setStatisticsEnabled("true".equals(properties.getProperty("jcache.statistics-enabled", "false")));
110
111 cache = cacheManager.createCache(internalName, configuration);
112 }
113 return cache;
114 }
115
116 CacheManager getCacheManager()
117 {
118 return cacheManager;
119 }
120 }