View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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   */
16  
17  package net.sf.ehcache.exceptionhandler;
18  
19  import net.sf.ehcache.Ehcache;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /***
25   * A test handler, used to test the Exception handling mechanism
26   *
27   * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
28   * @version $Id: CountingExceptionHandler.html 13146 2011-08-01 17:12:39Z oletizi $
29   */
30  public class CountingExceptionHandler implements CacheExceptionHandler {
31  
32      /***
33       * the list of handled exceptions, static so you can get them without a reference
34       */
35      public static final List HANDLED_EXCEPTIONS = new ArrayList();
36  
37      /***
38       * Called if an Exception occurs in a Cache method. This method is not called
39       * if an <code>Error</code> occurs.
40       *
41       * @param ehcache   the cache in which the Exception occurred
42       * @param key       the key used in the operation, or null if the operation does not use a key
43       * @param exception the exception caught
44       */
45      public void onException(Ehcache ehcache, Object key, Exception exception) {
46  
47          HandledException handledException = new HandledException(ehcache, key, exception);
48          HANDLED_EXCEPTIONS.add(handledException);
49      }
50  
51      /***
52       * Clear counter
53       */
54      public static void resetCounters() {
55          HANDLED_EXCEPTIONS.clear();
56      }
57  
58      /***
59       * A value object for each exception handled
60       */
61      public static class HandledException {
62          private final Ehcache ehcache;
63          private final Object key;
64          private final Exception exception;
65  
66          /***
67           * Constructor
68           *
69           * @param ehcache
70           * @param key
71           * @param exception
72           */
73          public HandledException(Ehcache ehcache, Object key, Exception exception) {
74              this.ehcache = ehcache;
75              this.key = key;
76              this.exception = exception;
77          }
78  
79          /***
80           * @return
81           */
82          public Object getKey() {
83              return key;
84          }
85  
86          /***
87           * @return underlying exception
88           */
89          public Exception getException() {
90              return exception;
91          }
92      }
93  
94  
95  }