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.EhcacheCollectionRegion;
21  import org.hibernate.cache.CacheException;
22  import org.hibernate.cache.CollectionRegion;
23  import org.hibernate.cache.access.CollectionRegionAccessStrategy;
24  import org.hibernate.cache.access.SoftLock;
25  import org.hibernate.cfg.Settings;
26  
27  /***
28   * JTA CollectionRegionAccessStrategy.
29   *
30   * @author Chris Dennis
31   * @author Ludovic Orban
32   */
33  public class TransactionalEhcacheCollectionRegionAccessStrategy extends AbstractEhcacheAccessStrategy<EhcacheCollectionRegion>
34          implements CollectionRegionAccessStrategy {
35  
36      private final Ehcache ehcache;
37  
38      /***
39       * Construct a new collection region access strategy.
40       * @param region the Hibernate region.
41       * @param ehcache the cache.
42       * @param settings the Hibernate settings.
43       */
44      public TransactionalEhcacheCollectionRegionAccessStrategy(EhcacheCollectionRegion region, Ehcache ehcache, Settings settings) {
45          super(region, settings);
46          this.ehcache = ehcache;
47      }
48  
49  
50  
51      /***
52       * {@inheritDoc}
53       */
54      public Object get(Object key, long txTimestamp) throws CacheException {
55          try {
56              Element element = ehcache.get(key);
57              return element == null ? null : element.getObjectValue();
58          } catch (net.sf.ehcache.CacheException e) {
59              throw new CacheException(e);
60          }
61      }
62  
63      /***
64       * {@inheritDoc}
65       */
66      public CollectionRegion getRegion() {
67          return region;
68      }
69  
70      /***
71       * {@inheritDoc}
72       */
73      public SoftLock lockItem(Object key, Object version) throws CacheException {
74          return null;
75      }
76  
77      /***
78       * {@inheritDoc}
79       */
80      public boolean putFromLoad(Object key, Object value, long txTimestamp,
81                                 Object version, boolean minimalPutOverride) throws CacheException {
82          try {
83              if (minimalPutOverride && ehcache.get(key) != null) {
84                  return false;
85              }
86              //OptimisticCache? versioning?
87              ehcache.put(new Element(key, value));
88              return true;
89          } catch (net.sf.ehcache.CacheException e) {
90              throw new CacheException(e);
91          }
92      }
93  
94      /***
95       * {@inheritDoc}
96       */
97      @Override
98      public void remove(Object key) throws CacheException {
99          try {
100             ehcache.remove(key);
101         } catch (net.sf.ehcache.CacheException e) {
102             throw new CacheException(e);
103         }
104     }
105 
106     /***
107      * {@inheritDoc}
108      */
109     public void unlockItem(Object key, SoftLock lock) throws CacheException {
110         // no-op
111     }
112 
113 }