View Javadoc

1   /*
2    * $Id: ConnectionInvocationHandler.java 10465 2008-01-22 20:17:19Z akuzmin $
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.providers.jms.xa;
11  
12  import org.mule.transaction.XaTransaction;
13  
14  import java.lang.reflect.InvocationHandler;
15  import java.lang.reflect.Method;
16  import java.lang.reflect.Proxy;
17  
18  import javax.jms.QueueSession;
19  import javax.jms.Session;
20  import javax.jms.TopicSession;
21  import javax.jms.XAConnection;
22  import javax.jms.XAQueueConnection;
23  import javax.jms.XAQueueSession;
24  import javax.jms.XASession;
25  import javax.jms.XATopicConnection;
26  import javax.jms.XATopicSession;
27  
28  public class ConnectionInvocationHandler implements InvocationHandler
29  {
30  
31      private Object xaConnection;
32  
33      public ConnectionInvocationHandler(Object xac)
34      {
35          this.xaConnection = xac;
36      }
37  
38      /**
39       * Can be one of 3 types.
40       * TODO check if we can portably cast it (JMS 1.1 vs 1.0.2b), see Jms102bSupport why
41       *
42       * @return underlying XAConnection instance
43       */
44      public Object getTargetObject()
45      {
46          return xaConnection;
47      }
48  
49      /*
50       * (non-Javadoc)
51       *
52       * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
53       *      java.lang.reflect.Method, java.lang.Object[])
54       */
55      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
56      {
57          if (ConnectionFactoryWrapper.logger.isDebugEnabled())
58          {
59              ConnectionFactoryWrapper.logger.debug("Invoking " + method);
60          }
61          if (method.getName().equals("createSession"))
62          {
63              XASession xas = ((XAConnection) xaConnection).createXASession();
64              return Proxy.newProxyInstance(Session.class.getClassLoader(), new Class[]{Session.class, XaTransaction.MuleXaObject.class},
65                                            new SessionInvocationHandler(xas));
66          }
67          else if (method.getName().equals("createQueueSession"))
68          {
69              XAQueueSession xaqs = ((XAQueueConnection) xaConnection).createXAQueueSession();
70              return Proxy.newProxyInstance(Session.class.getClassLoader(),
71                                            new Class[]{QueueSession.class, XaTransaction.MuleXaObject.class}, new SessionInvocationHandler(xaqs));
72          }
73          else if (method.getName().equals("createTopicSession"))
74          {
75              XATopicSession xats = ((XATopicConnection) xaConnection).createXATopicSession();
76              return Proxy.newProxyInstance(Session.class.getClassLoader(),
77                                            new Class[]{TopicSession.class, XaTransaction.MuleXaObject.class}, new SessionInvocationHandler(xats));
78          }
79          else
80          {
81              return method.invoke(xaConnection, args);
82          }
83      }
84  
85  }