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.ArrayList;
021: import java.util.List;
022:
023: import net.sf.ehcache.Ehcache;
024: import net.sf.ehcache.Element;
025:
026: import org.apache.jetspeed.cache.CacheElement;
027: import org.apache.jetspeed.cache.ContentCacheKey;
028: import org.apache.jetspeed.cache.JetspeedCache;
029: import org.apache.jetspeed.cache.JetspeedCacheEventListener;
030: import org.apache.jetspeed.request.RequestContext;
031:
032: public class EhCacheImpl implements JetspeedCache {
033: protected Ehcache ehcache;
034: protected List localListeners = new ArrayList();
035: protected List remoteListeners = new ArrayList();
036:
037: public EhCacheImpl(Ehcache ehcache) {
038: this .ehcache = ehcache;
039: }
040:
041: public CacheElement get(Object key) {
042: Element element = ehcache.get(key);
043: if (element == null)
044: return null;
045: return new EhCacheElementImpl(element);
046: }
047:
048: public int getTimeToIdleSeconds() {
049: return (int) ehcache.getTimeToIdleSeconds();
050: }
051:
052: public int getTimeToLiveSeconds() {
053: return (int) ehcache.getTimeToLiveSeconds();
054: }
055:
056: public boolean isKeyInCache(Object key) {
057: return ehcache.isKeyInCache(key);
058: }
059:
060: public void put(CacheElement element) {
061: EhCacheElementImpl impl = (EhCacheElementImpl) element;
062: ehcache.put(impl.getImplElement());
063: notifyListeners(true, CacheElement.ActionAdded, impl.getKey(),
064: impl.getContent());
065: }
066:
067: public CacheElement createElement(Object key, Object content) {
068: if (!((key instanceof Serializable) || !(content instanceof Serializable)))
069: throw new IllegalArgumentException(
070: "The cache key and the object to cache must be serializable."); //return null;
071: return new EhCacheElementImpl((Serializable) key,
072: (Serializable) content);
073: }
074:
075: public boolean remove(Object key) {
076: Element element = ehcache.get(key);
077: if (element == null)
078: return false;
079: boolean isRemoved = ehcache.remove(key);
080: if (isRemoved)
081: notifyListeners(true, CacheElement.ActionRemoved, key, null);
082: return isRemoved;
083: }
084:
085: public boolean removeQuiet(Object key) {
086: Element element = ehcache.get(key);
087: if (element == null)
088: return false;
089: return ehcache.removeQuiet(key);
090: }
091:
092: public void clear() {
093: ehcache.removeAll();
094: notifyListeners(true, CacheElement.ActionRemoved, null, null);
095: }
096:
097: public void evictContentForUser(String username) {
098: return;
099: }
100:
101: public void evictContentForSession(String session) {
102: return;
103: }
104:
105: public void addEventListener(JetspeedCacheEventListener listener,
106: boolean local) {
107: if (local)
108: localListeners.add(listener);
109: else
110: remoteListeners.add(listener);
111:
112: }
113:
114: public void removeEventListener(
115: JetspeedCacheEventListener listener, boolean local) {
116: if (local)
117: localListeners.remove(listener);
118: else
119: remoteListeners.remove(listener);
120:
121: }
122:
123: // ------------------------------------------------------
124:
125: public Object clone() throws CloneNotSupportedException {
126: return super .clone();
127: }
128:
129: public void dispose() {
130: }
131:
132: protected void notifyListeners(boolean local, int action,
133: Object key, Object value) {
134: List listeners = (local ? localListeners : remoteListeners);
135: for (int ix = 0; ix < listeners.size(); ix++) {
136: try {
137: JetspeedCacheEventListener listener = (JetspeedCacheEventListener) listeners
138: .get(ix);
139: switch (action) {
140: case CacheElement.ActionAdded:
141: listener
142: .notifyElementAdded(this , local, key, value);
143: break;
144: case CacheElement.ActionChanged:
145: listener.notifyElementChanged(this , local, key,
146: value);
147: break;
148: case CacheElement.ActionRemoved:
149: listener.notifyElementRemoved(this , local, key,
150: value);
151: break;
152: case CacheElement.ActionEvicted:
153: listener.notifyElementEvicted(this , local, key,
154: value);
155: break;
156: case CacheElement.ActionExpired:
157: listener.notifyElementExpired(this , local, key,
158: value);
159: break;
160: }
161: } catch (Exception e) {
162: e.printStackTrace();
163:
164: }
165: }
166: }
167:
168: public ContentCacheKey createCacheKey(RequestContext rc,
169: String windowId) {
170: return null; // not implemented here
171: }
172:
173: }
|