KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > component > store > WhirlycacheStore


1 /*
2 Copyright 2004
3 Peter Royal <peter.royal@pobox.com>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */

17
18 package com.whirlycott.cache.component.store;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.activity.Initializable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.thread.ThreadSafe;
29 import org.apache.excalibur.store.Store;
30
31 import com.whirlycott.cache.Cache;
32 import com.whirlycott.cache.CacheConfiguration;
33 import com.whirlycott.cache.CacheDecorator;
34 import com.whirlycott.cache.CacheMaintenancePolicy;
35 import com.whirlycott.cache.Constants;
36 import com.whirlycott.cache.ManagedCache;
37 import com.whirlycott.cache.Messages;
38
39 /**
40 * Implementation of the <a HREF="http://excalibur.apache.org/store/">Apache Excalibur Store</a> using Whirlycache as
41 * the backend.
42 *
43 * @author <a HREF="mailto:peter.royal@pobox.com">peter royal</a>
44 */

45 public class WhirlycacheStore implements Store, ThreadSafe, Configurable, Initializable, Disposable {
46    
47    /**
48     * This is the cache that holds the data that we are going to use.
49     */

50    private Cache cache;
51
52    /**
53     * Represents the cache configuration for this specific store.
54     */

55    private CacheConfiguration cacheConfiguration;
56    
57    /**
58     * The class that implements the Policy interface.
59     */

60    private Class JavaDoc cacheMaintenancePolicyClass;
61
62    /**
63     * The Class that implements the backend ManagedCache interface.
64     */

65    private Class JavaDoc managedCacheClass;
66
67    /**
68     * Clears all items in the cache.
69     */

70    public void clear() {
71        cache.clear();
72    }
73
74    
75    /**
76     * Configure the cache using a Configuration object.
77     */

78    public void configure(final Configuration configuration) throws ConfigurationException {
79        final Configuration backend = configuration.getChild(Constants.CONFIG_BACKEND);
80        final Configuration policy = configuration.getChild(Constants.CONFIG_POLICY);
81
82        cacheConfiguration = new CacheConfiguration();
83
84        cacheConfiguration.setName(configuration.getChild(Constants.CONFIG_NAME).getValue(toString()));
85        cacheConfiguration.setBackend(backend.getValue());
86        cacheConfiguration.setMaxSize(configuration.getChild(Constants.CONFIG_MAXSIZE).getValueAsInteger());
87        cacheConfiguration.setPolicy(policy.getValue());
88        cacheConfiguration.setTunerSleepTime(configuration.getChild(Constants.CONFIG_TUNER_SLEEPTIME).getValueAsInteger());
89
90        managedCacheClass = loadManagedCacheClass(backend.getLocation());
91        cacheMaintenancePolicyClass = loadCacheMaintenancePolicyClass(policy.getLocation());
92    }
93
94    
95    /**
96     * I can't imagine why anybody would rely on this method.
97     */

98    public boolean containsKey(final Object JavaDoc key) {
99        boolean retval = false;
100        if (cache.retrieve(key) != null)
101            retval = true;
102        return retval;
103    }
104    
105
106    private CacheMaintenancePolicy[] createCacheMaintenancePolicies(final ManagedCache cache)
107        throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
108        final CacheMaintenancePolicy policy = (CacheMaintenancePolicy)cacheMaintenancePolicyClass.newInstance();
109
110        policy.setCache(cache);
111        policy.setConfiguration(cacheConfiguration);
112
113        return new CacheMaintenancePolicy[]{policy};
114    }
115
116    /**
117     * Shuts down the Whirlycache and its associated tuning thread.
118     */

119    public void dispose() {
120        cache.clear();
121        /*
122         * The field 'cache' is really a CacheDecorator. We don't expose it because
123         * it shouldn't be used directly. But we'll cast it here.
124         */

125        ((CacheDecorator)cache).shutdown();
126    }
127    
128
129    /**
130     * This method is not supported by Whirlycache.
131     */

132    public void free() {
133        // we don't support this
134
}
135    
136    /**
137     * Gets an object out of the whirlycache
138     */

139    public Object JavaDoc get(final Object JavaDoc key) {
140        return cache.retrieve(key);
141    }
142    
143
144    /**
145     * Initializes a Whirlycache.
146     */

147    public void initialize() throws Exception JavaDoc {
148        final ManagedCache managedCache = (ManagedCache)managedCacheClass.newInstance();
149        cache = new CacheDecorator(managedCache, cacheConfiguration, createCacheMaintenancePolicies(managedCache));
150    }
151    
152    /**
153     * We don't support keys().
154     */

155    public Enumeration JavaDoc keys() {
156        throw new UnsupportedOperationException JavaDoc();
157    }
158    
159
160    /**
161     * Loads up the specified cache maintenance class
162     * @param _location
163     * @return
164     * @throws ConfigurationException
165     */

166    private Class JavaDoc loadCacheMaintenancePolicyClass(final String JavaDoc _location) throws ConfigurationException {
167        final String JavaDoc policy = cacheConfiguration.getPolicy();
168
169        try {
170            final Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(policy);
171
172            if (!CacheMaintenancePolicy.class.isAssignableFrom(clazz)) {
173                final Object JavaDoc[] args = {
174                        policy,
175                        _location
176                };
177                throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.not_cache_maintenance_policy", args)); //$NON-NLS-1$
178
}
179
180            return clazz;
181        } catch (final ClassNotFoundException JavaDoc e) {
182            final Object JavaDoc[] args = {
183                    policy,
184                    _location
185            };
186            throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.cannot_load_policy", args), e); //$NON-NLS-1$
187
}
188    }
189
190    /**
191     * Loads up the specified managed cache class.
192     * @param _location
193     * @return
194     * @throws ConfigurationException
195     */

196    private Class JavaDoc loadManagedCacheClass(final String JavaDoc _location) throws ConfigurationException {
197        final String JavaDoc backend = cacheConfiguration.getBackend();
198
199        try {
200            final Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(backend);
201
202            if (!ManagedCache.class.isAssignableFrom(clazz)) {
203                final Object JavaDoc[] args = {
204                        backend,
205                        _location
206                };
207                throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.not_managed_cache", args)); //$NON-NLS-1$
208
}
209
210            return clazz;
211        } catch (final ClassNotFoundException JavaDoc e) {
212            final Object JavaDoc[] args = {
213                    backend,
214                    _location
215            };
216            throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.cannot_load_backend", args), e); //$NON-NLS-1$
217
}
218    }
219    
220
221    /**
222     * Removes the specified object from the cache.
223     */

224    public void remove(final Object JavaDoc key) {
225        cache.remove(key);
226    }
227
228    
229    /**
230     * Returns the number of items in the cache.
231     */

232    public int size() {
233        return cache.size();
234    }
235    
236
237    /**
238     * Stores a value in the cache that can be retrieved using 'key'.
239     */

240    public void store(final Object JavaDoc key, final Object JavaDoc value) throws IOException JavaDoc {
241        cache.store(key, value);
242    }
243 }
Popular Tags