Coverage Report - org.mule.transport.jms.xa.ConnectionInvocationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectionInvocationHandler
57%
13/23
43%
6/14
5.333
 
 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  14
     {
 37  14
         this.xaConnection = xac;
 38  14
     }
 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  14
         return xaConnection;
 49  
     }
 50  
 
 51  
     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
 52  
     {
 53  607
         if (ConnectionFactoryWrapper.logger.isDebugEnabled())
 54  
         {
 55  0
             ConnectionFactoryWrapper.logger.debug("Invoking " + method);
 56  
         }
 57  
         
 58  607
         Transaction tx = TransactionCoordination.getInstance().getTransaction();
 59  
         
 60  607
         if (method.getName().equals("createSession"))
 61  
         {
 62  66
             if (tx != null)
 63  
             {
 64  66
                 XASession xas = ((XAConnection) xaConnection).createXASession();
 65  66
                 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 
 66  
                     new Class[]{ Session.class, XaTransaction.MuleXaObject.class },
 67  
                     new SessionInvocationHandler(xas));
 68  
             }
 69  
             else
 70  
             {
 71  0
                 return ((XAConnection) xaConnection).createSession(false, Session.AUTO_ACKNOWLEDGE);
 72  
             }
 73  
         }
 74  541
         else if (method.getName().equals("createQueueSession"))
 75  
         {
 76  0
             if (tx != null)
 77  
             {
 78  0
                 XAQueueSession xaqs = ((XAQueueConnection) xaConnection).createXAQueueSession();
 79  0
                 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
 80  
                     new Class[]{ QueueSession.class, XaTransaction.MuleXaObject.class }, 
 81  
                     new SessionInvocationHandler(xaqs));
 82  
             }
 83  
             else
 84  
             {
 85  0
                 return ((XAQueueConnection) xaConnection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
 86  
             }
 87  
         }
 88  541
         else if (method.getName().equals("createTopicSession"))
 89  
         {
 90  0
             if (tx != null)
 91  
             {
 92  0
                 XATopicSession xats = ((XATopicConnection) xaConnection).createXATopicSession();
 93  0
                 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
 94  
                     new Class[]{ TopicSession.class, XaTransaction.MuleXaObject.class }, 
 95  
                     new SessionInvocationHandler(xats));
 96  
             }
 97  
             else
 98  
             {
 99  0
                 return ((XATopicConnection) xaConnection).createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
 100  
             }
 101  
         }
 102  
         else
 103  
         {
 104  541
             return method.invoke(xaConnection, args);
 105  
         }
 106  
     }
 107  
 
 108  
 }