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.config.i18n.CoreMessages; |
14 | |
import org.mule.transaction.IllegalTransactionStateException; |
15 | |
import org.mule.transaction.TransactionCoordination; |
16 | |
import org.mule.transaction.XaTransaction; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.lang.reflect.Proxy; |
20 | |
|
21 | |
import javax.jms.JMSException; |
22 | |
import javax.jms.MessageConsumer; |
23 | |
import javax.jms.MessageProducer; |
24 | |
import javax.jms.QueueReceiver; |
25 | |
import javax.jms.QueueSender; |
26 | |
import javax.jms.Session; |
27 | |
import javax.jms.TopicPublisher; |
28 | |
import javax.jms.TopicSubscriber; |
29 | |
import javax.jms.XAQueueSession; |
30 | |
import javax.jms.XASession; |
31 | |
import javax.jms.XATopicSession; |
32 | |
import javax.transaction.xa.XAResource; |
33 | |
|
34 | |
import org.apache.commons.logging.Log; |
35 | |
import org.apache.commons.logging.LogFactory; |
36 | |
|
37 | |
public class SessionInvocationHandler implements TargetInvocationHandler |
38 | |
{ |
39 | 0 | protected static final transient Log logger = LogFactory.getLog(SessionInvocationHandler.class); |
40 | |
|
41 | |
private XASession xaSession; |
42 | |
private XAResource xaResource; |
43 | 0 | private volatile boolean enlisted = false; |
44 | 0 | private volatile boolean reuseObject = false; |
45 | |
private final Session session; |
46 | |
|
47 | |
private SessionInvocationHandler(XASession xaSession, Session session, Boolean sameRMOverrideValue) |
48 | |
{ |
49 | 0 | super(); |
50 | 0 | this.xaSession = xaSession; |
51 | 0 | this.session = session; |
52 | 0 | this.xaResource = new XAResourceWrapper(xaSession.getXAResource(), this, sameRMOverrideValue); |
53 | 0 | } |
54 | |
|
55 | |
public SessionInvocationHandler(XASession xaSession, Boolean sameRMOverrideValue) throws JMSException |
56 | |
{ |
57 | 0 | this(xaSession, xaSession.getSession(), sameRMOverrideValue); |
58 | 0 | } |
59 | |
|
60 | |
public SessionInvocationHandler(XAQueueSession xaSession, Boolean sameRMOverrideValue) throws JMSException |
61 | |
{ |
62 | 0 | this(xaSession, xaSession.getQueueSession(), sameRMOverrideValue); |
63 | 0 | } |
64 | |
|
65 | |
public SessionInvocationHandler(XATopicSession xaSession, Boolean sameRMOverrideValue) throws JMSException |
66 | |
{ |
67 | 0 | this(xaSession, xaSession.getTopicSession(), sameRMOverrideValue); |
68 | 0 | } |
69 | |
|
70 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
71 | |
{ |
72 | 0 | if (logger.isDebugEnabled()) |
73 | |
{ |
74 | 0 | logger.debug(this + " Invoking " + method); |
75 | |
} |
76 | |
|
77 | |
|
78 | 0 | if (XaTransaction.MuleXaObject.DELIST_METHOD_NAME.equals(method.getName())) |
79 | |
{ |
80 | 0 | return delist(); |
81 | |
} |
82 | 0 | else if (XaTransaction.MuleXaObject.ENLIST_METHOD_NAME.equals(method.getName())) |
83 | |
{ |
84 | 0 | return enlist(); |
85 | |
} |
86 | 0 | else if (XaTransaction.MuleXaObject.SET_REUSE_OBJECT_METHOD_NAME.equals(method.getName())) |
87 | |
{ |
88 | 0 | reuseObject = (Boolean) args[0]; |
89 | 0 | return null; |
90 | |
} |
91 | 0 | else if (XaTransaction.MuleXaObject.IS_REUSE_OBJECT_METHOD_NAME.equals(method.getName())) |
92 | |
{ |
93 | 0 | return reuseObject; |
94 | |
} |
95 | 0 | else if (XaTransaction.MuleXaObject.GET_TARGET_OBJECT_METHOD_NAME.equals(method.getName())) |
96 | |
{ |
97 | 0 | return getTargetObject(); |
98 | |
} |
99 | 0 | else if (XaTransaction.MuleXaObject.CLOSE_METHOD_NAME.equals(method.getName())) |
100 | |
{ |
101 | |
|
102 | |
try |
103 | |
{ |
104 | 0 | session.close(); |
105 | 0 | xaSession.close(); |
106 | |
} |
107 | 0 | catch (Exception ex) |
108 | |
{ |
109 | 0 | if (logger.isDebugEnabled()) |
110 | |
{ |
111 | 0 | logger.debug("Closing the session and the xaSession failed", ex); |
112 | |
} |
113 | 0 | } |
114 | 0 | return null; |
115 | |
} |
116 | |
|
117 | 0 | Object result = method.invoke(session, args); |
118 | |
|
119 | 0 | if (result instanceof TopicSubscriber) |
120 | |
{ |
121 | 0 | result = Proxy.newProxyInstance(Session.class.getClassLoader(), |
122 | |
new Class[]{TopicSubscriber.class}, new ConsumerProducerInvocationHandler(this, result)); |
123 | |
} |
124 | 0 | else if (result instanceof QueueReceiver) |
125 | |
{ |
126 | 0 | result = Proxy.newProxyInstance(Session.class.getClassLoader(), |
127 | |
new Class[]{QueueReceiver.class}, new ConsumerProducerInvocationHandler(this, result)); |
128 | |
} |
129 | 0 | else if (result instanceof MessageConsumer) |
130 | |
{ |
131 | 0 | result = Proxy.newProxyInstance(Session.class.getClassLoader(), |
132 | |
new Class[]{MessageConsumer.class}, new ConsumerProducerInvocationHandler(this, result)); |
133 | |
} |
134 | 0 | else if (result instanceof TopicPublisher) |
135 | |
{ |
136 | 0 | result = Proxy.newProxyInstance(Session.class.getClassLoader(), |
137 | |
new Class[]{TopicPublisher.class}, new ConsumerProducerInvocationHandler(this, result)); |
138 | |
} |
139 | 0 | else if (result instanceof QueueSender) |
140 | |
{ |
141 | 0 | result = Proxy.newProxyInstance(Session.class.getClassLoader(), |
142 | |
new Class[]{QueueSender.class}, new ConsumerProducerInvocationHandler(this, result)); |
143 | |
} |
144 | 0 | else if (result instanceof MessageProducer) |
145 | |
{ |
146 | 0 | result = Proxy.newProxyInstance(Session.class.getClassLoader(), |
147 | |
new Class[]{MessageProducer.class}, new ConsumerProducerInvocationHandler(this, result)); |
148 | |
} |
149 | 0 | return result; |
150 | |
} |
151 | |
|
152 | |
public boolean enlist() throws Exception |
153 | |
{ |
154 | 0 | if (isEnlisted()) |
155 | |
{ |
156 | 0 | return false; |
157 | |
} |
158 | |
|
159 | 0 | if (logger.isDebugEnabled()) |
160 | |
{ |
161 | 0 | logger.debug("Enlistment request: " + this); |
162 | |
} |
163 | |
|
164 | 0 | Transaction transaction = TransactionCoordination.getInstance().getTransaction(); |
165 | 0 | if (transaction == null) |
166 | |
{ |
167 | 0 | throw new IllegalTransactionStateException(CoreMessages.noMuleTransactionAvailable()); |
168 | |
} |
169 | 0 | if (!(transaction instanceof XaTransaction)) |
170 | |
{ |
171 | 0 | throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(transaction)); |
172 | |
} |
173 | |
|
174 | 0 | if (!isEnlisted()) |
175 | |
{ |
176 | 0 | if (logger.isDebugEnabled()) |
177 | |
{ |
178 | 0 | logger.debug("Enlisting resource " + xaResource + " in xa transaction " + transaction); |
179 | |
} |
180 | |
|
181 | 0 | enlisted = ((XaTransaction) transaction).enlistResource(xaResource); |
182 | |
} |
183 | |
|
184 | 0 | return enlisted; |
185 | |
} |
186 | |
|
187 | |
public boolean delist() throws Exception |
188 | |
{ |
189 | 0 | if (!isEnlisted()) |
190 | |
{ |
191 | 0 | return false; |
192 | |
} |
193 | |
|
194 | 0 | if (logger.isDebugEnabled()) |
195 | |
{ |
196 | 0 | logger.debug("Delistment request: " + this); |
197 | |
} |
198 | |
|
199 | 0 | Transaction transaction = TransactionCoordination.getInstance().getTransaction(); |
200 | 0 | if (transaction == null) |
201 | |
{ |
202 | 0 | throw new IllegalTransactionStateException(CoreMessages.noMuleTransactionAvailable()); |
203 | |
} |
204 | 0 | if (!(transaction instanceof XaTransaction)) |
205 | |
{ |
206 | 0 | throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(transaction)); |
207 | |
} |
208 | |
|
209 | 0 | if (isEnlisted()) |
210 | |
{ |
211 | 0 | if (logger.isDebugEnabled()) |
212 | |
{ |
213 | 0 | logger.debug("Delisting resource " + xaResource + " in xa transaction " + transaction); |
214 | |
} |
215 | |
|
216 | 0 | enlisted = !((XaTransaction) transaction).delistResource(xaResource, XAResource.TMSUCCESS); |
217 | |
} |
218 | 0 | return !isEnlisted(); |
219 | |
} |
220 | |
|
221 | |
public boolean isEnlisted() |
222 | |
{ |
223 | 0 | return enlisted; |
224 | |
} |
225 | |
|
226 | |
public void setEnlisted(boolean enlisted) |
227 | |
{ |
228 | 0 | this.enlisted = enlisted; |
229 | 0 | } |
230 | |
|
231 | |
public Object getTargetObject() |
232 | |
{ |
233 | 0 | return xaSession; |
234 | |
} |
235 | |
|
236 | |
public XAResource getXAResource() |
237 | |
{ |
238 | 0 | return xaResource; |
239 | |
} |
240 | |
|
241 | |
} |