View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.cache.impl;
18  
19  import java.io.Serializable;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import net.sf.ehcache.Cache;
26  import net.sf.ehcache.CacheException;
27  import net.sf.ehcache.Ehcache;
28  import net.sf.ehcache.Element;
29  import net.sf.ehcache.event.CacheEventListener;
30  import net.sf.ehcache.event.RegisteredEventListeners;
31  
32  import org.apache.jetspeed.cache.CacheElement;
33  import org.apache.jetspeed.cache.DistributedCacheObject;
34  import org.apache.jetspeed.cache.JetspeedCache;
35  import org.apache.jetspeed.request.RequestContext;
36  
37  public class EhCacheDistributedImpl extends EhCacheImpl implements JetspeedCache, CacheEventListener
38  {
39  
40  
41  	private Map refList = Collections.synchronizedMap(new HashMap());
42  
43  
44  	public EhCacheDistributedImpl(Ehcache ehcache)
45  	{
46  		super(ehcache);
47  		RegisteredEventListeners listeners = ehcache
48  				.getCacheEventNotificationService();
49  		listeners.registerListener(this);
50  
51  	}
52  
53  	public CacheElement get(Object key)
54  	{
55  		return get((Serializable)key);
56  	}
57  
58  
59  	public CacheElement get(Serializable key)
60  	{
61  		Element element = ehcache.get(key);
62  		if (element == null)
63  			return null;
64  		return new EhCacheDistributedElementImpl(element);
65  	}
66  
67  
68  	public boolean isKeyInCache(Object key)
69  	{
70  		if ((key == null) || (!(key instanceof Serializable)))
71  			return false;
72  		return ehcache.isKeyInCache(key);
73  	}
74  
75  	public boolean isKeyInCache(Serializable key)
76  	{
77  		return ehcache.isKeyInCache(key);
78  	}
79  
80  	public void put(CacheElement element)
81  	{
82  		EhCacheDistributedElementImpl impl = (EhCacheDistributedElementImpl) element;
83  		ehcache.put(impl.getImplElement());
84  		refList.put(impl.getKey(), impl);
85  		notifyListeners(true, CacheElement.ActionAdded,impl.getKey(),impl.getContent());
86  	}
87  
88  	public CacheElement createElement(Object key, Object content)
89  	{
90  		return new EhCacheDistributedElementImpl((Serializable)key, (DistributedCacheObject)content);
91  	}
92  
93  	public CacheElement createElement(Serializable key, DistributedCacheObject content)
94  	{
95  		return new EhCacheDistributedElementImpl(key, content);
96  	}
97  
98  
99  	public boolean remove(Object key)
100 	{
101 		return remove ((Serializable) key);
102 	}
103 
104 	public boolean remove(Serializable key)
105 	{
106 		Element element = ehcache.get(key);
107 		refList.remove(key);
108 		if (element == null)
109 			return false;
110         boolean isRemoved = ehcache.remove(key);
111         if (isRemoved)
112     		notifyListeners(true, CacheElement.ActionRemoved,key,null);
113         return isRemoved;
114 	}
115 
116 	public boolean removeQuiet(Object key)
117 	{
118 		Element element = ehcache.get(key);
119 		refList.remove(key);
120 		if (element == null)
121 			return false;
122 		return ehcache.removeQuiet(key);
123 
124 	}
125 
126 	public void evictContentForUser(RequestContext context)
127 	{
128 		return;
129 	}
130 
131 	public String createCacheKey(String primary, String secondary)
132 	{
133 		return primary;
134 	}
135 
136 	public Object clone() throws CloneNotSupportedException
137 	{
138 		return null;
139 	}
140 	
141    public void dispose()
142     {
143 		if (refList != null)
144 		{
145 			Map temp = refList;
146 			refList = null;
147 			temp.clear();
148 		}
149 		else 
150 			return;
151 		if (this.ehcache != null)
152 		{
153 			ehcache = null;
154 		}
155     }
156 	
157 	public void notifyElement( Ehcache cache, boolean local,Element arg1, int action)
158 	{
159 		if (cache != this.ehcache)
160 		{
161 			System.out.println ("Cache=" + cache.getName() + " is not my cache=" + this.ehcache.getName());
162 			return;
163 		}
164 		try
165 		{
166 			EhCacheDistributedElementImpl e = (EhCacheDistributedElementImpl) refList
167 					.get(arg1.getKey());
168 			if (e != null)
169 			{
170 				if (action < 0)
171 					refList.remove(arg1.getKey());
172 				else if (action == CacheElement.ActionAdded)
173 					refList.put(arg1.getKey(), arg1);
174 				e.notifyChange(action);
175 				notifyListeners(local, action,arg1.getKey(),arg1.getObjectValue());
176 			}
177 		} catch (Exception e)
178 		{
179 			e.printStackTrace();
180 		}
181 	}
182 
183 	public void notifyElementEvicted(Ehcache cache, Element arg1)
184 	{
185 			notifyElement(cache, false, arg1,CacheElement.ActionEvicted);
186 	}
187 
188 	public void notifyElementExpired(Ehcache cache, Element arg1)
189 	{
190 		notifyElement(cache, false, arg1,CacheElement.ActionExpired);
191 	}
192 
193 	public void notifyElementPut(Ehcache cache, Element arg1)
194 			throws CacheException
195 	{
196 		
197 			notifyElement(cache, false, arg1, CacheElement.ActionAdded);
198 	}
199 
200 	public void notifyElementRemoved(Ehcache cache, Element arg1)
201 			throws CacheException
202 	{
203 		notifyElement(cache, false, arg1,CacheElement.ActionRemoved);
204 	}
205 
206 	public void notifyElementUpdated(Ehcache cache, Element arg1)
207 			throws CacheException
208 	{
209 		notifyElement(cache, false,arg1,CacheElement.ActionChanged);
210 	}
211 	public void notifyRemoveAll(Ehcache cache)
212 	{
213 		if (cache != this.ehcache)
214 		{
215 			System.out.println ("Cache=" + cache.getName() + " is not my cache=" + this.ehcache.getName());
216 			return;
217 		}
218 		try
219 		{
220 			Iterator it = refList.entrySet().iterator();
221 			while (it.hasNext())
222 			{
223 				EhCacheDistributedElementImpl e = (EhCacheDistributedElementImpl)it.next();
224 				notifyListeners(false, CacheElement.ActionRemoved,e.getKey(),e);
225 				e.notifyChange(CacheElement.ActionRemoved);
226 			}
227 			refList.clear();
228 		} catch (Exception e)
229 		{
230 			e.printStackTrace();
231 		}
232 
233 	
234 	}
235 
236 }