KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > EvictionInterceptor


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

8 package org.jboss.cache.interceptors;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.jboss.cache.CacheSPI;
13 import org.jboss.cache.Fqn;
14 import org.jboss.cache.Region;
15 import org.jboss.cache.RegionManager;
16 import org.jboss.cache.eviction.EvictedEventNode;
17 import org.jboss.cache.eviction.NodeEventType;
18 import org.jboss.cache.marshall.MethodCall;
19 import org.jboss.cache.marshall.MethodDeclarations;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Eviction Interceptor.
26  * <p/>
27  * This interceptor is used to handle eviction events.
28  *
29  * @author Daniel Huang
30  * @version $Revision: 1.12 $
31  */

32 public class EvictionInterceptor extends Interceptor
33 {
34    private static final Log log = LogFactory.getLog(EvictionInterceptor.class);
35
36    protected RegionManager regionManager;
37    protected Map JavaDoc<Integer JavaDoc, EvictionMethodHandler> evictionMethodHandlers = new HashMap JavaDoc<Integer JavaDoc, EvictionMethodHandler>();
38
39    public EvictionInterceptor()
40    {
41       EvictionMethodHandler handler = new GetNodeEvictionMethodHandler();
42       evictionMethodHandlers.put(MethodDeclarations.getNodeMethodLocal_id, handler);
43       evictionMethodHandlers.put(MethodDeclarations.getDataMapMethodLocal_id, handler);
44
45       handler = new GetKeyEvictionMethodHandler();
46       evictionMethodHandlers.put(MethodDeclarations.getKeyValueMethodLocal_id, handler);
47
48       handler = new RemoveNodeEvictionMethodHandler();
49       evictionMethodHandlers.put(MethodDeclarations.removeNodeMethodLocal_id, handler);
50       evictionMethodHandlers.put(MethodDeclarations.removeDataMethodLocal_id, handler);
51
52       handler = new RemoveKeyEvictionMethodHandler();
53       evictionMethodHandlers.put(MethodDeclarations.removeKeyMethodLocal_id, handler);
54
55       handler = new PutDataEvictionMethodHandler();
56       evictionMethodHandlers.put(MethodDeclarations.putDataMethodLocal_id, handler);
57
58       handler = new PutDataEraseEvictionMethodHandler();
59       evictionMethodHandlers.put(MethodDeclarations.putDataEraseMethodLocal_id, handler);
60
61       handler = new PutKeyEvictionMethodHandler();
62       evictionMethodHandlers.put(MethodDeclarations.putKeyValMethodLocal_id, handler);
63    }
64
65    /**
66     * this method is for ease of unit testing. thus package access.
67     * <p/>
68     * Not to be attempted to be used anywhere else.
69     */

70    void setRegionManager(RegionManager regionManager)
71    {
72       this.regionManager = regionManager;
73    }
74
75    public void setCache(CacheSPI cache)
76    {
77       super.setCache(cache);
78       this.regionManager = cache.getRegionManager();
79    }
80
81    public Object JavaDoc invoke(MethodCall m) throws Throwable JavaDoc
82    {
83       Object JavaDoc ret = super.invoke(m);
84
85       if (log.isTraceEnabled())
86       {
87          log.trace("Invoking EvictionInterceptor");
88       }
89
90       // skip the TX. this interceptor will invoke around/after the call and lock. if the ret == null or if an exception
91
// is thrown, this interceptor is terminated. there is no need for explicit rollback logic.
92
this.updateNode(m, ret);
93
94       if (log.isTraceEnabled())
95       {
96          log.trace("Finished invoking EvictionInterceptor");
97       }
98
99       return ret;
100    }
101
102    protected void updateNode(MethodCall m, Object JavaDoc retVal)
103    {
104       if (log.isTraceEnabled())
105       {
106          log.trace("Updating node/element events with no tx");
107       }
108
109       EvictedEventNode event = this.extractEvent(m, retVal);
110       if (event == null)
111       {
112          // no node modifications.
113
return;
114       }
115
116       this.doEventUpdatesOnRegionManager(event);
117
118       if (log.isTraceEnabled())
119       {
120          log.trace("Finished updating node");
121       }
122    }
123
124    protected EvictedEventNode extractEvent(MethodCall m, Object JavaDoc retVal)
125    {
126       EvictionMethodHandler handler = this.evictionMethodHandlers.get(m.getMethodId());
127       if (handler == null)
128       {
129          return null;
130       }
131
132       return handler.extractEvictedEventNode(m, retVal);
133    }
134
135    protected boolean canIgnoreEvent(Fqn fqn, NodeEventType type)
136    {
137       Region r = regionManager.getRegion(fqn, Region.Type.EVICTION, false);
138       if (r == null) return true;// should never happen, we should at least get the default region.
139
return r.getEvictionPolicy().canIgnoreEvent(fqn, type);
140    }
141
142    protected void doEventUpdatesOnRegionManager(EvictedEventNode event)
143    {
144       Region region = regionManager.getRegion(event.getFqn(), false);
145       region.putNodeEvent(event);
146
147       if (log.isTraceEnabled())
148       {
149          log.trace("Adding event " + event + " to region at " + region.getFqn());
150       }
151    }
152
153    protected class GetNodeEvictionMethodHandler implements EvictionMethodHandler
154    {
155       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
156       {
157          if (retVal == null)
158          {
159             if (log.isTraceEnabled())
160             {
161                log.trace("No event added. Node does not exist");
162             }
163
164             return null;
165          }
166
167          Object JavaDoc args[] = mc.getArgs();
168          Fqn fqn = (Fqn) args[0];
169
170          if (fqn != null && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.VISIT_NODE_EVENT))
171          {
172             if (fqn != null
173                     && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.VISIT_NODE_EVENT))
174             {
175                return new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT);
176             }
177          }
178
179          return null;
180       }
181    }
182
183    protected class GetKeyEvictionMethodHandler implements EvictionMethodHandler
184    {
185       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
186       {
187          if (retVal == null)
188          {
189             if (log.isTraceEnabled())
190             {
191                log.trace("No event added. Element does not exist");
192             }
193
194             return null;
195          }
196
197          Object JavaDoc args[] = mc.getArgs();
198          Fqn fqn = (Fqn) args[0];
199          Object JavaDoc key = args[1];
200          if (fqn != null && key != null
201                  && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.VISIT_NODE_EVENT))
202          {
203             return new EvictedEventNode(fqn, NodeEventType.VISIT_NODE_EVENT);
204          }
205
206          return null;
207       }
208
209    }
210
211    protected class RemoveNodeEvictionMethodHandler implements EvictionMethodHandler
212    {
213       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
214       {
215          Object JavaDoc args[] = mc.getArgs();
216          Fqn fqn = (Fqn) args[1];
217
218          if (fqn != null
219                  && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.REMOVE_NODE_EVENT))
220          {
221             return new EvictedEventNode(fqn, NodeEventType.REMOVE_NODE_EVENT);
222          }
223
224          return null;
225       }
226
227    }
228
229    protected class RemoveKeyEvictionMethodHandler implements EvictionMethodHandler
230    {
231       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
232       {
233          if (retVal == null)
234          {
235             if (log.isTraceEnabled())
236             {
237                log.trace("No event added. Element does not exist");
238             }
239
240             return null;
241          }
242
243          Object JavaDoc args[] = mc.getArgs();
244          Fqn fqn = (Fqn) args[1];
245          Object JavaDoc key = args[2];
246          if (fqn != null && key != null
247                  && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.REMOVE_ELEMENT_EVENT))
248          {
249             return new EvictedEventNode(fqn, NodeEventType.REMOVE_ELEMENT_EVENT, 1);
250          }
251          return null;
252       }
253
254    }
255
256    protected class PutDataEvictionMethodHandler implements EvictionMethodHandler
257    {
258       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
259       {
260          Object JavaDoc[] args = mc.getArgs();
261          Fqn fqn = (Fqn) args[1];
262          Map JavaDoc putData = (Map JavaDoc) args[2];
263          if (fqn != null
264                  && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.ADD_NODE_EVENT))
265          {
266             if (putData == null)
267             {
268                if (log.isTraceEnabled())
269                {
270                   log.trace("Putting null data under fqn " + fqn + ".");
271                }
272
273                return null;
274             }
275
276             int size;
277             synchronized (putData)
278             {
279                size = putData.size();
280             }
281
282             return new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, size);
283          }
284
285          return null;
286       }
287
288    }
289
290    protected class PutDataEraseEvictionMethodHandler implements EvictionMethodHandler
291    {
292       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
293       {
294          Object JavaDoc[] args = mc.getArgs();
295          Fqn fqn = (Fqn) args[1];
296          Map JavaDoc putData = (Map JavaDoc) args[2];
297          Boolean JavaDoc resetElementCount = (Boolean JavaDoc) args[4];
298          if (fqn != null
299                  && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.ADD_NODE_EVENT))
300          {
301             if (putData == null)
302             {
303                if (log.isTraceEnabled())
304                {
305                   log.trace("Putting null data under fqn " + fqn + ".");
306                }
307
308                return null;
309             }
310
311             int size;
312             synchronized (putData)
313             {
314                size = putData.size();
315             }
316
317             EvictedEventNode event = new EvictedEventNode(fqn, NodeEventType.ADD_NODE_EVENT, size);
318             event.setResetElementCount(resetElementCount);
319             return event;
320          }
321
322          return null;
323       }
324    }
325
326    protected class PutKeyEvictionMethodHandler implements EvictionMethodHandler
327    {
328       public EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal)
329       {
330          Object JavaDoc[] args = mc.getArgs();
331          Fqn fqn = (Fqn) args[1];
332          Object JavaDoc key = args[2];
333          if (fqn != null && key != null
334                  && !EvictionInterceptor.this.canIgnoreEvent(fqn, NodeEventType.ADD_ELEMENT_EVENT))
335          {
336             return new EvictedEventNode(fqn, NodeEventType.ADD_ELEMENT_EVENT, 1);
337          }
338
339          return null;
340       }
341
342    }
343
344    protected interface EvictionMethodHandler
345    {
346       EvictedEventNode extractEvictedEventNode(MethodCall mc, Object JavaDoc retVal);
347    }
348
349 }
350
Popular Tags