001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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 org.apache.jetspeed.cache.impl;
018:
019: import java.io.Serializable;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import net.sf.ehcache.Cache;
026: import net.sf.ehcache.CacheException;
027: import net.sf.ehcache.Ehcache;
028: import net.sf.ehcache.Element;
029: import net.sf.ehcache.event.CacheEventListener;
030: import net.sf.ehcache.event.RegisteredEventListeners;
031:
032: import org.apache.jetspeed.cache.CacheElement;
033: import org.apache.jetspeed.cache.DistributedCacheObject;
034: import org.apache.jetspeed.cache.JetspeedCache;
035: import org.apache.jetspeed.request.RequestContext;
036:
037: public class EhCacheDistributedImpl extends EhCacheImpl implements
038: JetspeedCache, CacheEventListener {
039:
040: private Map refList = Collections.synchronizedMap(new HashMap());
041:
042: public EhCacheDistributedImpl(Ehcache ehcache) {
043: super (ehcache);
044: RegisteredEventListeners listeners = ehcache
045: .getCacheEventNotificationService();
046: listeners.registerListener(this );
047:
048: }
049:
050: public CacheElement get(Object key) {
051: return get((Serializable) key);
052: }
053:
054: public CacheElement get(Serializable key) {
055: Element element = ehcache.get(key);
056: if (element == null)
057: return null;
058: return new EhCacheDistributedElementImpl(element);
059: }
060:
061: public boolean isKeyInCache(Object key) {
062: if ((key == null) || (!(key instanceof Serializable)))
063: return false;
064: return ehcache.isKeyInCache(key);
065: }
066:
067: public boolean isKeyInCache(Serializable key) {
068: return ehcache.isKeyInCache(key);
069: }
070:
071: public void put(CacheElement element) {
072: EhCacheDistributedElementImpl impl = (EhCacheDistributedElementImpl) element;
073: ehcache.put(impl.getImplElement());
074: refList.put(impl.getKey(), impl);
075: notifyListeners(true, CacheElement.ActionAdded, impl.getKey(),
076: impl.getContent());
077: }
078:
079: public CacheElement createElement(Object key, Object content) {
080: return new EhCacheDistributedElementImpl((Serializable) key,
081: (DistributedCacheObject) content);
082: }
083:
084: public CacheElement createElement(Serializable key,
085: DistributedCacheObject content) {
086: return new EhCacheDistributedElementImpl(key, content);
087: }
088:
089: public boolean remove(Object key) {
090: return remove((Serializable) key);
091: }
092:
093: public boolean remove(Serializable key) {
094: Element element = ehcache.get(key);
095: refList.remove(key);
096: if (element == null)
097: return false;
098: boolean isRemoved = ehcache.remove(key);
099: if (isRemoved)
100: notifyListeners(true, CacheElement.ActionRemoved, key, null);
101: return isRemoved;
102: }
103:
104: public boolean removeQuiet(Object key) {
105: Element element = ehcache.get(key);
106: refList.remove(key);
107: if (element == null)
108: return false;
109: return ehcache.removeQuiet(key);
110:
111: }
112:
113: public void evictContentForUser(RequestContext context) {
114: return;
115: }
116:
117: public String createCacheKey(String primary, String secondary) {
118: return primary;
119: }
120:
121: public Object clone() throws CloneNotSupportedException {
122: return null;
123: }
124:
125: public void dispose() {
126: if (refList != null) {
127: Map temp = refList;
128: refList = null;
129: temp.clear();
130: } else
131: return;
132: if (this .ehcache != null) {
133: ehcache = null;
134: }
135: }
136:
137: public void notifyElement(Ehcache cache, boolean local,
138: Element arg1, int action) {
139: if (cache != this .ehcache) {
140: System.out.println("Cache=" + cache.getName()
141: + " is not my cache=" + this .ehcache.getName());
142: return;
143: }
144: try {
145: EhCacheDistributedElementImpl e = (EhCacheDistributedElementImpl) refList
146: .get(arg1.getKey());
147: if (e != null) {
148: if (action < 0)
149: refList.remove(arg1.getKey());
150: else if (action == CacheElement.ActionAdded)
151: refList.put(arg1.getKey(), arg1);
152: e.notifyChange(action);
153: notifyListeners(local, action, arg1.getKey(), arg1
154: .getObjectValue());
155: }
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160:
161: public void notifyElementEvicted(Ehcache cache, Element arg1) {
162: notifyElement(cache, false, arg1, CacheElement.ActionEvicted);
163: }
164:
165: public void notifyElementExpired(Ehcache cache, Element arg1) {
166: notifyElement(cache, false, arg1, CacheElement.ActionExpired);
167: }
168:
169: public void notifyElementPut(Ehcache cache, Element arg1)
170: throws CacheException {
171:
172: notifyElement(cache, false, arg1, CacheElement.ActionAdded);
173: }
174:
175: public void notifyElementRemoved(Ehcache cache, Element arg1)
176: throws CacheException {
177: notifyElement(cache, false, arg1, CacheElement.ActionRemoved);
178: }
179:
180: public void notifyElementUpdated(Ehcache cache, Element arg1)
181: throws CacheException {
182: notifyElement(cache, false, arg1, CacheElement.ActionChanged);
183: }
184:
185: public void notifyRemoveAll(Ehcache cache) {
186: if (cache != this .ehcache) {
187: System.out.println("Cache=" + cache.getName()
188: + " is not my cache=" + this .ehcache.getName());
189: return;
190: }
191: try {
192: Iterator it = refList.entrySet().iterator();
193: while (it.hasNext()) {
194: EhCacheDistributedElementImpl e = (EhCacheDistributedElementImpl) it
195: .next();
196: notifyListeners(false, CacheElement.ActionRemoved, e
197: .getKey(), e);
198: e.notifyChange(CacheElement.ActionRemoved);
199: }
200: refList.clear();
201: } catch (Exception e) {
202: e.printStackTrace();
203: }
204:
205: }
206:
207: }
|