View Javadoc

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