|
| 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