1 package org.shiftone.cache; 2 3 4 5 import org.shiftone.cache.config.ConfigurationInternals; 6 import org.shiftone.cache.policy.zero.ZeroCacheFactory; 7 import org.shiftone.cache.util.Log; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.InputStream ; 12 import java.util.Properties ; 13 14 15 19 public class CacheConfiguration 20 { 21 22 private static final Log LOG = new Log(CacheConfiguration.class); 23 private static final String DEFAULT_CACHE_CONFIG = "cache.properties"; 24 private ConfigurationInternals internals; 25 26 29 public CacheConfiguration() throws ConfigurationException 30 { 31 this(new String []{ DEFAULT_CACHE_CONFIG }); 32 } 33 34 35 public CacheConfiguration(String fileName) throws ConfigurationException 36 { 37 this(new String []{ fileName }); 38 } 39 40 41 public CacheConfiguration(String [] fileNames) throws ConfigurationException 42 { 43 44 Properties properties = new Properties (); 45 46 for (int i = 0; i < fileNames.length; i++) 47 { 48 init(properties, fileNames[i]); 49 } 50 51 internals = new ConfigurationInternals(properties); 52 } 53 54 55 public CacheConfiguration(Properties properties) throws ConfigurationException 56 { 57 internals = new ConfigurationInternals(properties); 58 } 59 60 61 public void init(Properties properties, String fileName) throws ConfigurationException 62 { 63 64 File file; 65 InputStream inputStream = null; 66 67 try 68 { 69 file = new File (fileName); 70 71 if (file.isFile()) 72 { 73 inputStream = new FileInputStream (fileName); 74 75 LOG.info("file: " + file.getAbsolutePath()); 76 } 77 else 78 { 79 inputStream = getClass().getResourceAsStream(fileName); 80 81 LOG.info("resource: " + fileName); 82 } 83 84 properties.load(inputStream); 85 } 86 catch (Throwable e) 87 { 88 throw new ConfigurationException(e); 89 } 90 finally 91 { 92 close(inputStream); 93 } 94 } 95 96 97 private void close(InputStream inputStream) 98 { 99 100 if (inputStream != null) 101 { 102 try 103 { 104 inputStream.close(); 105 } 106 catch (Throwable e) {} 107 } 108 } 109 110 111 115 public CacheFactory getCacheFactory(String factoryName) throws ConfigurationException 116 { 117 118 CacheFactory cacheFactory = null; 119 120 cacheFactory = internals.getFactory(factoryName); 121 122 if (cacheFactory == null) 123 { 124 throw new ConfigurationException("cache factory not configured : " + cacheFactory); 125 } 126 127 return cacheFactory; 128 } 129 130 131 135 public Cache createConfiguredCache(String cacheName) throws ConfigurationException 136 { 137 138 CacheFactory factory = getConfiguredFactoryForCache(cacheName); 139 long timeout = getConfiguredTimeoutForCache(cacheName); 140 int maxSize = getConfiguredMaxSizeForCache(cacheName); 141 142 return factory.newInstance(cacheName, timeout, maxSize); 143 } 144 145 146 150 public Cache createConfiguredCache(String cacheName, long timeout, int maxSize) throws ConfigurationException 151 { 152 153 CacheFactory factory = getConfiguredFactoryForCache(cacheName); 154 155 return factory.newInstance(cacheName, timeout, maxSize); 156 } 157 158 159 165 public Cache createConfiguredCacheSafely(String cacheName) 166 { 167 168 try 169 { 170 return createConfiguredCache(cacheName); 171 } 172 catch (Exception e) 173 { 174 LOG.error("error with configuration for cache : " + cacheName, e); 175 176 return ZeroCacheFactory.NULL_CACHE; 177 } 178 } 179 180 181 public CacheFactory getConfiguredFactoryForCache(String cacheName) throws ConfigurationException 182 { 183 184 String factoryName = internals.getConfiguredCacheProperty("factory", cacheName); 185 186 return getCacheFactory(factoryName); 187 } 188 189 190 public long getConfiguredTimeoutForCache(String cacheName) throws ConfigurationException 191 { 192 193 String timeout = internals.getConfiguredCacheProperty("timeout", cacheName); 194 195 return Long.parseLong(timeout); 196 } 197 198 199 public int getConfiguredMaxSizeForCache(String cacheName) throws ConfigurationException 200 { 201 202 String maxsize = internals.getConfiguredCacheProperty("maxsize", cacheName); 203 204 return Integer.parseInt(maxsize); 205 } 206 207 208 public static void main(String [] args) throws Exception 209 { 210 211 try 212 { 213 System.out.println("test"); 214 215 CacheConfiguration config = new CacheConfiguration(); 216 CacheFactory factory; 217 218 factory = config.getCacheFactory("lru"); 219 factory = config.getCacheFactory("missTest"); 220 factory = config.getCacheFactory("statLru"); 221 factory = config.getCacheFactory("softLfu"); 222 223 Cache cache = config.createConfiguredCache("com.indemand.royalty.organization.Channel"); 224 225 LOG.info("cache = " + cache); 226 227 LOG.info(factory.newInstance("xxx", 1, 2)); 229 } 230 catch (Throwable e) 231 { 232 LOG.error("main", e); 233 } 234 } 235 } 236 | Popular Tags |