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 java.lang.reflect.Proxy;
10  
11  import javax.jms.Connection;
12  import javax.jms.ConnectionFactory;
13  import javax.jms.JMSException;
14  import javax.jms.QueueConnection;
15  import javax.jms.QueueConnectionFactory;
16  import javax.jms.TopicConnection;
17  import javax.jms.TopicConnectionFactory;
18  import javax.jms.XAConnection;
19  import javax.jms.XAConnectionFactory;
20  import javax.jms.XAQueueConnection;
21  import javax.jms.XAQueueConnectionFactory;
22  import javax.jms.XATopicConnection;
23  import javax.jms.XATopicConnectionFactory;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  public class ConnectionFactoryWrapper
29          implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory
30  {
31      /**
32       * logger used by this class
33       */
34      protected static final transient Log logger = LogFactory.getLog(ConnectionFactoryWrapper.class);
35  
36      protected final Object factory;
37      private Boolean sameRMOverrideValue;
38  
39      public ConnectionFactoryWrapper(Object factory)
40      {
41          this(factory, null);
42      }
43  
44      public ConnectionFactoryWrapper(Object factory, Boolean sameRMOverrideValue)
45      {
46          this.factory = factory;
47          this.sameRMOverrideValue = sameRMOverrideValue;
48      }
49  
50      public Connection createConnection() throws JMSException
51      {
52          XAConnection xac = ((XAConnectionFactory) factory).createXAConnection();
53          Connection proxy = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
54                                                                 new Class[]{Connection.class},
55                                                                 new ConnectionInvocationHandler(xac, sameRMOverrideValue));
56          return proxy;
57      }
58  
59      public Connection createConnection(String username, String password) throws JMSException
60      {
61          XAConnection xac = ((XAConnectionFactory) factory).createXAConnection(username, password);
62          Connection proxy = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
63                                                                 new Class[]{Connection.class},
64                                                                 new ConnectionInvocationHandler(xac, sameRMOverrideValue));
65          return proxy;
66      }
67  
68      public QueueConnection createQueueConnection() throws JMSException
69      {
70          XAQueueConnection xaqc = ((XAQueueConnectionFactory) factory).createXAQueueConnection();
71          QueueConnection proxy = (QueueConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
72                                                                           new Class[]{QueueConnection.class},
73                                                                           new ConnectionInvocationHandler(xaqc, sameRMOverrideValue));
74          return proxy;
75      }
76  
77      public QueueConnection createQueueConnection(String username, String password) throws JMSException
78      {
79          XAQueueConnection xaqc = ((XAQueueConnectionFactory) factory).createXAQueueConnection(username,
80                                                                                                password);
81          QueueConnection proxy = (QueueConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
82                                                                           new Class[]{QueueConnection.class},
83                                                                           new ConnectionInvocationHandler(xaqc, sameRMOverrideValue));
84          return proxy;
85      }
86  
87      public TopicConnection createTopicConnection() throws JMSException
88      {
89          XATopicConnection xatc = ((XATopicConnectionFactory) factory).createXATopicConnection();
90          TopicConnection proxy = (TopicConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
91                                                                           new Class[]{TopicConnection.class},
92                                                                           new ConnectionInvocationHandler(xatc, sameRMOverrideValue));
93          return proxy;
94      }
95  
96      public TopicConnection createTopicConnection(String username, String password) throws JMSException
97      {
98          XATopicConnection xatc = ((XATopicConnectionFactory) factory).createXATopicConnection(username,
99                                                                                                password);
100         TopicConnection proxy = (TopicConnection) Proxy.newProxyInstance(Connection.class.getClassLoader(),
101                                                                          new Class[]{TopicConnection.class},
102                                                                          new ConnectionInvocationHandler(xatc, sameRMOverrideValue));
103         return proxy;
104     }
105 
106 }