KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > ReadOnlyCache


1 //$Id: ReadOnlyCache.java,v 1.8 2005/03/16 06:01:16 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.util.Comparator JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 /**
10  * Caches data that is never updated.
11  * @see CacheConcurrencyStrategy
12  */

13 public class ReadOnlyCache implements CacheConcurrencyStrategy {
14     
15     private Cache cache;
16     private static final Log log = LogFactory.getLog(ReadOnlyCache.class);
17     
18     public ReadOnlyCache() {}
19     
20     public void setCache(Cache cache) {
21         this.cache=cache;
22     }
23
24     public Cache getCache() {
25         return cache;
26     }
27
28     public String JavaDoc getRegionName() {
29         return cache.getRegionName();
30     }
31     
32     public synchronized Object JavaDoc get(Object JavaDoc key, long timestamp) throws CacheException {
33         Object JavaDoc result = cache.get(key);
34         if ( result!=null && log.isDebugEnabled() ) log.debug("Cache hit: " + key);
35         return result;
36     }
37     
38     /**
39      * Unsupported!
40      */

41     public SoftLock lock(Object JavaDoc key, Object JavaDoc version) {
42         log.error("Application attempted to edit read only item: " + key);
43         throw new UnsupportedOperationException JavaDoc("Can't write to a readonly object");
44     }
45     
46     public synchronized boolean put(
47             Object JavaDoc key,
48             Object JavaDoc value,
49             long timestamp,
50             Object JavaDoc version,
51             Comparator JavaDoc versionComparator,
52             boolean minimalPut)
53     throws CacheException {
54         if ( minimalPut && cache.get(key)!=null ) {
55             if ( log.isDebugEnabled() ) log.debug("item already cached: " + key);
56             return false;
57         }
58         if ( log.isDebugEnabled() ) log.debug("Caching: " + key);
59         cache.put(key, value);
60         return true;
61     }
62     
63     /**
64      * Unsupported!
65      */

66     public void release(Object JavaDoc key, SoftLock lock) {
67         log.error("Application attempted to edit read only item: " + key);
68         //throw new UnsupportedOperationException("Can't write to a readonly object");
69
}
70     
71     public void clear() throws CacheException {
72         cache.clear();
73     }
74
75     public void remove(Object JavaDoc key) throws CacheException {
76         cache.remove(key);
77     }
78     
79     public void destroy() {
80         try {
81             cache.destroy();
82         }
83         catch (Exception JavaDoc e) {
84             log.warn("could not destroy cache", e);
85         }
86     }
87
88     /**
89      * Unsupported!
90      */

91     public boolean afterUpdate(Object JavaDoc key, Object JavaDoc value, Object JavaDoc version, SoftLock lock) throws CacheException {
92         log.error("Application attempted to edit read only item: " + key);
93         throw new UnsupportedOperationException JavaDoc("Can't write to a readonly object");
94     }
95
96     /**
97      * Do nothing.
98      */

99     public boolean afterInsert(Object JavaDoc key, Object JavaDoc value, Object JavaDoc version) throws CacheException {
100         if ( log.isDebugEnabled() ) log.debug("Caching after insert: " + key);
101         cache.update(key, value);
102         return true;
103     }
104
105     /**
106      * Do nothing.
107      */

108     public void evict(Object JavaDoc key) throws CacheException {
109         // noop
110
}
111
112     /**
113      * Do nothing.
114      */

115     public boolean insert(Object JavaDoc key, Object JavaDoc value) throws CacheException {
116         return false;
117     }
118
119     /**
120      * Unsupported!
121      */

122     public boolean update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
123         log.error("Application attempted to edit read only item: " + key);
124         throw new UnsupportedOperationException JavaDoc("Can't write to a readonly object");
125     }
126
127     public String JavaDoc toString() {
128         return cache + "(read-only)";
129     }
130
131 }
132
133
134
135
136
137
138
Popular Tags