View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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          this(xac, null);
34      }
35  
36      public ConnectionInvocationHandler(Object xac, Boolean sameRMOverrideValue)
37      {
38          this.xaConnection = xac;
39          this.sameRMOverrideValue = sameRMOverrideValue;
40      }
41  
42      public Object getTargetObject()
43      {
44          return xaConnection;
45      }
46  
47      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
48      {
49          if (ConnectionFactoryWrapper.logger.isDebugEnabled())
50          {
51              ConnectionFactoryWrapper.logger.debug("Invoking " + method);
52          }
53          
54          Transaction tx = TransactionCoordination.getInstance().getTransaction();
55          
56          if (method.getName().equals("createSession"))
57          {
58              if (tx != null)
59              {
60                  XASession xas = ((XAConnection) xaConnection).createXASession();
61                  return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 
62                      new Class[]{ Session.class, XaTransaction.MuleXaObject.class },
63                      new SessionInvocationHandler(xas, sameRMOverrideValue));
64              }
65              else
66              {
67                  return ((XAConnection) xaConnection).createSession(false, Session.AUTO_ACKNOWLEDGE);
68              }
69          }
70          else if (method.getName().equals("createQueueSession"))
71          {
72              if (tx != null)
73              {
74                  XAQueueSession xaqs = ((XAQueueConnection) xaConnection).createXAQueueSession();
75                  return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
76                      new Class[]{ QueueSession.class, XaTransaction.MuleXaObject.class }, 
77                      new SessionInvocationHandler(xaqs, sameRMOverrideValue));
78              }
79              else
80              {
81                  return ((XAQueueConnection) xaConnection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
82              }
83          }
84          else if (method.getName().equals("createTopicSession"))
85          {
86              if (tx != null)
87              {
88                  XATopicSession xats = ((XATopicConnection) xaConnection).createXATopicSession();
89                  return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
90                      new Class[]{ TopicSession.class, XaTransaction.MuleXaObject.class }, 
91                      new SessionInvocationHandler(xats, sameRMOverrideValue));
92              }
93              else
94              {
95                  return ((XATopicConnection) xaConnection).createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
96              }
97          }
98          else
99          {
100             return method.invoke(xaConnection, args);
101         }
102     }
103 }