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.strategy;
17  
18  import net.sf.ehcache.Ehcache;
19  import net.sf.ehcache.Element;
20  import net.sf.ehcache.hibernate.regions.EhcacheEntityRegion;
21  import org.hibernate.cache.CacheException;
22  import org.hibernate.cache.EntityRegion;
23  import org.hibernate.cache.access.EntityRegionAccessStrategy;
24  import org.hibernate.cache.access.SoftLock;
25  import org.hibernate.cfg.Settings;
26  
27  /***
28   * JTA EntityRegionAccessStrategy.
29   *
30   * @author Chris Dennis
31   * @author Ludovic Orban
32   */
33  public class TransactionalEhcacheEntityRegionAccessStrategy extends AbstractEhcacheAccessStrategy<EhcacheEntityRegion>
34          implements EntityRegionAccessStrategy {
35  
36      private final Ehcache ehcache;
37  
38      /***
39       * Construct a new entity region access strategy.
40       * @param region the Hibernate region.
41       * @param ehcache the cache.
42       * @param settings the Hibernate settings.
43       */
44      public TransactionalEhcacheEntityRegionAccessStrategy(EhcacheEntityRegion region, Ehcache ehcache, Settings settings) {
45          super(region, settings);
46          this.ehcache = ehcache;
47      }
48  
49      /***
50       * {@inheritDoc}
51       */
52      public boolean afterInsert(Object key, Object value, Object version) {
53          return false;
54      }
55  
56      /***
57       * {@inheritDoc}
58       */
59      public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) {
60          return false;
61      }
62  
63      /***
64       * {@inheritDoc}
65       */
66      public Object get(Object key, long txTimestamp) throws CacheException {
67          try {
68              Element element = ehcache.get(key);
69              return element == null ? null : element.getObjectValue();
70          } catch (net.sf.ehcache.CacheException e) {
71              throw new CacheException(e);
72          }
73      }
74  
75      /***
76       * {@inheritDoc}
77       */
78      public EntityRegion getRegion() {
79          return region;
80      }
81  
82      /***
83       * {@inheritDoc}
84       */
85      public boolean insert(Object key, Object value, Object version)
86              throws CacheException {
87          //OptimisticCache? versioning?
88          try {
89              ehcache.put(new Element(key, value));
90              return true;
91          } catch (net.sf.ehcache.CacheException e) {
92              throw new CacheException(e);
93          }
94      }
95  
96      /***
97       * {@inheritDoc}
98       */
99      public SoftLock lockItem(Object key, Object version) throws CacheException {
100         return null;
101     }
102 
103     /***
104      * {@inheritDoc}
105      */
106     public boolean putFromLoad(Object key, Object value, long txTimestamp,
107                                Object version, boolean minimalPutOverride) throws CacheException {
108         try {
109             if (minimalPutOverride && ehcache.get(key) != null) {
110                 return false;
111             }
112             //OptimisticCache? versioning?
113             ehcache.put(new Element(key, value));
114             return true;
115         } catch (net.sf.ehcache.CacheException e) {
116             throw new CacheException(e);
117         }
118     }
119 
120     /***
121      * {@inheritDoc}
122      */
123     @Override
124     public void remove(Object key) throws CacheException {
125         try {
126             ehcache.remove(key);
127         } catch (net.sf.ehcache.CacheException e) {
128             throw new CacheException(e);
129         }
130     }
131 
132     /***
133      * {@inheritDoc}
134      */
135     public void unlockItem(Object key, SoftLock lock) throws CacheException {
136         // no-op
137     }
138 
139     /***
140      * {@inheritDoc}
141      */
142     public boolean update(Object key, Object value, Object currentVersion,
143                           Object previousVersion) throws CacheException {
144         try {
145             ehcache.put(new Element(key, value));
146             return true;
147         } catch (net.sf.ehcache.CacheException e) {
148             throw new CacheException(e);
149         }
150     }
151 }