KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.interceptors;
2
3 import org.jboss.cache.CacheImpl;
4 import org.jboss.cache.CacheSPI;
5 import org.jboss.cache.GlobalTransaction;
6 import org.jboss.cache.InvocationContext;
7 import org.jboss.cache.config.Option;
8 import org.jboss.cache.marshall.MethodCall;
9 import org.jboss.cache.marshall.MethodDeclarations;
10
11 import javax.transaction.Transaction JavaDoc;
12
13 /**
14  * Always at the end of the chain, directly in front of the cache. Simply calls into the cache using reflection.
15  * If the call resulted in a modification, add the Modification to the end of the modification list
16  * keyed by the current transaction.
17  * <p/>
18  * Although always added to the end of an optimistically locked chain as well, calls should not make it down to
19  * this interceptor unless it is a call the OptimisticNodeInterceptor knows nothing about.
20  *
21  * @author Bela Ban
22  * @version $Id: CallInterceptor.java,v 1.16 2006/12/30 17:49:53 msurtani Exp $
23  */

24 public class CallInterceptor extends Interceptor
25 {
26    private CacheImpl cache;
27
28    public void setCache(CacheSPI cache)
29    {
30       super.setCache(cache);
31    }
32
33    public void setTreeCacheInstance(CacheImpl c)
34    {
35       cache = c;
36    }
37
38    public Object JavaDoc invoke(MethodCall m) throws Throwable JavaDoc
39    {
40
41       Object JavaDoc retval = null;
42
43       if (!MethodDeclarations.isTransactionLifecycleMethod(m.getMethodId()))
44       {
45          if (log.isTraceEnabled()) log.trace("Passing up method " + m + " so it gets invoked on cache.");
46          try
47          {
48             //retval = super.invoke(m);
49
retval = m.invoke(cache);
50          }
51          catch (Throwable JavaDoc t)
52          {
53             retval = t;
54          }
55       }
56       else
57       {
58          if (log.isTraceEnabled()) log.trace("Suppressing invocation of method " + m + " on cache.");
59       }
60
61       InvocationContext ctx = cache.getInvocationContext();
62       Transaction tx = ctx.getTransaction();
63       if (tx != null && isValid(tx))
64       {
65          // test for exceptions.
66
if (retval instanceof Throwable JavaDoc)
67          {
68             tx.setRollbackOnly(); // no exception, regular return
69
}
70          else
71          {
72             // only add the modification to the modification list if we are using pessimistic locking.
73
// Optimistic locking calls *should* not make it this far down the interceptor chain, but just
74
// in case a method has been invoked that the OptimisticNodeInterceptor knows nothing about, it will
75
// filter down here.
76

77             if (!configuration.isNodeLockingOptimistic() && MethodDeclarations.isCrudMethod(m.getMethodId()))
78             {
79                // if method is a CRUD (Create/Remove/Update/Delete) method: add it to the modification
80
// list, otherwise skip (e.g. get() is not added)
81
// add the modification to the TX's modification list. this is used to later
82
// (on TX commit) send all modifications done in this TX to all members
83
GlobalTransaction gtx = ctx.getGlobalTransaction();
84                if (gtx == null)
85                {
86                   if (log.isDebugEnabled())
87                   {
88                      log.debug("didn't find GlobalTransaction for " + tx + "; won't add modification to transaction list");
89                   }
90                }
91                else
92                {
93                   Option o = cache.getInvocationContext().getOptionOverrides();
94                   if (o != null && o.isCacheModeLocal())
95                   {
96                      log.debug("Not adding method to modification list since cache mode local is set.");
97                   }
98                   else
99                   {
100                      cache.getTransactionTable().addModification(gtx, m);
101                   }
102                   if (cache.getCacheLoaderManager() != null)
103                      cache.getTransactionTable().addCacheLoaderModification(gtx, m);
104                }
105             }
106          }
107       }
108
109       if (retval instanceof Throwable JavaDoc)
110       {
111          throw (Throwable JavaDoc) retval;
112       }
113
114       return retval;
115    }
116 }
117
Popular Tags