001: package org.apache.torque.manager;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.Serializable;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.apache.commons.pool.ObjectPool;
027: import org.apache.commons.pool.impl.StackObjectPool;
028:
029: import org.apache.jcs.access.GroupCacheAccess;
030: import org.apache.jcs.access.exception.CacheException;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import org.apache.torque.TorqueException;
036:
037: /**
038: * This class provides a cache for convenient storage of method
039: * results.
040: *
041: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
042: * @version $Id: MethodResultCache.java 552334 2007-07-01 16:26:41Z tv $
043: */
044: public class MethodResultCache {
045: private ObjectPool pool;
046: private GroupCacheAccess jcsCache;
047: private Map groups;
048:
049: /** Logging */
050: private static Log log = LogFactory.getLog(MethodResultCache.class);
051:
052: public MethodResultCache(GroupCacheAccess cache)
053: throws TorqueException {
054: this .jcsCache = cache;
055: groups = new HashMap();
056: pool = new StackObjectPool(new MethodCacheKey.Factory(), 10000);
057: }
058:
059: /**
060: * Allows subclasses to have ctors that do not require a cache.
061: * This is used by NullMethodResultCache which has no-op versions
062: * of all methods.
063: */
064: protected MethodResultCache() {
065: }
066:
067: public void clear() {
068: if (jcsCache != null) {
069: try {
070: jcsCache.clear();
071: groups.clear();
072: } catch (CacheException ce) {
073: log
074: .error(new TorqueException(
075: "Could not clear cache due to internal JCS error.",
076: ce));
077: }
078: }
079: }
080:
081: protected Object getImpl(MethodCacheKey key) {
082: Object result = null;
083: if (jcsCache != null) {
084: synchronized (this ) {
085: result = jcsCache.getFromGroup(key, key.getGroupKey());
086: }
087: }
088:
089: if (result != null) {
090: if (log.isDebugEnabled()) {
091: log
092: .debug("MethodResultCache saved expensive operation: "
093: + key);
094: }
095: }
096: return result;
097: }
098:
099: protected Object putImpl(MethodCacheKey key, Object value)
100: throws TorqueException {
101: //register the group, if this is the first occurrence
102: String group = key.getGroupKey();
103: if (!groups.containsKey(group)) {
104: groups.put(group, null);
105: }
106:
107: Object old = null;
108: if (jcsCache != null) {
109: try {
110: synchronized (this ) {
111: old = jcsCache.getFromGroup(key, group);
112: jcsCache.putInGroup(key, group, value);
113: }
114: } catch (CacheException ce) {
115: throw new TorqueException(
116: "Could not cache due to internal JCS error", ce);
117: }
118: }
119: return old;
120: }
121:
122: protected Object removeImpl(MethodCacheKey key)
123: throws TorqueException {
124: Object old = null;
125: if (jcsCache != null) {
126: synchronized (this ) {
127: old = jcsCache.getFromGroup(key, key.getGroupKey());
128: jcsCache.remove(key, key.getGroupKey());
129: }
130: }
131: return old;
132: }
133:
134: public Object get(Serializable instanceOrClass, String method) {
135: Object result = null;
136: if (jcsCache != null) {
137: try {
138: MethodCacheKey key = (MethodCacheKey) pool
139: .borrowObject();
140: key.init(instanceOrClass, method);
141: result = getImpl(key);
142: try {
143: pool.returnObject(key);
144: } catch (Exception e) {
145: log
146: .warn(
147: "Nonfatal error. Could not return key to pool",
148: e);
149: }
150: } catch (Exception e) {
151: log.error("", e);
152: }
153: }
154: return result;
155: }
156:
157: public Object get(Serializable instanceOrClass, String method,
158: Serializable arg1) {
159: Object result = null;
160: if (jcsCache != null) {
161: try {
162: MethodCacheKey key = (MethodCacheKey) pool
163: .borrowObject();
164: key.init(instanceOrClass, method, arg1);
165: result = getImpl(key);
166: try {
167: pool.returnObject(key);
168: } catch (Exception e) {
169: log
170: .warn(
171: "Nonfatal error. Could not return key to pool",
172: e);
173: }
174: } catch (Exception e) {
175: log.error("", e);
176: }
177: }
178: return result;
179: }
180:
181: public Object get(Serializable instanceOrClass, String method,
182: Serializable arg1, Serializable arg2) {
183: Object result = null;
184: if (jcsCache != null) {
185: try {
186: MethodCacheKey key = (MethodCacheKey) pool
187: .borrowObject();
188: key.init(instanceOrClass, method, arg1, arg2);
189: result = getImpl(key);
190: try {
191: pool.returnObject(key);
192: } catch (Exception e) {
193: log
194: .warn(
195: "Nonfatal error. Could not return key to pool",
196: e);
197: }
198: } catch (Exception e) {
199: log.error("", e);
200: }
201: }
202: return result;
203: }
204:
205: public Object get(Serializable instanceOrClass, String method,
206: Serializable arg1, Serializable arg2, Serializable arg3) {
207: Object result = null;
208: if (jcsCache != null) {
209: try {
210: MethodCacheKey key = (MethodCacheKey) pool
211: .borrowObject();
212: key.init(instanceOrClass, method, arg1, arg2, arg3);
213: result = getImpl(key);
214: try {
215: pool.returnObject(key);
216: } catch (Exception e) {
217: log
218: .warn(
219: "Nonfatal error. Could not return key to pool",
220: e);
221: }
222: } catch (Exception e) {
223: log.error("", e);
224: }
225: }
226: return result;
227: }
228:
229: public Object get(Serializable[] keys) {
230: Object result = null;
231: if (jcsCache != null) {
232: try {
233: MethodCacheKey key = (MethodCacheKey) pool
234: .borrowObject();
235: key.init(keys);
236: result = getImpl(key);
237: try {
238: pool.returnObject(key);
239: } catch (Exception e) {
240: log
241: .warn(
242: "Nonfatal error. Could not return key to pool",
243: e);
244: }
245: } catch (Exception e) {
246: log.error("", e);
247: }
248: }
249: return result;
250: }
251:
252: public void put(Object value, Serializable instanceOrClass,
253: String method) {
254: try {
255: MethodCacheKey key = (MethodCacheKey) pool.borrowObject();
256: key.init(instanceOrClass, method);
257: putImpl(key, value);
258: } catch (Exception e) {
259: log.error("", e);
260: }
261: }
262:
263: public void put(Object value, Serializable instanceOrClass,
264: String method, Serializable arg1) {
265: try {
266: MethodCacheKey key = (MethodCacheKey) pool.borrowObject();
267: key.init(instanceOrClass, method, arg1);
268: putImpl(key, value);
269: } catch (Exception e) {
270: log.error("", e);
271: }
272: }
273:
274: public void put(Object value, Serializable instanceOrClass,
275: String method, Serializable arg1, Serializable arg2) {
276: try {
277: MethodCacheKey key = (MethodCacheKey) pool.borrowObject();
278: key.init(instanceOrClass, method, arg1, arg2);
279: putImpl(key, value);
280: } catch (Exception e) {
281: log.error("", e);
282: }
283: }
284:
285: public void put(Object value, Serializable instanceOrClass,
286: String method, Serializable arg1, Serializable arg2,
287: Serializable arg3) {
288: try {
289: MethodCacheKey key = (MethodCacheKey) pool.borrowObject();
290: key.init(instanceOrClass, method, arg1, arg2, arg3);
291: putImpl(key, value);
292: } catch (Exception e) {
293: log.error("", e);
294: }
295: }
296:
297: public void put(Object value, Serializable[] keys) {
298: try {
299: MethodCacheKey key = (MethodCacheKey) pool.borrowObject();
300: key.init(keys);
301: putImpl(key, value);
302: } catch (Exception e) {
303: log.error("", e);
304: }
305: }
306:
307: public void removeAll(Serializable instanceOrClass, String method) {
308: if (jcsCache != null) {
309: try {
310: MethodCacheKey key = (MethodCacheKey) pool
311: .borrowObject();
312: key.init(instanceOrClass, method);
313: String groupName = key.getGroupKey();
314: jcsCache.invalidateGroup(groupName);
315: groups.remove(groupName);
316: try {
317: pool.returnObject(key);
318: } catch (Exception e) {
319: log
320: .warn(
321: "Nonfatal error. Could not return key to pool",
322: e);
323: }
324: } catch (Exception e) {
325: log.error("", e);
326: }
327: }
328: }
329:
330: public Object remove(Serializable instanceOrClass, String method) {
331: Object result = null;
332: if (jcsCache != null) {
333: try {
334: MethodCacheKey key = (MethodCacheKey) pool
335: .borrowObject();
336: key.init(instanceOrClass, method);
337: result = removeImpl(key);
338: try {
339: pool.returnObject(key);
340: } catch (Exception e) {
341: log
342: .warn(
343: "Nonfatal error. Could not return key to pool",
344: e);
345: }
346: } catch (Exception e) {
347: log.error("", e);
348: }
349: }
350: return result;
351: }
352:
353: public Object remove(Serializable instanceOrClass, String method,
354: Serializable arg1) {
355: Object result = null;
356: if (jcsCache != null) {
357: try {
358: MethodCacheKey key = (MethodCacheKey) pool
359: .borrowObject();
360: key.init(instanceOrClass, method, arg1);
361: result = removeImpl(key);
362: try {
363: pool.returnObject(key);
364: } catch (Exception e) {
365: log
366: .warn(
367: "Nonfatal error. Could not return key to pool",
368: e);
369: }
370: } catch (Exception e) {
371: log.error("Error removing element", e);
372: }
373: }
374: return result;
375: }
376:
377: public Object remove(Serializable instanceOrClass, String method,
378: Serializable arg1, Serializable arg2) {
379: Object result = null;
380: if (jcsCache != null) {
381: try {
382: MethodCacheKey key = (MethodCacheKey) pool
383: .borrowObject();
384: key.init(instanceOrClass, method, arg1, arg2);
385: result = removeImpl(key);
386: try {
387: pool.returnObject(key);
388: } catch (Exception e) {
389: log
390: .warn(
391: "Nonfatal error: Could not return key to pool",
392: e);
393: }
394: } catch (Exception e) {
395: log.error("Error removing element from cache", e);
396: }
397: }
398: return result;
399: }
400:
401: public Object remove(Serializable instanceOrClass, String method,
402: Serializable arg1, Serializable arg2, Serializable arg3) {
403: Object result = null;
404: if (jcsCache != null) {
405: try {
406: MethodCacheKey key = (MethodCacheKey) pool
407: .borrowObject();
408: key.init(instanceOrClass, method, arg1, arg2, arg3);
409: result = removeImpl(key);
410: try {
411: pool.returnObject(key);
412: } catch (Exception e) {
413: log
414: .warn(
415: "Nonfatal error. Could not return key to pool",
416: e);
417: }
418: } catch (Exception e) {
419: log.error("Error removing element from cache", e);
420: }
421: }
422: return result;
423: }
424:
425: public Object remove(Serializable[] keys) {
426: Object result = null;
427: if (jcsCache != null) {
428: try {
429: MethodCacheKey key = (MethodCacheKey) pool
430: .borrowObject();
431: key.init(keys);
432: result = removeImpl(key);
433: try {
434: pool.returnObject(key);
435: } catch (Exception e) {
436: log
437: .warn(
438: "Nonfatal error: Could not return key to pool",
439: e);
440: }
441: } catch (Exception e) {
442: log.error("Error removing element from cache", e);
443: }
444: }
445: return result;
446: }
447: }
|