KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > CachedMapInterceptor


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.aop;
8
9 import org.jboss.cache.Fqn;
10 import org.jboss.cache.Node;
11 import org.jboss.logging.Logger;
12 import org.jboss.util.NestedRuntimeException;
13
14 import java.util.*;
15
16 /**
17  * Map interceptor to perform Map cache storage and retrieval
18  *
19  * @author <a HREF="mailto:harald@gliebe.de">Harald Gliebe</a>
20  * @author Ben Wang
21  */

22 public class CachedMapInterceptor implements CachedCollectionInterceptor, Map
23 {
24
25    protected static final Logger log = Logger.getLogger(CachedMapInterceptor.class);
26    protected static final Map managedMethods =
27          CollectionInterceptorUtil.getManagedMethods(Map.class);
28
29    protected TreeCacheAop cache;
30    protected Fqn fqn;
31    protected Map methodMap;
32
33    protected CachedMapInterceptor(TreeCacheAop cache, Fqn fqn, Class JavaDoc clazz)
34    {
35       this.cache = cache;
36       this.fqn = fqn;
37       methodMap = CollectionInterceptorUtil.getMethodMap(clazz);
38    }
39
40    public String JavaDoc getName()
41    {
42       return "CachedMapInterceptor";
43    }
44
45    public Object JavaDoc invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable JavaDoc
46    {
47       return CollectionInterceptorUtil.invoke(invocation,
48             this,
49             methodMap,
50             managedMethods);
51    }
52
53    // implementation of the java.util.Map interface
54

55    protected Node getNode()
56    {
57       try {
58          return cache.get(fqn);
59       } catch (Exception JavaDoc e) {
60          throw new NestedRuntimeException(e);
61       }
62    }
63
64    public Object JavaDoc get(Object JavaDoc key)
65    {
66       try {
67          return cache.getObject(new Fqn(fqn, key));
68       } catch (Exception JavaDoc e) {
69          throw new NestedRuntimeException(e);
70       }
71    }
72
73    public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
74    {
75       try {
76          return cache.putObject(new Fqn(fqn, key), value);
77       } catch (Exception JavaDoc e) {
78          throw new NestedRuntimeException(e);
79       }
80    }
81
82    public void putAll(Map map)
83    {
84       for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
85          Map.Entry entry = (Map.Entry) i.next();
86          put(entry.getKey(), entry.getValue());
87       }
88    }
89
90    public Object JavaDoc remove(Object JavaDoc key)
91    {
92       try {
93          return cache.removeObject(new Fqn(fqn, key));
94       } catch (Exception JavaDoc e) {
95          throw new NestedRuntimeException(e);
96       }
97    }
98
99    public void clear()
100    {
101       // Need to clone first to avoid CME
102
ArrayList list = new ArrayList(keySet());
103       for(int i=0; i < list.size(); i++) {
104          remove(list.get(i));
105       }
106    }
107
108    public int size()
109    {
110       Node node = getNode();
111       if(node == null) {
112          return 0;
113       }
114
115       Map children = node.getChildren();
116       return children == null ? 0 : children.size();
117    }
118
119    public boolean isEmpty()
120    {
121       return size() == 0;
122    }
123
124    public boolean containsKey(Object JavaDoc object)
125    {
126       Map children = getNode().getChildren();
127       return children == null ? false : children.containsKey(object);
128    }
129
130    public boolean containsValue(Object JavaDoc object)
131    {
132       throw new AopOperationNotSupportedException("CachedMapInterceptor: map.containsValues() operation not supported");
133    }
134
135    public Set entrySet()
136    {
137       final CachedMapInterceptor map = this;
138
139       return new AbstractSet()
140       {
141
142          public int size()
143          {
144             Map children = getNode().getChildren();
145             return children == null ? 0 : children.size();
146          }
147
148          public Iterator iterator()
149          {
150             Map children = getNode().getChildren();
151             final Iterator i =
152                   children == null
153                   ? Collections.EMPTY_LIST.iterator()
154                   : children.keySet().iterator();
155
156             return new Iterator()
157             {
158                Object JavaDoc lastKey; // for remove
159

160                public boolean hasNext()
161                {
162                   return i.hasNext();
163                }
164
165                public Object JavaDoc next()
166                {
167                   return new Entry(lastKey = i.next());
168                }
169
170                public void remove()
171                {
172                   map.remove(lastKey);
173                }
174             };
175          }
176       };
177    }
178
179    public Collection values()
180    {
181       throw new AopOperationNotSupportedException("CachedMapInterceptor: map.values() operation not supported");
182    }
183
184    public Set keySet()
185    {
186       /**
187        * TODO Note that return Set is not thread safe because children is not.
188        */

189       Map children = getNode().getChildren();
190       return children == null ? Collections.EMPTY_SET : children.keySet();
191    }
192
193    public int hashCode()
194    {
195       int result = 0;
196       for (Iterator i = entrySet().iterator(); i.hasNext();) {
197          result += i.next().hashCode();
198       }
199       return result;
200    }
201
202    public boolean equals(Object JavaDoc object)
203    {
204       if (object == this)
205          return true;
206       if (!(object instanceof Map))
207          return false;
208       Map map = (Map) object;
209       if (size() != map.size())
210          return false;
211       for (Iterator i = entrySet().iterator(); i.hasNext();) {
212          Map.Entry entry = (Map.Entry) i.next();
213          Object JavaDoc value = entry.getValue();
214          if (value != null) {
215             if (map.get(entry.getKey()) == null
216                   || !map.containsKey(entry.getKey()))
217                return false;
218          } else { // Null key is not allowed
219
return false;
220          }
221       }
222       return true;
223    }
224
225    public String JavaDoc toString() {
226       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
227       Set set = keySet();
228       for(Iterator it = set.iterator(); it.hasNext();) {
229          Object JavaDoc key = it.next();
230          buf.append("[" +key).append(", ").append(get(key)).append("]");
231          if(it.hasNext()) buf.append(", ");
232       }
233
234       return buf.toString();
235    }
236
237    protected class Entry implements Map.Entry
238    {
239
240       Object JavaDoc key;
241
242       public Entry(Object JavaDoc key)
243       {
244          this.key = key;
245       }
246
247       public Object JavaDoc getKey()
248       {
249          return key;
250       }
251
252       public Object JavaDoc getValue()
253       {
254          try {
255             return cache.getObject(new Fqn(fqn, key));
256          } catch (Exception JavaDoc e) {
257             throw new NestedRuntimeException(e);
258          }
259       }
260
261       public Object JavaDoc setValue(Object JavaDoc value)
262       {
263          try {
264             return cache.putObject(new Fqn(fqn, key), value);
265          } catch (Exception JavaDoc e) {
266             throw new NestedRuntimeException(e);
267          }
268       }
269
270       public int hashCode()
271       {
272          Object JavaDoc value = getValue();
273          return ((key == null) ? 0 : key.hashCode())
274                ^ ((value == null) ? 0 : value.hashCode());
275       }
276
277       public boolean equals(Object JavaDoc obj)
278       {
279          if (!(obj instanceof Map.Entry))
280             return false;
281          Map.Entry entry = (Map.Entry) obj;
282          Object JavaDoc value = getValue();
283          return (
284                key == null
285                ? entry.getKey() == null
286                : key.equals(entry.getKey()))
287                && (value == null
288                ? entry.getValue() == null
289                : value.equals(entry.getValue()));
290       }
291    }
292
293 }
294
Popular Tags