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.hibernate.regions.EhcacheCollectionRegion;
19  
20  import org.hibernate.cache.CacheException;
21  import org.hibernate.cache.CollectionRegion;
22  import org.hibernate.cache.access.CollectionRegionAccessStrategy;
23  import org.hibernate.cache.access.SoftLock;
24  import org.hibernate.cfg.Settings;
25  
26  /***
27   * Ehcache specific non-strict read/write collection region access strategy
28   *
29   * @author Chris Dennis
30   */
31  public class NonStrictReadWriteEhcacheCollectionRegionAccessStrategy extends AbstractEhcacheAccessStrategy<EhcacheCollectionRegion>
32          implements CollectionRegionAccessStrategy {
33  
34      /***
35       * Create a non-strict read/write access strategy accessing the given collection region.
36       */
37      public NonStrictReadWriteEhcacheCollectionRegionAccessStrategy(EhcacheCollectionRegion region, Settings settings) {
38          super(region, settings);
39      }
40  
41      /***
42       * {@inheritDoc}
43       */
44      public CollectionRegion getRegion() {
45          return region;
46      }
47  
48      /***
49       * {@inheritDoc}
50       */
51      public Object get(Object key, long txTimestamp) throws CacheException {
52          return region.get(key);
53      }
54  
55      /***
56       * {@inheritDoc}
57       */
58      public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
59              throws CacheException {
60          if (minimalPutOverride && region.contains(key)) {
61              return false;
62          } else {
63              region.put(key, value);
64              return true;
65          }
66      }
67  
68      /***
69       * Since this is a non-strict read/write strategy item locking is not used.
70       */
71      public SoftLock lockItem(Object key, Object version) throws CacheException {
72          return null;
73      }
74  
75      /***
76       * Since this is a non-strict read/write strategy item locking is not used.
77       */
78      public void unlockItem(Object key, SoftLock lock) throws CacheException {
79          region.remove(key);
80      }
81  
82      /***
83       * {@inheritDoc}
84       */
85      @Override
86      public void remove(Object key) throws CacheException {
87          region.remove(key);
88      }
89  }