Clover coverage report - Coverage timestamp: Sa Jul 7 2007 09:11:40 CEST FRAMES NO FRAMES |
file stats: | LOC: | 123 |  | Methods: | 5
| NCLOC: | 62 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
OSCacheProvider.java | 0% | 0% | 0% | 0% |

1 |  | package com.opensymphony.oscache.hibernate;
2 |  |
3 |  | import java.util.Properties;
4 |  |
5 |  | import org.apache.commons.logging.Log;
6 |  | import org.apache.commons.logging.LogFactory;
7 |  |
8 |  | import org.hibernate.cache.Cache;
9 |  | import org.hibernate.cache.CacheException;
10 |  | import org.hibernate.cache.CacheProvider;
11 |  | import org.hibernate.cache.Timestamper;
12 |  | import org.hibernate.util.StringHelper;
13 |  |
14 |  | import com.opensymphony.oscache.base.CacheEntry;
15 |  | import com.opensymphony.oscache.base.Config;
16 |  | import com.opensymphony.oscache.general.GeneralCacheAdministrator;
17 |  | import com.opensymphony.oscache.util.StringUtil;
18 |  |
19 |  | /**
20 |  | * Cache provider plugin for Hibernate 3.2 and OpenSymphony OSCache 2.4.
21 |  | * <p/>
22 |  | * This implementation assumes that identifiers have well-behaved <tt>toString()</tt> methods.
23 |  | * <p/>
24 |  | * To enable OSCache for Hibernate's second level cache add the following line to Hibernate's configuration e.g. <code>hibernate.cfg.xml</code>):
25 |  | * <code>hibernate.cache.provider_class=com.opensymphony.oscache.hibernate.OSCacheProvider</code>
26 |  | * To configure a different configuration file use the following parameter in the Hibernate's configuration:
27 |  | * <code>com.opensymphony.oscache.configurationResourceName=[path to oscache-hibernate.properties]</code>
28 |  | *
29 |  | * @version $Revision:$
30 |  | */
31 |  | public class OSCacheProvider implements CacheProvider {
32 |  |
33 |  | private static final Log LOG = LogFactory.getLog(OSCacheProvider.class);
34 |  |
35 |  | /** In the Hibernate system property you can specify the location of the oscache configuration file name. */
36 |  | public static final String OSCACHE_CONFIGURATION_RESOURCE_NAME = "com.opensymphony.oscache.configurationResourceName";
37 |  |
38 |  | /** The <tt>OSCache</tt> refresh period property suffix. */
39 |  | public static final String OSCACHE_REFRESH_PERIOD = "refresh.period";
40 |  |
41 |  | /** The <tt>OSCache</tt> CRON expression property suffix. */
42 |  | public static final String OSCACHE_CRON = "cron";
43 |  |
44 |  | private static GeneralCacheAdministrator cache;
45 |  |
46 |  | /**
47 |  | * Builds a new {@link Cache} instance, and gets it's properties from the
48 |  | * GeneralCacheAdministrator {@link GeneralCacheAdministrator}
49 |  | * which reads the properties file (<code>oscache.properties</code>) in the start method:
50 |  | * @see com.opensymphony.oscache.hibernate.OSCacheProvider#start(java.util.Properties)
51 |  | *
52 |  | * @param region the region of the cache
53 |  | * @param properties not used
54 |  | * @return the hibernate 2nd level cache
55 |  | * @throws CacheException
56 |  | *
57 |  | * @see org.hibernate.cache.CacheProvider#buildCache(java.lang.String, java.util.Properties)
58 |  | */
59 | 0 | public Cache buildCache(String region, Properties properties) throws CacheException {
60 | 0 | if (cache != null) {
61 | 0 | LOG.debug("building cache in OSCacheProvider...");
62 |  |
63 | 0 | String refreshPeriodString = cache.getProperty( StringHelper.qualify(region, OSCACHE_REFRESH_PERIOD) );
64 | 0 | int refreshPeriod = refreshPeriodString==null ? CacheEntry.INDEFINITE_EXPIRY : Integer.parseInt( refreshPeriodString.trim() );
65 |  |
66 | 0 | String cron = cache.getProperty( StringHelper.qualify(region, OSCACHE_CRON) );
67 |  |
68 | 0 | return new OSCache(cache, refreshPeriod, cron, region);
69 |  | }
70 | 0 | throw new CacheException("OSCache was stopped or wasn't configured via method start.");
71 |  | }
72 |  |
73 |  | /**
74 |  | * @see org.hibernate.cache.CacheProvider#nextTimestamp()
75 |  | */
76 | 0 | public long nextTimestamp() {
77 | 0 | return Timestamper.next();
78 |  | }
79 |  |
80 |  | /**
81 |  | * This method isn't documented in Hibernate:
82 |  | * @see org.hibernate.cache.CacheProvider#isMinimalPutsEnabledByDefault()
83 |  | */
84 | 0 | public boolean isMinimalPutsEnabledByDefault() {
85 | 0 | return false;
86 |  | }
87 |  |
88 |  | /**
89 |  | * @see org.hibernate.cache.CacheProvider#stop()
90 |  | */
91 | 0 | public void stop() {
92 | 0 | if (cache != null) {
93 | 0 | LOG.debug("Stopping OSCacheProvider...");
94 | 0 | cache.destroy();
95 | 0 | cache = null;
96 | 0 | LOG.debug("OSCacheProvider stopped.");
97 |  | }
98 |  | }
99 |  |
100 |  | /**
101 |  | * @see org.hibernate.cache.CacheProvider#start(java.util.Properties)
102 |  | */
103 | 0 | public void start(Properties hibernateSystemProperties) throws CacheException {
104 | 0 | if (cache == null) {
105 |  | // construct the cache
106 | 0 | LOG.debug("Starting OSCacheProvider...");
107 | 0 | String configResourceName = null;
108 | 0 | if (hibernateSystemProperties != null) {
109 | 0 | configResourceName = (String) hibernateSystemProperties.get(OSCACHE_CONFIGURATION_RESOURCE_NAME);
110 |  | }
111 | 0 | if (StringUtil.isEmpty(configResourceName)) {
112 | 0 | cache = new GeneralCacheAdministrator();
113 |  | } else {
114 | 0 | Properties propertiesOSCache = Config.loadProperties(configResourceName, this.getClass().getName());
115 | 0 | cache = new GeneralCacheAdministrator(propertiesOSCache);
116 |  | }
117 | 0 | LOG.debug("OSCacheProvider started.");
118 |  | } else {
119 | 0 | LOG.warn("Tried to restart OSCacheProvider, which is already running.");
120 |  | }
121 |  | }
122 |  |
123 |  | }