001: package com.opensymphony.oscache.hibernate;
002:
003: import java.util.Properties;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007:
008: import org.hibernate.cache.Cache;
009: import org.hibernate.cache.CacheException;
010: import org.hibernate.cache.CacheProvider;
011: import org.hibernate.cache.Timestamper;
012: import org.hibernate.util.StringHelper;
013:
014: import com.opensymphony.oscache.base.CacheEntry;
015: import com.opensymphony.oscache.base.Config;
016: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
017: import com.opensymphony.oscache.util.StringUtil;
018:
019: /**
020: * Cache provider plugin for Hibernate 3.2 and OpenSymphony OSCache 2.4.
021: * <p/>
022: * This implementation assumes that identifiers have well-behaved <tt>toString()</tt> methods.
023: * <p/>
024: * To enable OSCache for Hibernate's second level cache add the following line to Hibernate's configuration e.g. <code>hibernate.cfg.xml</code>):
025: * <code>hibernate.cache.provider_class=com.opensymphony.oscache.hibernate.OSCacheProvider</code>
026: * To configure a different configuration file use the following parameter in the Hibernate's configuration:
027: * <code>com.opensymphony.oscache.configurationResourceName=[path to oscache-hibernate.properties]</code>
028: *
029: * @version $Revision:$
030: */
031: public class OSCacheProvider implements CacheProvider {
032:
033: private static final Log LOG = LogFactory
034: .getLog(OSCacheProvider.class);
035:
036: /** In the Hibernate system property you can specify the location of the oscache configuration file name. */
037: public static final String OSCACHE_CONFIGURATION_RESOURCE_NAME = "com.opensymphony.oscache.configurationResourceName";
038:
039: /** The <tt>OSCache</tt> refresh period property suffix. */
040: public static final String OSCACHE_REFRESH_PERIOD = "refresh.period";
041:
042: /** The <tt>OSCache</tt> CRON expression property suffix. */
043: public static final String OSCACHE_CRON = "cron";
044:
045: private static GeneralCacheAdministrator cache;
046:
047: /**
048: * Builds a new {@link Cache} instance, and gets it's properties from the
049: * GeneralCacheAdministrator {@link GeneralCacheAdministrator}
050: * which reads the properties file (<code>oscache.properties</code>) in the start method:
051: * @see com.opensymphony.oscache.hibernate.OSCacheProvider#start(java.util.Properties)
052: *
053: * @param region the region of the cache
054: * @param properties not used
055: * @return the hibernate 2nd level cache
056: * @throws CacheException
057: *
058: * @see org.hibernate.cache.CacheProvider#buildCache(java.lang.String, java.util.Properties)
059: */
060: public Cache buildCache(String region, Properties properties)
061: throws CacheException {
062: if (cache != null) {
063: LOG.debug("building cache in OSCacheProvider...");
064:
065: String refreshPeriodString = cache.getProperty(StringHelper
066: .qualify(region, OSCACHE_REFRESH_PERIOD));
067: int refreshPeriod = refreshPeriodString == null ? CacheEntry.INDEFINITE_EXPIRY
068: : Integer.parseInt(refreshPeriodString.trim());
069:
070: String cron = cache.getProperty(StringHelper.qualify(
071: region, OSCACHE_CRON));
072:
073: return new OSCache(cache, refreshPeriod, cron, region);
074: }
075: throw new CacheException(
076: "OSCache was stopped or wasn't configured via method start.");
077: }
078:
079: /**
080: * @see org.hibernate.cache.CacheProvider#nextTimestamp()
081: */
082: public long nextTimestamp() {
083: return Timestamper.next();
084: }
085:
086: /**
087: * This method isn't documented in Hibernate:
088: * @see org.hibernate.cache.CacheProvider#isMinimalPutsEnabledByDefault()
089: */
090: public boolean isMinimalPutsEnabledByDefault() {
091: return false;
092: }
093:
094: /**
095: * @see org.hibernate.cache.CacheProvider#stop()
096: */
097: public void stop() {
098: if (cache != null) {
099: LOG.debug("Stopping OSCacheProvider...");
100: cache.destroy();
101: cache = null;
102: LOG.debug("OSCacheProvider stopped.");
103: }
104: }
105:
106: /**
107: * @see org.hibernate.cache.CacheProvider#start(java.util.Properties)
108: */
109: public void start(Properties hibernateSystemProperties)
110: throws CacheException {
111: if (cache == null) {
112: // construct the cache
113: LOG.debug("Starting OSCacheProvider...");
114: String configResourceName = null;
115: if (hibernateSystemProperties != null) {
116: configResourceName = (String) hibernateSystemProperties
117: .get(OSCACHE_CONFIGURATION_RESOURCE_NAME);
118: }
119: if (StringUtil.isEmpty(configResourceName)) {
120: cache = new GeneralCacheAdministrator();
121: } else {
122: Properties propertiesOSCache = Config.loadProperties(
123: configResourceName, this .getClass().getName());
124: cache = new GeneralCacheAdministrator(propertiesOSCache);
125: }
126: LOG.debug("OSCacheProvider started.");
127: } else {
128: LOG
129: .warn("Tried to restart OSCacheProvider, which is already running.");
130: }
131: }
132:
133: }
|