KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > CacheConfiguration


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 JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.Properties JavaDoc;
13
14
15 /**
16  * @version $Revision: 1.13 $
17  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
18  */

19 public class CacheConfiguration
20 {
21
22     private static final Log LOG = new Log(CacheConfiguration.class);
23     private static final String JavaDoc DEFAULT_CACHE_CONFIG = "cache.properties";
24     private ConfigurationInternals internals;
25
26     /**
27      * create a default cache configuration
28      */

29     public CacheConfiguration() throws ConfigurationException
30     {
31         this(new String JavaDoc[]{ DEFAULT_CACHE_CONFIG });
32     }
33
34
35     public CacheConfiguration(String JavaDoc fileName) throws ConfigurationException
36     {
37         this(new String JavaDoc[]{ fileName });
38     }
39
40
41     public CacheConfiguration(String JavaDoc[] fileNames) throws ConfigurationException
42     {
43
44         Properties JavaDoc properties = new Properties JavaDoc();
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 JavaDoc properties) throws ConfigurationException
56     {
57         internals = new ConfigurationInternals(properties);
58     }
59
60
61     public void init(Properties JavaDoc properties, String JavaDoc fileName) throws ConfigurationException
62     {
63
64         File JavaDoc file;
65         InputStream JavaDoc inputStream = null;
66
67         try
68         {
69             file = new File JavaDoc(fileName);
70
71             if (file.isFile())
72             {
73                 inputStream = new FileInputStream JavaDoc(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 JavaDoc e)
87         {
88             throw new ConfigurationException(e);
89         }
90         finally
91         {
92             close(inputStream);
93         }
94     }
95
96
97     private void close(InputStream JavaDoc inputStream)
98     {
99
100         if (inputStream != null)
101         {
102             try
103             {
104                 inputStream.close();
105             }
106             catch (Throwable JavaDoc e) {}
107         }
108     }
109
110
111     /**
112      * Obtain a configured cache factory by it's name. If no factory
113      * exists by this name, a ConfigurationException is thrown.
114      */

115     public CacheFactory getCacheFactory(String JavaDoc 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     /**
132      * Create a new cache, using the configured values for the
133      * factory, timeout, and maxSize.
134      */

135     public Cache createConfiguredCache(String JavaDoc 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     /**
147      * Create a new cache by looking up the configured factory, and then using supplied
148      * name, timeout and max size. Method requested by Neville.
149      */

150     public Cache createConfiguredCache(String JavaDoc 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     /**
160      * Attempt to create a configured cache, as in createConfiguredCache, except if
161      * an error occures, a "zero cache" will be returned. In other words, any exception
162      * is supressed, and the failure is hidden from the application (except there won't
163      * be any caching).
164      */

165     public Cache createConfiguredCacheSafely(String JavaDoc cacheName)
166     {
167
168         try
169         {
170             return createConfiguredCache(cacheName);
171         }
172         catch (Exception JavaDoc 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 JavaDoc cacheName) throws ConfigurationException
182     {
183
184         String JavaDoc factoryName = internals.getConfiguredCacheProperty("factory", cacheName);
185
186         return getCacheFactory(factoryName);
187     }
188
189
190     public long getConfiguredTimeoutForCache(String JavaDoc cacheName) throws ConfigurationException
191     {
192
193         String JavaDoc timeout = internals.getConfiguredCacheProperty("timeout", cacheName);
194
195         return Long.parseLong(timeout);
196     }
197
198
199     public int getConfiguredMaxSizeForCache(String JavaDoc cacheName) throws ConfigurationException
200     {
201
202         String JavaDoc maxsize = internals.getConfiguredCacheProperty("maxsize", cacheName);
203
204         return Integer.parseInt(maxsize);
205     }
206
207
208     public static void main(String JavaDoc[] args) throws Exception JavaDoc
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             // factory.newInstance("test", 100, 100);
228
LOG.info(factory.newInstance("xxx", 1, 2));
229         }
230         catch (Throwable JavaDoc e)
231         {
232             LOG.error("main", e);
233         }
234     }
235 }
236
Popular Tags