001: package org.tigris.scarab.services.cache;
002:
003: /* ================================================================
004: * Copyright (c) 2001 Collab.Net. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.io.Serializable;
050: import java.util.HashMap;
051: import java.util.Iterator;
052: import java.util.Map;
053: import java.util.WeakHashMap;
054:
055: import org.apache.avalon.framework.activity.Initializable;
056: import org.apache.avalon.framework.logger.AbstractLogEnabled;
057: import org.apache.avalon.framework.service.ServiceManager;
058: import org.apache.avalon.framework.service.Serviceable;
059: import org.apache.fulcrum.InitializationException;
060: import org.apache.fulcrum.pool.PoolService;
061: import org.tigris.scarab.tools.localization.L10NKeySet;
062: import org.tigris.scarab.util.Log;
063: import org.tigris.scarab.util.ScarabRuntimeException;
064:
065: /**
066: * This class provides a simple Map cache that is available to the
067: * current thread.
068: *
069: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
070: * @version $Id: DefaultScarabCacheService.java 9284 2004-12-02 21:13:20Z dabbous $
071: */
072: public class DefaultScarabCacheService extends AbstractLogEnabled
073: implements ScarabCacheService, Serviceable, Initializable {
074:
075: //private Configuration props;
076: private Map maps;
077: private Class keyClass;
078:
079: private PoolService poolService;
080: private ServiceManager manager;
081:
082: public DefaultScarabCacheService() {
083: }
084:
085: public Map getMapImpl() {
086: Thread t = Thread.currentThread();
087: Map map = (Map) maps.get(t);
088: if (map == null) {
089: map = new HashMap();
090: maps.put(t, map);
091: }
092:
093: return map;
094: }
095:
096: public void clearImpl() {
097: Map map = (Map) maps.get(Thread.currentThread());
098: if (map != null) {
099: Iterator i = map.keySet().iterator();
100: while (i.hasNext()) {
101: poolService.putInstance(i.next());
102: }
103: map.clear();
104: }
105: }
106:
107: public Object getImpl(Serializable instanceOrClass, String method) {
108: Object result = null;
109: try {
110: ScarabCacheKey key = (ScarabCacheKey) poolService
111: .getInstance(keyClass);
112: key.init(instanceOrClass, method);
113: result = getMapImpl().get(key);
114: } catch (Exception e) {
115: Log.get().error(e);
116: }
117: return result;
118: }
119:
120: public Object getImpl(Serializable instanceOrClass, String method,
121: Serializable arg1) {
122: Object result = null;
123: try {
124: ScarabCacheKey key = (ScarabCacheKey) poolService
125: .getInstance(keyClass);
126: key.init(instanceOrClass, method, arg1);
127: result = getMapImpl().get(key);
128: } catch (Exception e) {
129: Log.get().error(e);
130: }
131: return result;
132: }
133:
134: public Object getImpl(Serializable instanceOrClass, String method,
135: Serializable arg1, Serializable arg2) {
136: Object result = null;
137: try {
138: ScarabCacheKey key = (ScarabCacheKey) poolService
139: .getInstance(keyClass);
140: key.init(instanceOrClass, method, arg1, arg2);
141: result = getMapImpl().get(key);
142: } catch (Exception e) {
143: Log.get().error(e);
144: }
145: return result;
146: }
147:
148: public Object getImpl(Serializable instanceOrClass, String method,
149: Serializable arg1, Serializable arg2, Serializable arg3) {
150: Object result = null;
151: try {
152: ScarabCacheKey key = (ScarabCacheKey) poolService
153: .getInstance(keyClass);
154: key.init(instanceOrClass, method, arg1, arg2, arg3);
155: result = getMapImpl().get(key);
156: } catch (Exception e) {
157: Log.get().error(e);
158: }
159: return result;
160: }
161:
162: public Object getImpl(Serializable[] keys) {
163: Object result = null;
164: try {
165: ScarabCacheKey key = (ScarabCacheKey) poolService
166: .getInstance(keyClass);
167: key.init(keys);
168: result = getMapImpl().get(key);
169: } catch (Exception e) {
170: Log.get().error(e);
171: }
172: return result;
173: }
174:
175: public void putImpl(Object value, Serializable instanceOrClass,
176: String method) {
177: try {
178: ScarabCacheKey key = (ScarabCacheKey) poolService
179: .getInstance(keyClass);
180: key.init(instanceOrClass, method);
181: getMapImpl().put(key, value);
182: } catch (Exception e) {
183: Log.get().error(e);
184: }
185: }
186:
187: public void putImpl(Object value, Serializable instanceOrClass,
188: String method, Serializable arg1) {
189: try {
190: ScarabCacheKey key = (ScarabCacheKey) poolService
191: .getInstance(keyClass);
192: key.init(instanceOrClass, method, arg1);
193: getMapImpl().put(key, value);
194: } catch (Exception e) {
195: Log.get().error(e);
196: }
197: }
198:
199: public void putImpl(Object value, Serializable instanceOrClass,
200: String method, Serializable arg1, Serializable arg2) {
201: try {
202: ScarabCacheKey key = (ScarabCacheKey) poolService
203: .getInstance(keyClass);
204: key.init(instanceOrClass, method, arg1, arg2);
205: getMapImpl().put(key, value);
206: } catch (Exception e) {
207: Log.get().error(e);
208: }
209: }
210:
211: public void putImpl(Object value, Serializable instanceOrClass,
212: String method, Serializable arg1, Serializable arg2,
213: Serializable arg3) {
214: try {
215: ScarabCacheKey key = (ScarabCacheKey) poolService
216: .getInstance(keyClass);
217: key.init(instanceOrClass, method, arg1, arg2, arg3);
218: getMapImpl().put(key, value);
219: } catch (Exception e) {
220: Log.get().error(e);
221: }
222: }
223:
224: public void putImpl(Object value, Serializable[] keys) {
225: try {
226: ScarabCacheKey key = (ScarabCacheKey) poolService
227: .getInstance(keyClass);
228: key.init(keys);
229: getMapImpl().put(key, value);
230: } catch (Exception e) {
231: Log.get().error(e);
232: }
233: }
234:
235: /**
236: * Avalon component lifecycle method
237: * @avalon.dependency type="org.apache.fulcrum.factory.FactoryService"
238: */
239: public void service(ServiceManager manager) {
240: this .manager = manager;
241: }
242:
243: /**
244: * Avalon component lifecycle method
245: * Initializes the service by loading default class loaders
246: * and customized object factories.
247: *
248: * @throws InitializationException if initialization fails.
249: */
250: public void initialize() throws Exception {
251: try {
252: poolService = (PoolService) manager
253: .lookup(PoolService.ROLE);
254: } catch (Exception e) {
255: throw new ScarabRuntimeException(
256: L10NKeySet.ExceptionScarabCacheService, e);
257: }
258: maps = new WeakHashMap();
259: try {
260: keyClass = Class
261: .forName("org.tigris.scarab.services.cache.ScarabCacheKey");
262: } catch (Exception x) {
263: throw new InitializationException(
264: "Failed to initialize ScarabCache", x); //EXCEPTION
265: }
266:
267: }
268: }
|