Skip to content

Commit 0a8a7e2

Browse files
XML Config Cache Helper. Issue #8.
1 parent 517c07d commit 0a8a7e2

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@
5252
<version>1.0.0</version>
5353
</dependency>
5454

55+
<!-- Injection interface. JSR 330 -->
56+
<dependency>
57+
<groupId>javax.inject</groupId>
58+
<artifactId>javax.inject</artifactId>
59+
<version>1</version>
60+
</dependency>
61+
62+
<!-- Config -->
63+
<dependency>
64+
<groupId>com.github.bordertech.config</groupId>
65+
<artifactId>config</artifactId>
66+
<version>1.0.1</version>
67+
</dependency>
68+
5569
<!-- Logging -->
5670
<dependency>
5771
<groupId>org.slf4j</groupId>

taskmaster-cache-helper/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@
3838
<artifactId>cache-api</artifactId>
3939
</dependency>
4040

41+
<!-- Config -->
42+
<dependency>
43+
<groupId>com.github.bordertech.config</groupId>
44+
<artifactId>config</artifactId>
45+
</dependency>
46+
47+
<!-- Injection interface. JSR 330 -->
48+
<dependency>
49+
<groupId>javax.inject</groupId>
50+
<artifactId>javax.inject</artifactId>
51+
<scope>provided</scope>
52+
</dependency>
53+
4154
<!-- Servlet Interface -->
4255
<dependency>
4356
<groupId>javax.servlet</groupId>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.bordertech.taskmaster.cache;
2+
3+
import com.github.bordertech.config.Config;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import javax.cache.Cache;
7+
import javax.cache.CacheManager;
8+
import javax.cache.Caching;
9+
import javax.cache.configuration.Configuration;
10+
import javax.cache.expiry.Duration;
11+
import javax.cache.spi.CachingProvider;
12+
import javax.inject.Singleton;
13+
import org.apache.commons.logging.Log;
14+
import org.apache.commons.logging.LogFactory;
15+
16+
/**
17+
* Cache Helper that relies on the cache config in a xml file.
18+
*/
19+
@Singleton
20+
public class CacheHelperXmlConfigImpl implements CacheHelper {
21+
22+
private static final Log LOGGER = LogFactory.getLog(CacheHelperXmlConfigImpl.class);
23+
24+
private static final CacheManager MANAGER;
25+
26+
static {
27+
// Load cache configfile location (default tm-cache.xml)
28+
String config = Config.getInstance().getString("bordertech.taskmaster.cache.config", "/tm-cache.xml");
29+
LOGGER.info("Loading cache config [" + config + "].");
30+
URI uri;
31+
try {
32+
uri = CacheHelperXmlConfigImpl.class.getResource(config).toURI();
33+
} catch (URISyntaxException e) {
34+
throw new IllegalStateException("Could not load cache config [" + config + "]." + e.getMessage(), e);
35+
}
36+
CachingProvider cachingProvider = Caching.getCachingProvider();
37+
MANAGER = cachingProvider.getCacheManager(uri, CacheHelperXmlConfigImpl.class.getClassLoader());
38+
}
39+
40+
@Override
41+
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
42+
final Class<V> valueClass, final Duration duration) {
43+
// Ignore duration
44+
return handleGetCache(name, keyClass, valueClass);
45+
}
46+
47+
@Override
48+
public synchronized <K, V> Cache<K, V> getOrCreateCache(final String name, final Class<K> keyClass,
49+
final Class<V> valueClass, final Configuration<K, V> config) {
50+
// Ignore config
51+
return handleGetCache(name, keyClass, valueClass);
52+
}
53+
54+
/**
55+
* Get the pre-configured cache.
56+
*
57+
* @param name the cache name
58+
* @param keyClass the cache key class
59+
* @param valueClass the cache entry class
60+
* @return the cache instance
61+
* @param <K> the cache key type
62+
* @param <V> the cache entry type
63+
*/
64+
protected synchronized <K, V> Cache<K, V> handleGetCache(final String name, final Class<K> keyClass,
65+
final Class<V> valueClass) {
66+
return MANAGER.getCache(name, keyClass, valueClass);
67+
}
68+
69+
/**
70+
* @return the XML Configured cache manager
71+
*/
72+
protected final CacheManager getManager() {
73+
return MANAGER;
74+
}
75+
}

0 commit comments

Comments
 (0)