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 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class OSCacheProvider implements CacheProvider { |
32 |
| |
33 |
| private static final Log LOG = LogFactory.getLog(OSCacheProvider.class); |
34 |
| |
35 |
| |
36 |
| public static final String OSCACHE_CONFIGURATION_RESOURCE_NAME = "com.opensymphony.oscache.configurationResourceName"; |
37 |
| |
38 |
| |
39 |
| public static final String OSCACHE_REFRESH_PERIOD = "refresh.period"; |
40 |
| |
41 |
| |
42 |
| public static final String OSCACHE_CRON = "cron"; |
43 |
| |
44 |
| private static GeneralCacheAdministrator cache; |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
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 |
| |
75 |
| |
76 |
0
| public long nextTimestamp() {
|
77 |
0
| return Timestamper.next();
|
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
0
| public boolean isMinimalPutsEnabledByDefault() {
|
85 |
0
| return false;
|
86 |
| } |
87 |
| |
88 |
| |
89 |
| |
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 |
| |
102 |
| |
103 |
0
| public void start(Properties hibernateSystemProperties) throws CacheException {
|
104 |
0
| if (cache == null) {
|
105 |
| |
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 |
| } |