001: package org.shiftone.cache;
002:
003: import org.shiftone.cache.config.ConfigurationInternals;
004: import org.shiftone.cache.policy.zero.ZeroCacheFactory;
005: import org.shiftone.cache.util.Log;
006:
007: import java.io.File;
008: import java.io.FileInputStream;
009: import java.io.InputStream;
010: import java.util.Properties;
011:
012: /**
013: * @version $Revision: 1.13 $
014: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
015: */
016: public class CacheConfiguration {
017:
018: private static final Log LOG = new Log(CacheConfiguration.class);
019: private static final String DEFAULT_CACHE_CONFIG = "cache.properties";
020: private ConfigurationInternals internals;
021:
022: /**
023: * create a default cache configuration
024: */
025: public CacheConfiguration() throws ConfigurationException {
026: this (new String[] { DEFAULT_CACHE_CONFIG });
027: }
028:
029: public CacheConfiguration(String fileName)
030: throws ConfigurationException {
031: this (new String[] { fileName });
032: }
033:
034: public CacheConfiguration(String[] fileNames)
035: throws ConfigurationException {
036:
037: Properties properties = new Properties();
038:
039: for (int i = 0; i < fileNames.length; i++) {
040: init(properties, fileNames[i]);
041: }
042:
043: internals = new ConfigurationInternals(properties);
044: }
045:
046: public CacheConfiguration(Properties properties)
047: throws ConfigurationException {
048: internals = new ConfigurationInternals(properties);
049: }
050:
051: public void init(Properties properties, String fileName)
052: throws ConfigurationException {
053:
054: File file;
055: InputStream inputStream = null;
056:
057: try {
058: file = new File(fileName);
059:
060: if (file.isFile()) {
061: inputStream = new FileInputStream(fileName);
062:
063: LOG.info("file: " + file.getAbsolutePath());
064: } else {
065: inputStream = getClass().getResourceAsStream(fileName);
066:
067: LOG.info("resource: " + fileName);
068: }
069:
070: properties.load(inputStream);
071: } catch (Throwable e) {
072: throw new ConfigurationException(e);
073: } finally {
074: close(inputStream);
075: }
076: }
077:
078: private void close(InputStream inputStream) {
079:
080: if (inputStream != null) {
081: try {
082: inputStream.close();
083: } catch (Throwable e) {
084: }
085: }
086: }
087:
088: /**
089: * Obtain a configured cache factory by it's name. If no factory
090: * exists by this name, a ConfigurationException is thrown.
091: */
092: public CacheFactory getCacheFactory(String factoryName)
093: throws ConfigurationException {
094:
095: CacheFactory cacheFactory = null;
096:
097: cacheFactory = internals.getFactory(factoryName);
098:
099: if (cacheFactory == null) {
100: throw new ConfigurationException(
101: "cache factory not configured : " + cacheFactory);
102: }
103:
104: return cacheFactory;
105: }
106:
107: /**
108: * Create a new cache, using the configured values for the
109: * factory, timeout, and maxSize.
110: */
111: public Cache createConfiguredCache(String cacheName)
112: throws ConfigurationException {
113:
114: CacheFactory factory = getConfiguredFactoryForCache(cacheName);
115: long timeout = getConfiguredTimeoutForCache(cacheName);
116: int maxSize = getConfiguredMaxSizeForCache(cacheName);
117:
118: return factory.newInstance(cacheName, timeout, maxSize);
119: }
120:
121: /**
122: * Create a new cache by looking up the configured factory, and then using supplied
123: * name, timeout and max size. Method requested by Neville.
124: */
125: public Cache createConfiguredCache(String cacheName, long timeout,
126: int maxSize) throws ConfigurationException {
127:
128: CacheFactory factory = getConfiguredFactoryForCache(cacheName);
129:
130: return factory.newInstance(cacheName, timeout, maxSize);
131: }
132:
133: /**
134: * Attempt to create a configured cache, as in createConfiguredCache, except if
135: * an error occures, a "zero cache" will be returned. In other words, any exception
136: * is supressed, and the failure is hidden from the application (except there won't
137: * be any caching).
138: */
139: public Cache createConfiguredCacheSafely(String cacheName) {
140:
141: try {
142: return createConfiguredCache(cacheName);
143: } catch (Exception e) {
144: LOG.error("error with configuration for cache : "
145: + cacheName, e);
146:
147: return ZeroCacheFactory.NULL_CACHE;
148: }
149: }
150:
151: public CacheFactory getConfiguredFactoryForCache(String cacheName)
152: throws ConfigurationException {
153:
154: String factoryName = internals.getConfiguredCacheProperty(
155: "factory", cacheName);
156:
157: return getCacheFactory(factoryName);
158: }
159:
160: public long getConfiguredTimeoutForCache(String cacheName)
161: throws ConfigurationException {
162:
163: String timeout = internals.getConfiguredCacheProperty(
164: "timeout", cacheName);
165:
166: return Long.parseLong(timeout);
167: }
168:
169: public int getConfiguredMaxSizeForCache(String cacheName)
170: throws ConfigurationException {
171:
172: String maxsize = internals.getConfiguredCacheProperty(
173: "maxsize", cacheName);
174:
175: return Integer.parseInt(maxsize);
176: }
177:
178: public static void main(String[] args) throws Exception {
179:
180: try {
181: System.out.println("test");
182:
183: CacheConfiguration config = new CacheConfiguration();
184: CacheFactory factory;
185:
186: factory = config.getCacheFactory("lru");
187: factory = config.getCacheFactory("missTest");
188: factory = config.getCacheFactory("statLru");
189: factory = config.getCacheFactory("softLfu");
190:
191: Cache cache = config
192: .createConfiguredCache("com.indemand.royalty.organization.Channel");
193:
194: LOG.info("cache = " + cache);
195:
196: // factory.newInstance("test", 100, 100);
197: LOG.info(factory.newInstance("xxx", 1, 2));
198: } catch (Throwable e) {
199: LOG.error("main", e);
200: }
201: }
202: }
|