01: /**
02: * Copyright 2003-2007 Luck Consulting Pty Ltd
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package net.sf.ehcache.exceptionhandler;
16:
17: import net.sf.ehcache.Ehcache;
18:
19: import java.util.List;
20: import java.util.ArrayList;
21:
22: /**
23: * A test handler, used to test the Exception handling mechanism
24: * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
25: * @version $Id: CountingExceptionHandler.java 529 2007-08-05 09:11:32Z gregluck $
26: */
27: public class CountingExceptionHandler implements CacheExceptionHandler {
28:
29: /**
30: * the list of handled exceptions, static so you can get them without a reference
31: */
32: public static final List HANDLED_EXCEPTIONS = new ArrayList();
33:
34: /**
35: * Called if an Exception occurs in a Cache method. This method is not called
36: * if an <code>Error</code> occurs.
37: *
38: * @param ehcache the cache in which the Exception occurred
39: * @param key the key used in the operation, or null if the operation does not use a key
40: * @param exception the exception caught
41: */
42: public void onException(Ehcache ehcache, Object key,
43: Exception exception) {
44:
45: HandledException handledException = new HandledException(
46: ehcache, key, exception);
47: HANDLED_EXCEPTIONS.add(handledException);
48: }
49:
50: /**
51: * Clear counter
52: */
53: public static void resetCounters() {
54: HANDLED_EXCEPTIONS.clear();
55: }
56:
57: /**
58: * A value object for each exception handled
59: */
60: public static class HandledException {
61: private final Ehcache ehcache;
62: private final Object key;
63: private final Exception exception;
64:
65: /**
66: * Constructor
67: * @param ehcache
68: * @param key
69: * @param exception
70: */
71: public HandledException(Ehcache ehcache, Object key,
72: Exception exception) {
73: this .ehcache = ehcache;
74: this .key = key;
75: this .exception = exception;
76: }
77:
78: /**
79: * @return
80: */
81: public Object getKey() {
82: return key;
83: }
84:
85: /**
86: *
87: * @return underlying exception
88: */
89: public Exception getException() {
90: return exception;
91: }
92: }
93:
94: }
|