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 java.util.Comparator;
19  
20  import org.hibernate.cache.CacheException;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import org.hibernate.cache.access.SoftLock;
26  
27  /***
28   * Ehcache specific read-only cache concurrency strategy.
29   * <p>
30   * This is the Ehcache specific equivalent to Hibernate's ReadOnlyCache.
31   *
32   * @author Chris Dennis
33   */
34  @Deprecated
35  public class EhcacheReadOnlyCache extends AbstractEhcacheConcurrencyStrategy {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(EhcacheReadOnlyCache.class);
38  
39      /***
40       * {@inheritDoc}
41       */
42      public Object get(Object key, long timestamp) throws CacheException {
43          return cache.get(key);
44      }
45  
46      /***
47       * Throws UnsupportedOperationException since items in a read-only cache should not be mutated.
48       *
49       * @throws UnsupportedOperationException always
50       */
51      public SoftLock lock(Object key, Object version) throws UnsupportedOperationException {
52          LOG.error("Application attempted to edit read only item: " + key);
53          throw new UnsupportedOperationException("Can't write to a readonly object");
54      }
55  
56      /***
57       * {@inheritDoc}
58       */
59      public boolean put(Object key, Object value, long timestamp, Object version,
60              Comparator versionComparator, boolean minimalPut) throws CacheException {
61          if (minimalPut && cache.get(key) != null) {
62              return false;
63          } else {
64              cache.put(key, value);
65              return true;
66          }
67      }
68  
69      /***
70       * Logs an error since items in a read-only cache should not be mutated.
71       */
72      public void release(Object key, SoftLock lock) {
73          LOG.error("Application attempted to edit read only item: " + key);
74          //throw new UnsupportedOperationException("Can't write to a readonly object");
75      }
76  
77      /***
78       * Throws UnsupportedOperationException since items in a read-only cache should not be mutated.
79       *
80       * @throws UnsupportedOperationException always
81       */
82      public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws UnsupportedOperationException {
83          LOG.error("Application attempted to edit read only item: " + key);
84          throw new UnsupportedOperationException("Can't write to a readonly object");
85      }
86  
87      /***
88       * Inserts the specified item into the cache.
89       */
90      public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
91          cache.update(key, value);
92          return true;
93      }
94  
95      /***
96       * A No-Op, since we are an asynchronous cache concurrency strategy.
97       */
98      public void evict(Object key) throws CacheException {
99      }
100 
101     /***
102      * A No-Op, since we are an asynchronous cache concurrency strategy.
103      */
104     public boolean insert(Object key, Object value, Object currentVersion) {
105         return false;
106     }
107 
108     /***
109      * Throws UnsupportedOperationException since items in a read-only cache should not be mutated.
110      *
111      * @throws UnsupportedOperationException always
112      */
113     public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws UnsupportedOperationException  {
114         LOG.error("Application attempted to edit read only item: " + key);
115         throw new UnsupportedOperationException("Can't write to a readonly object");
116     }
117 
118     /***
119      * {@inheritDoc}
120      */
121     @Override
122     public String toString() {
123         return cache + "(read-only)";
124     }
125 }