001: /*
002: * Created on Dec 19, 2004 by pjacob
003: *
004: */
005: package com.whirlycott.cache.hibernate;
006:
007: import net.sf.hibernate.cache.Timestamper;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import com.whirlycott.cache.Cache;
013: import com.whirlycott.cache.CacheException;
014: import com.whirlycott.cache.CacheManager;
015: import com.whirlycott.cache.Messages;
016:
017: /**
018: * @author pjacob
019: *
020: */
021: public class WhirlycachePlugin implements net.sf.hibernate.cache.Cache {
022:
023: /**
024: * Logger.
025: */
026: private static final Log log = LogFactory
027: .getLog(WhirlycachePlugin.class);
028:
029: /**
030: * Number of milliseconds in 1 minute.
031: */
032: private static final int MS_PER_MINUTE = 60000;
033:
034: /**
035: * Reference to the Whirlycache that we are going to use.
036: */
037: private Cache cache;
038:
039: /**
040: * Name of the cache we're using.
041: */
042: private final String cacheName;
043:
044: /**
045: *
046: * @param _name
047: */
048: public WhirlycachePlugin(final String _name)
049: throws net.sf.hibernate.cache.CacheException {
050: super ();
051:
052: //Short circuit if there's any nonsense.
053: if (_name == null)
054: throw new IllegalArgumentException(
055: Messages
056: .getString("WhirlycachePlugin.cannot_lookup_cache_with_null_name")); //$NON-NLS-1$
057:
058: //Store the cache name away for using with the destroy() method.
059: cacheName = _name;
060:
061: try {
062: cache = CacheManager.getInstance().getCache(_name);
063: } catch (final CacheException e) {
064: //Rethrow this whirlycache-specific exception as a hibernate exception.
065: throw new net.sf.hibernate.cache.CacheException(e
066: .getMessage());
067: }
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see net.sf.hibernate.cache.Cache#clear()
074: */
075: public void clear() throws net.sf.hibernate.cache.CacheException {
076: cache.clear();
077: }
078:
079: /*
080: * (non-Javadoc)
081: *
082: * @see net.sf.hibernate.cache.Cache#destroy()
083: */
084: public void destroy() throws net.sf.hibernate.cache.CacheException {
085: try {
086: CacheManager.getInstance().destroy(cacheName);
087: } catch (final CacheException e) {
088: log.error(e.getMessage(), e);
089: }
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see net.sf.hibernate.cache.Cache#get(java.lang.Object)
096: */
097: public Object get(final Object _key)
098: throws net.sf.hibernate.cache.CacheException {
099: return cache.retrieve(_key);
100: }
101:
102: /*
103: * (non-Javadoc)
104: *
105: * @see net.sf.hibernate.cache.Cache#getTimeout()
106: */
107: public int getTimeout() {
108: return Timestamper.ONE_MS * MS_PER_MINUTE;
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see net.sf.hibernate.cache.Cache#lock(java.lang.Object)
115: */
116: public void lock(final Object arg0)
117: throws net.sf.hibernate.cache.CacheException {
118: return;
119: }
120:
121: /*
122: * (non-Javadoc)
123: *
124: * @see net.sf.hibernate.cache.Cache#nextTimestamp()
125: */
126: public long nextTimestamp() {
127: return Timestamper.next();
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see net.sf.hibernate.cache.Cache#put(java.lang.Object, java.lang.Object)
134: */
135: public void put(final Object _key, final Object _val)
136: throws net.sf.hibernate.cache.CacheException {
137: cache.store(_key, _val);
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see net.sf.hibernate.cache.Cache#remove(java.lang.Object)
144: */
145: public void remove(final Object _key)
146: throws net.sf.hibernate.cache.CacheException {
147: cache.remove(_key);
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see net.sf.hibernate.cache.Cache#unlock(java.lang.Object)
154: */
155: public void unlock(final Object arg0)
156: throws net.sf.hibernate.cache.CacheException {
157: return;
158: }
159:
160: }
|