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  package net.sf.ehcache.hibernate.ccs;
17  
18  import net.sf.ehcache.hibernate.EhCache;
19  import org.hibernate.cache.Cache;
20  import org.hibernate.cache.CacheConcurrencyStrategy;
21  import org.hibernate.cache.CacheException;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  /***
26   * Superclass of all Ehcache specific cache concurrency strategies.
27   *
28   * @author Chris Dennis
29   */
30  @Deprecated
31  abstract class AbstractEhcacheConcurrencyStrategy implements CacheConcurrencyStrategy {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(EhcacheReadOnlyCache.class);
34  
35      /***
36       * Ehcache instance this strategy accesses.
37       */
38      protected EhCache cache;
39  
40      /***
41       * {@inheritDoc}
42       * 
43       * @throws CacheException if the underlying cache is not an Ehcache
44       */
45      public final void setCache(Cache cache) throws CacheException {
46          if (cache instanceof EhCache) {
47              this.cache = (EhCache) cache;
48          } else {
49              throw new CacheException("Ehcache concurrency strategies must be used with Ehcache caches");
50          }
51      }
52  
53      /***
54       * {@inheritDoc}
55       */
56      public final Cache getCache() {
57          return cache;
58      }
59  
60      /***
61       * {@inheritDoc}
62       */
63      public final String getRegionName() {
64          return cache.getRegionName();
65      }
66  
67      /***
68       * {@inheritDoc}
69       */
70      public final void remove(Object key) throws CacheException {
71          cache.remove(key);
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public final void clear() throws CacheException {
78          cache.clear();
79      }
80  
81      /***
82       * {@inheritDoc}
83       */
84      public final void destroy() {
85          try {
86              cache.destroy();
87          } catch (Exception e) {
88              LOG.warn("could not destroy cache", e);
89          }
90      }
91  }