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