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.extension;
18  
19  import net.sf.ehcache.CacheException;
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.Element;
22  import net.sf.ehcache.Status;
23  
24  /***
25   * Tests the interface methods of CacheExtension
26   *
27   * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
28   * @version $Id: TestCacheExtension.html 13146 2011-08-01 17:12:39Z oletizi $
29   */
30  public class TestCacheExtension implements CacheExtension {
31  
32      /***
33       * Package local and static so we can test
34       */
35      private static Status status;
36  
37      /***
38       * Package local and static so we can test
39       */
40      private static String propertyA;
41  
42      /***
43       * Package local so we can test
44       */
45      private Ehcache cache;
46  
47  
48      /***
49       * Creates a cache extension. Note that an Ehcache is passed in. To do anything
50       * useful a CacheExtension needs a cache reference.
51       */
52      public TestCacheExtension(Ehcache cache, String propertyA) {
53          this.cache = cache;
54          TestCacheExtension.propertyA = propertyA;
55          status = Status.STATUS_UNINITIALISED;
56      }
57  
58  
59      /***
60       * Notifies providers to initialise themselves.
61       *
62       * @throws net.sf.ehcache.CacheException
63       */
64      public void init() {
65          status = Status.STATUS_ALIVE;
66          cache.put(new Element("key1", "value1"));
67  
68      }
69  
70      /***
71       * Providers may be doing all sorts of exotic things and need to be able to clean up on
72       * dispose.
73       *
74       * @throws net.sf.ehcache.CacheException
75       */
76      public void dispose() throws CacheException {
77          status = Status.STATUS_SHUTDOWN;
78      }
79  
80      /***
81       * @return
82       */
83      public Status getStatus() {
84          return status;
85      }
86  
87      /***
88       * @return
89       */
90      public static Status getStaticStatus() {
91          return status;
92      }
93  
94  
95      /***
96       * @return
97       */
98      public static String getPropertyA() {
99          return propertyA;
100     }
101 
102     /***
103      * @return
104      */
105     public Ehcache getCache() {
106         return cache;
107     }
108 
109     /***
110      * Creates a clone of this extension. This method will only be called by ehcache before a
111      * cache is initialized.
112      * <p/>
113      * Implementations should throw CloneNotSupportedException if they do not support clone but that
114      * will stop them from being used with defaultCache.
115      *
116      * @return a clone
117      * @throws CloneNotSupportedException if the extension could not be cloned.
118      */
119     public CacheExtension clone(Ehcache newCache) throws CloneNotSupportedException {
120         TestCacheExtension copy = new TestCacheExtension(newCache, propertyA);
121         return copy;
122     }
123 
124 
125 }