View Javadoc

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