| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.ksb.cache; |
| 18 | |
|
| 19 | |
import java.util.Properties; |
| 20 | |
|
| 21 | |
import org.apache.commons.lang.StringUtils; |
| 22 | |
import org.apache.log4j.Logger; |
| 23 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 24 | |
import org.kuali.rice.ksb.api.bus.ServiceBus; |
| 25 | |
|
| 26 | |
import com.opensymphony.oscache.base.AbstractCacheAdministrator; |
| 27 | |
import com.opensymphony.oscache.base.CacheEntry; |
| 28 | |
import com.opensymphony.oscache.base.NeedsRefreshException; |
| 29 | |
import com.opensymphony.oscache.general.GeneralCacheAdministrator; |
| 30 | |
import org.kuali.rice.ksb.api.cache.RiceCacheAdministrator; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | public class RiceCacheAdministratorImpl implements RiceCacheAdministrator { |
| 38 | |
|
| 39 | |
private static final String DEFAULT_SERVICE_NAME = "OSCacheNotificationService"; |
| 40 | |
|
| 41 | |
private static final String CACHE_PREFIX = "cache."; |
| 42 | |
private GeneralCacheAdministrator cacheAdministrator; |
| 43 | |
private boolean started; |
| 44 | |
private boolean forceRegistryRefresh; |
| 45 | |
private String serviceName; |
| 46 | |
protected ServiceBus serviceBus; |
| 47 | 0 | private static final Logger LOG = Logger.getLogger(RiceCacheAdministratorImpl.class); |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public ServiceBus getServiceBus() { |
| 53 | 0 | return this.serviceBus; |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public void setServiceBus(ServiceBus serviceBus) { |
| 60 | 0 | this.serviceBus = serviceBus; |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
public boolean isStarted() { |
| 64 | 0 | return this.started; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public Object getFromCache(String key) { |
| 68 | 0 | return getFromCache(key, CacheEntry.INDEFINITE_EXPIRY, null); |
| 69 | |
} |
| 70 | |
|
| 71 | |
public Object getFromCache(String key, int refreshPeriod) { |
| 72 | 0 | return getFromCache(key, refreshPeriod, null); |
| 73 | |
} |
| 74 | |
|
| 75 | |
public Object getFromCache(String key, int refreshPeriod, String cronExpression ) { |
| 76 | |
try { |
| 77 | 0 | return getCacheAdministrator().getFromCache(key, refreshPeriod, cronExpression); |
| 78 | 0 | } catch (NeedsRefreshException e) { |
| 79 | 0 | getCacheAdministrator().cancelUpdate(key); |
| 80 | 0 | return null; |
| 81 | |
} |
| 82 | |
} |
| 83 | |
|
| 84 | |
public void putInCache(String key, Object content, String[] groups) { |
| 85 | |
try { |
| 86 | 0 | getCacheAdministrator().putInCache(key, content, groups); |
| 87 | 0 | } catch (IllegalStateException e) { |
| 88 | 0 | LOG.warn("Failed to insert object into cache with key: " + key); |
| 89 | 0 | } |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
public void putInCache(String key, Object content) { |
| 93 | |
try { |
| 94 | 0 | getCacheAdministrator().putInCache(key, content); |
| 95 | 0 | } catch (IllegalStateException e) { |
| 96 | 0 | LOG.warn("Failed to insert object into cache with key: " + key); |
| 97 | 0 | } |
| 98 | 0 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
public void flushEntry(String key) { |
| 102 | 0 | getCacheAdministrator().flushEntry(key); |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
public void flushGroup(String group) { |
| 107 | 0 | getCacheAdministrator().flushGroup(group); |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
public void flushAll() { |
| 111 | 0 | getCacheAdministrator().flushAll(); |
| 112 | 0 | } |
| 113 | |
|
| 114 | |
public void setCacheCapacity(int capacity) { |
| 115 | 0 | getCacheAdministrator().setCacheCapacity(capacity); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public int getCacheCapacity() { |
| 119 | 0 | return getCacheAdministrator().getCache().getCapacity(); |
| 120 | |
} |
| 121 | |
|
| 122 | |
public int getSize() { |
| 123 | 0 | return getCacheAdministrator().getCache().getSize(); |
| 124 | |
} |
| 125 | |
|
| 126 | |
public void start() throws Exception { |
| 127 | 0 | Properties props = loadCacheSettings(); |
| 128 | 0 | this.cacheAdministrator = new GeneralCacheAdministrator(props); |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
protected Properties loadCacheSettings() { |
| 132 | 0 | Properties properties = new Properties(); |
| 133 | 0 | Properties configProperties = ConfigContext.getCurrentContextConfig().getProperties(); |
| 134 | 0 | for (Object keyObject : configProperties.keySet()) { |
| 135 | 0 | String key = (String)keyObject; |
| 136 | 0 | if (key.startsWith(CACHE_PREFIX)) { |
| 137 | 0 | properties.put(key, configProperties.getProperty(key)); |
| 138 | |
} |
| 139 | 0 | } |
| 140 | |
|
| 141 | 0 | if (!properties.containsKey(AbstractCacheAdministrator.CACHE_MEMORY_KEY)) { |
| 142 | 0 | properties.put(AbstractCacheAdministrator.CACHE_MEMORY_KEY, "true"); |
| 143 | |
} |
| 144 | 0 | if (!properties.containsKey(AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY)) { |
| 145 | 0 | properties.put(AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY, RiceDistributedCacheListener.class.getName()); |
| 146 | |
} |
| 147 | 0 | if (!properties.containsKey(AbstractCacheAdministrator.CACHE_BLOCKING_KEY)) { |
| 148 | 0 | properties.put(AbstractCacheAdministrator.CACHE_BLOCKING_KEY, "false"); |
| 149 | |
} |
| 150 | 0 | properties.put(CacheProperties.FORCE_REGISTRY_REFRESH_KEY, new Boolean(this.forceRegistryRefresh)); |
| 151 | 0 | properties.put(CacheProperties.SERVICE_BUS, serviceBus); |
| 152 | 0 | if (StringUtils.isBlank(this.serviceName)) { |
| 153 | 0 | this.serviceName = DEFAULT_SERVICE_NAME; |
| 154 | |
} |
| 155 | 0 | properties.put(CacheProperties.SERVICE_NAME_KEY, this.serviceName); |
| 156 | 0 | return properties; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public void stop() throws Exception { |
| 160 | 0 | getCacheAdministrator().destroy(); |
| 161 | 0 | this.started = false; |
| 162 | 0 | } |
| 163 | |
|
| 164 | |
public void putInCache(String key, Object content, String group) { |
| 165 | 0 | putInCache(key, content, new String[] {group}); |
| 166 | 0 | } |
| 167 | |
|
| 168 | |
protected GeneralCacheAdministrator getCacheAdministrator() { |
| 169 | 0 | return this.cacheAdministrator; |
| 170 | |
} |
| 171 | |
|
| 172 | |
public void setForceRegistryRefresh(boolean forceRegistryRefresh) { |
| 173 | 0 | this.forceRegistryRefresh = forceRegistryRefresh; |
| 174 | 0 | } |
| 175 | |
|
| 176 | |
public String getServiceName() { |
| 177 | 0 | return this.serviceName; |
| 178 | |
} |
| 179 | |
|
| 180 | |
public void setServiceName(String serviceName) { |
| 181 | 0 | this.serviceName = serviceName; |
| 182 | 0 | } |
| 183 | |
|
| 184 | |
} |