001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.cache;
018:
019: import java.util.Properties;
020:
021: import org.kuali.rice.config.Config;
022: import org.kuali.rice.core.Core;
023:
024: import com.opensymphony.oscache.base.AbstractCacheAdministrator;
025: import com.opensymphony.oscache.base.NeedsRefreshException;
026: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
027:
028: /**
029: * Default implementation of the {@link RiceCacheAdministrator}.
030: *
031: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
032: */
033: public class RiceCacheAdministratorImpl implements
034: RiceCacheAdministrator {
035:
036: private static final String CACHE_PREFIX = "cache.";
037: private GeneralCacheAdministrator cacheAdministrator;
038: private boolean started;
039: private String messageEntity;
040: private boolean forceRegistryRefresh;
041:
042: public boolean isStarted() {
043: return this .started;
044: }
045:
046: public Object getFromCache(String key) {
047: try {
048: return getCacheAdministrator().getFromCache(key);
049: } catch (NeedsRefreshException e) {
050: getCacheAdministrator().cancelUpdate(key);
051: return null;
052: }
053: }
054:
055: public void putInCache(String key, Object content, String[] groups) {
056: getCacheAdministrator().putInCache(key, content, groups);
057: }
058:
059: public void putInCache(String key, Object content) {
060: getCacheAdministrator().putInCache(key, content);
061: }
062:
063: public void flushEntry(String key) {
064: getCacheAdministrator().flushEntry(key);
065: }
066:
067: public void flushGroup(String group) {
068: getCacheAdministrator().flushGroup(group);
069: }
070:
071: public void flushAll() {
072: getCacheAdministrator().flushAll();
073: }
074:
075: public void setCacheCapacity(int capacity) {
076: getCacheAdministrator().setCacheCapacity(capacity);
077: }
078:
079: public int getCacheCapacity() {
080: return getCacheAdministrator().getCache().getCapacity();
081: }
082:
083: public int getSize() {
084: return getCacheAdministrator().getCache().getSize();
085: }
086:
087: public void start() throws Exception {
088: Properties props = loadCacheSettings();
089: this .cacheAdministrator = new GeneralCacheAdministrator(props);
090: }
091:
092: protected Properties loadCacheSettings() {
093: Properties properties = new Properties();
094: Properties configProperties = Core.getCurrentContextConfig()
095: .getProperties();
096: for (Object keyObject : configProperties.keySet()) {
097: String key = (String) keyObject;
098: if (key.startsWith(CACHE_PREFIX)) {
099: properties.put(key, configProperties.getProperty(key));
100: }
101: }
102: // setup defaults if certain properties aren't set
103: if (!properties
104: .containsKey(AbstractCacheAdministrator.CACHE_MEMORY_KEY)) {
105: properties.put(AbstractCacheAdministrator.CACHE_MEMORY_KEY,
106: "true");
107: }
108: if (!properties
109: .containsKey(AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY)) {
110: properties
111: .put(
112: AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY,
113: RiceDistributedCacheListener.class
114: .getName());
115: }
116: if (!properties
117: .containsKey(AbstractCacheAdministrator.CACHE_BLOCKING_KEY)) {
118: properties.put(
119: AbstractCacheAdministrator.CACHE_BLOCKING_KEY,
120: "false");
121: }
122: // we put the message entity in as a property because it's required in order for the KEWDistributedCacheListener to be initialized
123: properties.put(Config.MESSAGE_ENTITY, getMessageEntity());
124: properties.put(
125: RiceCacheAdministrator.FORCE_REGISTRY_REFRESH_KEY,
126: new Boolean(this .forceRegistryRefresh));
127: return properties;
128: }
129:
130: public void stop() throws Exception {
131: getCacheAdministrator().destroy();
132: this .started = false;
133: }
134:
135: public void putInCache(String key, Object content, String group) {
136: putInCache(key, content, new String[] { group });
137: }
138:
139: protected GeneralCacheAdministrator getCacheAdministrator() {
140: return this .cacheAdministrator;
141: }
142:
143: public String getMessageEntity() {
144: if (this .messageEntity == null) {
145: return Core.getCurrentContextConfig().getMessageEntity();
146: }
147: return this .messageEntity;
148: }
149:
150: public void setMessageEntity(String notificationTopicName) {
151: this .messageEntity = notificationTopicName;
152: }
153:
154: public void setForceRegistryRefresh(boolean forceRegistryRefresh) {
155: this.forceRegistryRefresh = forceRegistryRefresh;
156: }
157: }
|