KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.interceptors;
8
9 import org.jboss.cache.GlobalTransaction;
10 import org.jboss.cache.InvocationContext;
11 import org.jboss.cache.config.Option;
12 import org.jboss.cache.marshall.MethodCall;
13 import org.jboss.cache.marshall.MethodDeclarations;
14
15 import javax.transaction.SystemException JavaDoc;
16 import javax.transaction.Transaction JavaDoc;
17
18 /**
19  * Always place this interceptor at the start of the interceptor chain to ensure invocation contexts and set up and cleaned up correctly.
20  *
21  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
22  */

23 public class InvocationContextInterceptor extends BaseTransactionalContextInterceptor implements InvocationContextInterceptorMBean
24 {
25    public Object JavaDoc invoke(MethodCall call) throws Throwable JavaDoc
26    {
27       InvocationContext ctx = cache.getInvocationContext();
28       Option optionOverride = ctx.getOptionOverrides();
29       Transaction suspendedTransaction = null;
30       boolean resumeSuspended = false;
31
32       if (log.isTraceEnabled())
33          log.trace("Invoked on cache instance [" + cache.getLocalAddress() + "] and InvocationContext [" + ctx + "]");
34
35       try
36       {
37          Transaction tx = getTransaction();
38          setTransactionalContext(tx, getGlobalTransaction(tx, call));
39
40          if (optionOverride != null)
41          {
42
43             if (optionOverride.isFailSilently())
44             {
45                log.debug("FAIL_SILENTLY Option is present - suspending any ongoing transaction.");
46                if (ctx.getTransaction() != null)
47                {
48                   suspendedTransaction = txManager.suspend();
49                   setTransactionalContext(null, null);
50                   if (log.isTraceEnabled()) log.trace("Suspending transaction " + suspendedTransaction);
51                   resumeSuspended = true;
52                }
53                else
54                {
55                   log.trace("No ongoing transaction to suspend");
56                }
57             }
58
59             if (optionOverride.isBypassInterceptorChain())
60             {
61                log.trace("Interceptor chain bypass option set for call; skipping interceptor chain and proceeding to last interceptor.");
62                return getLast().invoke(call);
63             }
64          }
65
66          return super.invoke(call);
67
68       }
69       finally
70       {
71          // clean up any invocation-scope options set up
72
log.trace("Resetting invocation-scope options");
73          cache.getInvocationContext().getOptionOverrides().reset();
74
75          if (resumeSuspended)
76          {
77             txManager.resume(suspendedTransaction);
78          }
79          else
80          {
81             if (ctx.getTransaction() != null && (isValid(ctx.getTransaction())))// || isRollingBack(ctx.getTransaction())))
82
{
83                copyInvocationScopeOptionsToTxScope(ctx);
84             }
85          }
86       }
87    }
88
89    private GlobalTransaction getGlobalTransaction(Transaction tx, MethodCall call)
90    {
91       GlobalTransaction gtx = null;
92       if (MethodDeclarations.isTransactionLifecycleMethod(call.getMethodId()))
93       {
94          gtx = findGlobalTransaction(call.getArgs());
95          gtx.setRemote(isRemoteGlobalTx(gtx));
96       }
97       else
98       {
99          gtx = cache.getCurrentTransaction(tx, false);
100       }
101
102       return gtx;
103    }
104
105    private Transaction getTransaction() throws SystemException JavaDoc
106    {
107       // this creates a context if one did not exist.
108
if (txManager == null)
109       {
110          log.trace("no transaction manager configured, setting tx as null.");
111          return null;
112       }
113       else
114       {
115          return txManager.getTransaction();
116       }
117    }
118
119    protected GlobalTransaction findGlobalTransaction(Object JavaDoc[] params)
120    {
121       int clue = 0;
122
123       if (params[clue] instanceof GlobalTransaction)
124          return (GlobalTransaction) params[clue];
125       else
126          for (Object JavaDoc param : params) if (param instanceof GlobalTransaction) return (GlobalTransaction) param;
127       return null;
128    }
129
130    /**
131     * Tests if a global transaction originated from a different cache in the cluster
132     *
133     * @param gtx
134     * @return true if the gtx is remote, false if it originated locally.
135     */

136    private boolean isRemoteGlobalTx(GlobalTransaction gtx)
137    {
138       return gtx != null && (gtx.getAddress() != null) && (!gtx.getAddress().equals(cache.getLocalAddress()));
139    }
140 }
141
Popular Tags