View Javadoc

1   /*
2    * $Id: ConnectionInvocationHandler.java 12181 2008-06-26 20:05:55Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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      {
37          this.xaConnection = xac;
38      }
39  
40      /**
41       * Can be one of 3 types.
42       * TODO check if we can portably cast it (JMS 1.1 vs 1.0.2b), see Jms102bSupport why
43       *
44       * @return underlying XAConnection instance
45       */
46      public Object getTargetObject()
47      {
48          return xaConnection;
49      }
50  
51      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
52      {
53          if (ConnectionFactoryWrapper.logger.isDebugEnabled())
54          {
55              ConnectionFactoryWrapper.logger.debug("Invoking " + method);
56          }
57          
58          Transaction tx = TransactionCoordination.getInstance().getTransaction();
59          
60          if (method.getName().equals("createSession"))
61          {
62              if (tx != null)
63              {
64                  XASession xas = ((XAConnection) xaConnection).createXASession();
65                  return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 
66                      new Class[]{ Session.class, XaTransaction.MuleXaObject.class },
67                      new SessionInvocationHandler(xas));
68              }
69              else
70              {
71                  return ((XAConnection) xaConnection).createSession(false, Session.AUTO_ACKNOWLEDGE);
72              }
73          }
74          else if (method.getName().equals("createQueueSession"))
75          {
76              if (tx != null)
77              {
78                  XAQueueSession xaqs = ((XAQueueConnection) xaConnection).createXAQueueSession();
79                  return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
80                      new Class[]{ QueueSession.class, XaTransaction.MuleXaObject.class }, 
81                      new SessionInvocationHandler(xaqs));
82              }
83              else
84              {
85                  return ((XAQueueConnection) xaConnection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
86              }
87          }
88          else if (method.getName().equals("createTopicSession"))
89          {
90              if (tx != null)
91              {
92                  XATopicSession xats = ((XATopicConnection) xaConnection).createXATopicSession();
93                  return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
94                      new Class[]{ TopicSession.class, XaTransaction.MuleXaObject.class }, 
95                      new SessionInvocationHandler(xats));
96              }
97              else
98              {
99                  return ((XATopicConnection) xaConnection).createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
100             }
101         }
102         else
103         {
104             return method.invoke(xaConnection, args);
105         }
106     }
107 
108 }