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.regions;
17  
18  import java.util.Properties;
19  
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.Element;
22  import net.sf.ehcache.constructs.nonstop.NonStopCacheException;
23  import net.sf.ehcache.hibernate.nonstop.HibernateNonstopCacheExceptionHandler;
24  import net.sf.ehcache.hibernate.strategy.EhcacheAccessStrategyFactory;
25  
26  import org.hibernate.cache.CacheException;
27  import org.hibernate.cache.GeneralDataRegion;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /***
32   * An Ehcache specific GeneralDataRegion.
33   * <p>
34   * GeneralDataRegion instances are used for both the timestamps and query caches.
35   *
36   * @author Chris Dennis
37   * @author Greg Luck
38   * @author Emmanuel Bernard
39   * @author Abhishek Sanoujam
40   */
41  abstract class EhcacheGeneralDataRegion extends EhcacheDataRegion implements GeneralDataRegion {
42  
43      private static final Logger LOG = LoggerFactory.getLogger(EhcacheGeneralDataRegion.class);
44  
45      /***
46       * Creates an EhcacheGeneralDataRegion using the given Ehcache instance as a backing.
47       */
48      public EhcacheGeneralDataRegion(EhcacheAccessStrategyFactory accessStrategyFactory, Ehcache cache, Properties properties) {
49          super(accessStrategyFactory, cache, properties);
50      }
51  
52      /***
53       * {@inheritDoc}
54       */
55      public Object get(Object key) throws CacheException {
56          try {
57              LOG.debug("key: {}", key);
58              if (key == null) {
59                  return null;
60              } else {
61                  Element element = cache.get(key);
62                  if (element == null) {
63                      LOG.debug("Element for key {} is null", key);
64                      return null;
65                  } else {
66                      return element.getObjectValue();
67                  }
68              }
69          } catch (net.sf.ehcache.CacheException e) {
70              if (e instanceof NonStopCacheException) {
71                  HibernateNonstopCacheExceptionHandler.getInstance().handleNonstopCacheException((NonStopCacheException) e);
72                  return null;
73              } else {
74                  throw new CacheException(e);
75              }
76          }
77      }
78  
79      /***
80       * {@inheritDoc}
81       */
82      public void put(Object key, Object value) throws CacheException {
83          LOG.debug("key: {} value: {}", key, value);
84          try {
85              Element element = new Element(key, value);
86              cache.put(element);
87          } catch (IllegalArgumentException e) {
88              throw new CacheException(e);
89          } catch (IllegalStateException e) {
90              throw new CacheException(e);
91          } catch (net.sf.ehcache.CacheException e) {
92              if (e instanceof NonStopCacheException) {
93                  HibernateNonstopCacheExceptionHandler.getInstance().handleNonstopCacheException((NonStopCacheException) e);
94              } else {
95                  throw new CacheException(e);
96              }
97          }
98      }
99  
100     /***
101      * {@inheritDoc}
102      */
103     public void evict(Object key) throws CacheException {
104         try {
105             cache.remove(key);
106         } catch (ClassCastException e) {
107             throw new CacheException(e);
108         } catch (IllegalStateException e) {
109             throw new CacheException(e);
110         } catch (net.sf.ehcache.CacheException e) {
111             if (e instanceof NonStopCacheException) {
112                 HibernateNonstopCacheExceptionHandler.getInstance().handleNonstopCacheException((NonStopCacheException) e);
113             } else {
114                 throw new CacheException(e);
115             }
116         }
117     }
118 
119     /***
120      * {@inheritDoc}
121      */
122     public void evictAll() throws CacheException {
123         try {
124             cache.removeAll();
125         } catch (IllegalStateException e) {
126             throw new CacheException(e);
127         } catch (net.sf.ehcache.CacheException e) {
128             if (e instanceof NonStopCacheException) {
129                 HibernateNonstopCacheExceptionHandler.getInstance().handleNonstopCacheException((NonStopCacheException) e);
130             } else {
131                 throw new CacheException(e);
132             }
133         }
134     }
135 }