1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.jms.xa; |
8 | |
|
9 | |
import org.mule.api.transaction.Transaction; |
10 | |
import org.mule.transaction.TransactionCoordination; |
11 | |
import org.mule.transaction.XaTransaction; |
12 | |
|
13 | |
import java.lang.reflect.Method; |
14 | |
import java.lang.reflect.Proxy; |
15 | |
|
16 | |
import javax.jms.QueueSession; |
17 | |
import javax.jms.Session; |
18 | |
import javax.jms.TopicSession; |
19 | |
import javax.jms.XAConnection; |
20 | |
import javax.jms.XAQueueConnection; |
21 | |
import javax.jms.XAQueueSession; |
22 | |
import javax.jms.XASession; |
23 | |
import javax.jms.XATopicConnection; |
24 | |
import javax.jms.XATopicSession; |
25 | |
|
26 | |
public class ConnectionInvocationHandler implements TargetInvocationHandler |
27 | |
{ |
28 | |
private Object xaConnection; |
29 | |
private Boolean sameRMOverrideValue; |
30 | |
|
31 | |
public ConnectionInvocationHandler(Object xac) |
32 | |
{ |
33 | 0 | this(xac, null); |
34 | 0 | } |
35 | |
|
36 | |
public ConnectionInvocationHandler(Object xac, Boolean sameRMOverrideValue) |
37 | 0 | { |
38 | 0 | this.xaConnection = xac; |
39 | 0 | this.sameRMOverrideValue = sameRMOverrideValue; |
40 | 0 | } |
41 | |
|
42 | |
public Object getTargetObject() |
43 | |
{ |
44 | 0 | return xaConnection; |
45 | |
} |
46 | |
|
47 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
48 | |
{ |
49 | 0 | if (ConnectionFactoryWrapper.logger.isDebugEnabled()) |
50 | |
{ |
51 | 0 | ConnectionFactoryWrapper.logger.debug("Invoking " + method); |
52 | |
} |
53 | |
|
54 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
55 | |
|
56 | 0 | if (method.getName().equals("createSession")) |
57 | |
{ |
58 | 0 | if (tx != null) |
59 | |
{ |
60 | 0 | XASession xas = ((XAConnection) xaConnection).createXASession(); |
61 | 0 | return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), |
62 | |
new Class[]{ Session.class, XaTransaction.MuleXaObject.class }, |
63 | |
new SessionInvocationHandler(xas, sameRMOverrideValue)); |
64 | |
} |
65 | |
else |
66 | |
{ |
67 | 0 | return ((XAConnection) xaConnection).createSession(false, Session.AUTO_ACKNOWLEDGE); |
68 | |
} |
69 | |
} |
70 | 0 | else if (method.getName().equals("createQueueSession")) |
71 | |
{ |
72 | 0 | if (tx != null) |
73 | |
{ |
74 | 0 | XAQueueSession xaqs = ((XAQueueConnection) xaConnection).createXAQueueSession(); |
75 | 0 | return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), |
76 | |
new Class[]{ QueueSession.class, XaTransaction.MuleXaObject.class }, |
77 | |
new SessionInvocationHandler(xaqs, sameRMOverrideValue)); |
78 | |
} |
79 | |
else |
80 | |
{ |
81 | 0 | return ((XAQueueConnection) xaConnection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE); |
82 | |
} |
83 | |
} |
84 | 0 | else if (method.getName().equals("createTopicSession")) |
85 | |
{ |
86 | 0 | if (tx != null) |
87 | |
{ |
88 | 0 | XATopicSession xats = ((XATopicConnection) xaConnection).createXATopicSession(); |
89 | 0 | return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), |
90 | |
new Class[]{ TopicSession.class, XaTransaction.MuleXaObject.class }, |
91 | |
new SessionInvocationHandler(xats, sameRMOverrideValue)); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | return ((XATopicConnection) xaConnection).createTopicSession(false, Session.AUTO_ACKNOWLEDGE); |
96 | |
} |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | return method.invoke(xaConnection, args); |
101 | |
} |
102 | |
} |
103 | |
} |