KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > services > cache > DefaultScarabCacheService


1 package org.tigris.scarab.services.cache;
2
3 /* ================================================================
4  * Copyright (c) 2001 Collab.Net. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.Serializable JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.Map JavaDoc;
53 import java.util.WeakHashMap JavaDoc;
54
55 import org.apache.avalon.framework.activity.Initializable;
56 import org.apache.avalon.framework.logger.AbstractLogEnabled;
57 import org.apache.avalon.framework.service.ServiceManager;
58 import org.apache.avalon.framework.service.Serviceable;
59 import org.apache.fulcrum.InitializationException;
60 import org.apache.fulcrum.pool.PoolService;
61 import org.tigris.scarab.tools.localization.L10NKeySet;
62 import org.tigris.scarab.util.Log;
63 import org.tigris.scarab.util.ScarabRuntimeException;
64
65 /**
66  * This class provides a simple Map cache that is available to the
67  * current thread.
68  *
69  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
70  * @version $Id: DefaultScarabCacheService.java 9284 2004-12-02 21:13:20Z dabbous $
71  */

72 public class DefaultScarabCacheService
73 extends AbstractLogEnabled implements ScarabCacheService, Serviceable, Initializable
74 {
75     
76
77     //private Configuration props;
78
private Map JavaDoc maps;
79     private Class JavaDoc keyClass;
80     
81     private PoolService poolService;
82     private ServiceManager manager;
83
84     public DefaultScarabCacheService()
85     {
86     }
87
88     public Map JavaDoc getMapImpl()
89     {
90         Thread JavaDoc t = Thread.currentThread();
91         Map JavaDoc map = (Map JavaDoc)maps.get(t);
92         if (map == null)
93         {
94             map = new HashMap JavaDoc();
95             maps.put(t, map);
96         }
97         
98         return map;
99     }
100
101     public void clearImpl()
102     {
103         Map JavaDoc map = (Map JavaDoc)maps.get(Thread.currentThread());
104         if (map != null)
105         {
106             Iterator JavaDoc i = map.keySet().iterator();
107             while (i.hasNext())
108             {
109                 poolService.putInstance(i.next());
110             }
111             map.clear();
112         }
113     }
114
115     public Object JavaDoc getImpl(Serializable JavaDoc instanceOrClass, String JavaDoc method)
116     {
117         Object JavaDoc result = null;
118         try
119         {
120             ScarabCacheKey key =
121                 (ScarabCacheKey)poolService.getInstance(keyClass);
122             key.init(instanceOrClass, method);
123             result = getMapImpl().get(key);
124         }
125         catch (Exception JavaDoc e)
126         {
127             Log.get().error(e);
128         }
129         return result;
130     }
131
132     public Object JavaDoc getImpl(Serializable JavaDoc instanceOrClass, String JavaDoc method,
133                              Serializable JavaDoc arg1)
134     {
135         Object JavaDoc result = null;
136         try
137         {
138             ScarabCacheKey key =
139                 (ScarabCacheKey)poolService.getInstance(keyClass);
140             key.init(instanceOrClass, method, arg1);
141             result = getMapImpl().get(key);
142         }
143         catch (Exception JavaDoc e)
144         {
145             Log.get().error(e);
146         }
147         return result;
148     }
149
150     public Object JavaDoc getImpl(Serializable JavaDoc instanceOrClass, String JavaDoc method,
151                              Serializable JavaDoc arg1, Serializable JavaDoc arg2)
152     {
153         Object JavaDoc result = null;
154         try
155         {
156             ScarabCacheKey key =
157                 (ScarabCacheKey)poolService.getInstance(keyClass);
158             key.init(instanceOrClass, method, arg1, arg2);
159             result = getMapImpl().get(key);
160         }
161         catch (Exception JavaDoc e)
162         {
163             Log.get().error(e);
164         }
165         return result;
166     }
167
168     public Object JavaDoc getImpl(Serializable JavaDoc instanceOrClass, String JavaDoc method,
169                              Serializable JavaDoc arg1, Serializable JavaDoc arg2,
170                              Serializable JavaDoc arg3)
171     {
172         Object JavaDoc result = null;
173         try
174         {
175             ScarabCacheKey key =
176                 (ScarabCacheKey)poolService.getInstance(keyClass);
177             key.init(instanceOrClass, method, arg1, arg2, arg3);
178             result = getMapImpl().get(key);
179         }
180         catch (Exception JavaDoc e)
181         {
182             Log.get().error(e);
183         }
184         return result;
185     }
186
187     public Object JavaDoc getImpl(Serializable JavaDoc[] keys)
188     {
189         Object JavaDoc result = null;
190         try
191         {
192             ScarabCacheKey key =
193                 (ScarabCacheKey)poolService.getInstance(keyClass);
194             key.init(keys);
195             result = getMapImpl().get(key);
196         }
197         catch (Exception JavaDoc e)
198         {
199             Log.get().error(e);
200         }
201         return result;
202     }
203
204     public void putImpl(Object JavaDoc value, Serializable JavaDoc instanceOrClass,
205                            String JavaDoc method)
206     {
207         try
208         {
209             ScarabCacheKey key =
210                 (ScarabCacheKey)poolService.getInstance(keyClass);
211             key.init(instanceOrClass, method);
212             getMapImpl().put(key, value);
213         }
214         catch (Exception JavaDoc e)
215         {
216             Log.get().error(e);
217         }
218     }
219
220     public void putImpl(Object JavaDoc value, Serializable JavaDoc instanceOrClass,
221                            String JavaDoc method, Serializable JavaDoc arg1)
222     {
223         try
224         {
225             ScarabCacheKey key =
226                 (ScarabCacheKey)poolService.getInstance(keyClass);
227             key.init(instanceOrClass, method, arg1);
228             getMapImpl().put(key, value);
229         }
230         catch (Exception JavaDoc e)
231         {
232             Log.get().error(e);
233         }
234     }
235
236     public void putImpl(Object JavaDoc value, Serializable JavaDoc instanceOrClass,
237                            String JavaDoc method, Serializable JavaDoc arg1, Serializable JavaDoc arg2)
238     {
239         try
240         {
241             ScarabCacheKey key =
242                 (ScarabCacheKey)poolService.getInstance(keyClass);
243             key.init(instanceOrClass, method, arg1, arg2);
244             getMapImpl().put(key, value);
245         }
246         catch (Exception JavaDoc e)
247         {
248             Log.get().error(e);
249         }
250     }
251
252     public void putImpl(Object JavaDoc value, Serializable JavaDoc instanceOrClass,
253                            String JavaDoc method, Serializable JavaDoc arg1, Serializable JavaDoc arg2,
254                            Serializable JavaDoc arg3)
255     {
256         try
257         {
258             ScarabCacheKey key =
259                 (ScarabCacheKey)poolService.getInstance(keyClass);
260             key.init(instanceOrClass, method, arg1, arg2, arg3);
261             getMapImpl().put(key, value);
262         }
263         catch (Exception JavaDoc e)
264         {
265             Log.get().error(e);
266         }
267     }
268
269     public void putImpl(Object JavaDoc value, Serializable JavaDoc[] keys)
270     {
271         try
272         {
273             ScarabCacheKey key =
274                 (ScarabCacheKey)poolService.getInstance(keyClass);
275             key.init(keys);
276             getMapImpl().put(key, value);
277         }
278         catch (Exception JavaDoc e)
279         {
280             Log.get().error(e);
281         }
282     }
283
284     
285     /**
286      * Avalon component lifecycle method
287      * @avalon.dependency type="org.apache.fulcrum.factory.FactoryService"
288      */

289     public void service(ServiceManager manager)
290     {
291         this.manager = manager;
292     }
293
294     /**
295      * Avalon component lifecycle method
296      * Initializes the service by loading default class loaders
297      * and customized object factories.
298      *
299      * @throws InitializationException if initialization fails.
300      */

301     public void initialize() throws Exception JavaDoc
302     {
303         try
304         {
305             poolService = (PoolService) manager.lookup(PoolService.ROLE);
306         }
307         catch (Exception JavaDoc e)
308         {
309             throw new ScarabRuntimeException(
310                L10NKeySet.ExceptionScarabCacheService, e);
311         }
312         maps = new WeakHashMap JavaDoc();
313         try
314         {
315             keyClass = Class
316                 .forName("org.tigris.scarab.services.cache.ScarabCacheKey");
317         }
318         catch (Exception JavaDoc x)
319         {
320             throw new InitializationException(
321                 "Failed to initialize ScarabCache",x); //EXCEPTION
322
}
323         
324     }
325 }
326
Popular Tags