1 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 ; 16 import javax.transaction.Transaction ; 17 18 23 public class InvocationContextInterceptor extends BaseTransactionalContextInterceptor implements InvocationContextInterceptorMBean 24 { 25 public Object invoke(MethodCall call) throws Throwable 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 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()))) { 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 106 { 107 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 [] params) 120 { 121 int clue = 0; 122 123 if (params[clue] instanceof GlobalTransaction) 124 return (GlobalTransaction) params[clue]; 125 else 126 for (Object param : params) if (param instanceof GlobalTransaction) return (GlobalTransaction) param; 127 return null; 128 } 129 130 136 private boolean isRemoteGlobalTx(GlobalTransaction gtx) 137 { 138 return gtx != null && (gtx.getAddress() != null) && (!gtx.getAddress().equals(cache.getLocalAddress())); 139 } 140 } 141 | Popular Tags |