KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > hibernate > WhirlycachePlugin


1 /*
2  * Created on Dec 19, 2004 by pjacob
3  *
4  */

5 package com.whirlycott.cache.hibernate;
6
7 import net.sf.hibernate.cache.Timestamper;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import com.whirlycott.cache.Cache;
13 import com.whirlycott.cache.CacheException;
14 import com.whirlycott.cache.CacheManager;
15 import com.whirlycott.cache.Messages;
16
17 /**
18  * @author pjacob
19  *
20  */

21 public class WhirlycachePlugin implements net.sf.hibernate.cache.Cache {
22
23     /**
24      * Logger.
25      */

26     private static final Log log = LogFactory.getLog(WhirlycachePlugin.class);
27
28     /**
29      * Number of milliseconds in 1 minute.
30      */

31     private static final int MS_PER_MINUTE = 60000;
32
33     /**
34      * Reference to the Whirlycache that we are going to use.
35      */

36     private Cache cache;
37
38     /**
39      * Name of the cache we're using.
40      */

41     private final String JavaDoc cacheName;
42
43     /**
44      *
45      * @param _name
46      */

47     public WhirlycachePlugin(final String JavaDoc _name)
48             throws net.sf.hibernate.cache.CacheException {
49         super();
50
51         //Short circuit if there's any nonsense.
52
if (_name == null)
53             throw new IllegalArgumentException JavaDoc(Messages.getString("WhirlycachePlugin.cannot_lookup_cache_with_null_name")); //$NON-NLS-1$
54

55         //Store the cache name away for using with the destroy() method.
56
cacheName = _name;
57
58         try {
59             cache = CacheManager.getInstance().getCache(_name);
60         } catch (final CacheException e) {
61             //Rethrow this whirlycache-specific exception as a hibernate exception.
62
throw new net.sf.hibernate.cache.CacheException(e.getMessage());
63         }
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see net.sf.hibernate.cache.Cache#clear()
70      */

71     public void clear() throws net.sf.hibernate.cache.CacheException {
72         cache.clear();
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see net.sf.hibernate.cache.Cache#destroy()
79      */

80     public void destroy() throws net.sf.hibernate.cache.CacheException {
81         try {
82             CacheManager.getInstance().destroy(cacheName);
83         } catch (final CacheException e) {
84             log.error(e.getMessage(), e);
85         }
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see net.sf.hibernate.cache.Cache#get(java.lang.Object)
92      */

93     public Object JavaDoc get(final Object JavaDoc _key) throws net.sf.hibernate.cache.CacheException {
94         return cache.retrieve(_key);
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see net.sf.hibernate.cache.Cache#getTimeout()
101      */

102     public int getTimeout() {
103         return Timestamper.ONE_MS * MS_PER_MINUTE;
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see net.sf.hibernate.cache.Cache#lock(java.lang.Object)
110      */

111     public void lock(final Object JavaDoc arg0) throws net.sf.hibernate.cache.CacheException {
112         return;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see net.sf.hibernate.cache.Cache#nextTimestamp()
119      */

120     public long nextTimestamp() {
121         return Timestamper.next();
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see net.sf.hibernate.cache.Cache#put(java.lang.Object, java.lang.Object)
128      */

129     public void put(final Object JavaDoc _key, final Object JavaDoc _val) throws net.sf.hibernate.cache.CacheException {
130         cache.store(_key, _val);
131     }
132
133     /*
134      * (non-Javadoc)
135      *
136      * @see net.sf.hibernate.cache.Cache#remove(java.lang.Object)
137      */

138     public void remove(final Object JavaDoc _key) throws net.sf.hibernate.cache.CacheException {
139         cache.remove(_key);
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see net.sf.hibernate.cache.Cache#unlock(java.lang.Object)
146      */

147     public void unlock(final Object JavaDoc arg0) throws net.sf.hibernate.cache.CacheException {
148         return;
149     }
150
151 }
152
153
Popular Tags