1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.interceptors; |
8 |
| |
9 |
| import org.jboss.cache.InvocationContext; |
10 |
| import org.jboss.cache.config.Option; |
11 |
| import org.jboss.cache.marshall.MethodCall; |
12 |
| import org.jboss.cache.marshall.MethodDeclarations; |
13 |
| import org.jboss.cache.transaction.GlobalTransaction; |
14 |
| |
15 |
| import javax.transaction.SystemException; |
16 |
| import javax.transaction.Transaction; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class InvocationContextInterceptor extends BaseTransactionalContextInterceptor implements InvocationContextInterceptorMBean |
24 |
| { |
25 |
2483249
| public Object invoke(InvocationContext ctx) throws Throwable
|
26 |
| { |
27 |
2483249
| MethodCall call = ctx.getMethodCall();
|
28 |
2483249
| Option optionOverride = ctx.getOptionOverrides();
|
29 |
2483249
| boolean suppressExceptions = false;
|
30 |
2483249
| Transaction suspendedTransaction = null;
|
31 |
2483249
| boolean resumeSuspended = false;
|
32 |
| |
33 |
2483247
| if (log.isTraceEnabled())
|
34 |
0
| log.trace("Invoked on cache instance [" + cache.getLocalAddress() + "] and InvocationContext [" + ctx + "]");
|
35 |
| |
36 |
2483249
| try
|
37 |
| { |
38 |
2483249
| Transaction tx = getTransaction();
|
39 |
2483249
| setTransactionalContext(tx, getGlobalTransaction(tx, call), ctx);
|
40 |
| |
41 |
2483249
| if (optionOverride != null)
|
42 |
| { |
43 |
| |
44 |
2483249
| if (optionOverride.isFailSilently())
|
45 |
| { |
46 |
29
| log.debug("FAIL_SILENTLY Option is present - suspending any ongoing transaction.");
|
47 |
29
| suppressExceptions = true;
|
48 |
29
| if (ctx.getTransaction() != null)
|
49 |
| { |
50 |
6
| suspendedTransaction = txManager.suspend();
|
51 |
6
| setTransactionalContext(null, null, ctx);
|
52 |
0
| if (log.isTraceEnabled()) log.trace("Suspending transaction " + suspendedTransaction);
|
53 |
6
| resumeSuspended = true;
|
54 |
| } |
55 |
| else |
56 |
| { |
57 |
23
| log.trace("No ongoing transaction to suspend");
|
58 |
| } |
59 |
| } |
60 |
| } |
61 |
| |
62 |
2483249
| Object retval = super.invoke(ctx);
|
63 |
| |
64 |
2483174
| if (retval instanceof Throwable)
|
65 |
| { |
66 |
| |
67 |
2
| if (suppressExceptions) return null;
|
68 |
| |
69 |
8
| Throwable t = (Throwable) retval;
|
70 |
8
| if (t instanceof RuntimeException)
|
71 |
0
| throw t.getCause();
|
72 |
| else |
73 |
8
| throw t;
|
74 |
| } |
75 |
2483164
| return retval;
|
76 |
| } |
77 |
| finally |
78 |
| { |
79 |
| |
80 |
2483248
| log.trace("Resetting invocation-scope options");
|
81 |
2483248
| ctx.getOptionOverrides().reset();
|
82 |
| |
83 |
2483248
| if (resumeSuspended)
|
84 |
| { |
85 |
6
| txManager.resume(suspendedTransaction);
|
86 |
| } |
87 |
| else |
88 |
| { |
89 |
2483242
| if (ctx.getTransaction() != null && (isValid(ctx.getTransaction())))
|
90 |
| { |
91 |
1398459
| copyInvocationScopeOptionsToTxScope(ctx);
|
92 |
| } |
93 |
| } |
94 |
| } |
95 |
| } |
96 |
| |
97 |
2483249
| private GlobalTransaction getGlobalTransaction(Transaction tx, MethodCall call)
|
98 |
| { |
99 |
2483249
| GlobalTransaction gtx = null;
|
100 |
2483249
| if (MethodDeclarations.isTransactionLifecycleMethod(call.getMethodId()))
|
101 |
| { |
102 |
1473
| gtx = findGlobalTransaction(call.getArgs());
|
103 |
1473
| gtx.setRemote(isRemoteGlobalTx(gtx));
|
104 |
| } |
105 |
| else |
106 |
| { |
107 |
2481776
| gtx = cache.getCurrentTransaction(tx, false);
|
108 |
| } |
109 |
| |
110 |
2483249
| return gtx;
|
111 |
| } |
112 |
| |
113 |
2483249
| private Transaction getTransaction() throws SystemException
|
114 |
| { |
115 |
| |
116 |
2483249
| if (txManager == null)
|
117 |
| { |
118 |
114172
| log.trace("no transaction manager configured, setting tx as null.");
|
119 |
114172
| return null;
|
120 |
| } |
121 |
| else |
122 |
| { |
123 |
2369077
| return txManager.getTransaction();
|
124 |
| } |
125 |
| } |
126 |
| |
127 |
1473
| protected GlobalTransaction findGlobalTransaction(Object[] params)
|
128 |
| { |
129 |
1473
| int clue = 0;
|
130 |
| |
131 |
1473
| if (params[clue] instanceof GlobalTransaction)
|
132 |
1473
| return (GlobalTransaction) params[clue];
|
133 |
| else |
134 |
0
| for (Object param : params) if (param instanceof GlobalTransaction) return (GlobalTransaction) param;
|
135 |
0
| return null;
|
136 |
| } |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
1473
| private boolean isRemoteGlobalTx(GlobalTransaction gtx)
|
145 |
| { |
146 |
1473
| return gtx != null && (gtx.getAddress() != null) && (!gtx.getAddress().equals(cache.getLocalAddress()));
|
147 |
| } |
148 |
| } |