View Javadoc

1   /*
2    * $Id: OracleJmsConnection.java 7976 2007-08-21 14:26:13Z 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  
11  package org.mule.providers.oracle.jms;
12  
13  import java.util.ArrayList;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import javax.jms.Connection;
18  import javax.jms.ConnectionConsumer;
19  import javax.jms.ConnectionMetaData;
20  import javax.jms.ExceptionListener;
21  import javax.jms.JMSException;
22  import javax.jms.Queue;
23  import javax.jms.QueueConnection;
24  import javax.jms.QueueSession;
25  import javax.jms.ServerSessionPool;
26  import javax.jms.Topic;
27  import javax.jms.TopicConnection;
28  import javax.jms.TopicSession;
29  
30  import oracle.jms.AQjmsConnection;
31  import oracle.jms.AQjmsQueueConnectionFactory;
32  import oracle.jms.AQjmsTopicConnectionFactory;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  public class OracleJmsConnection implements TopicConnection, QueueConnection
37  {
38  
39      private TopicConnection topicConnection;
40      private QueueConnection queueConnection;
41  
42      /**
43       * Instead of a single JMS Connection, the Oracle JMS Connector maintains a list
44       * of open connections.
45       * 
46       * @see OracleJmsConnector#multipleSessionsPerConnection
47       */
48      private List connections;
49  
50      /**
51       * Holds a reference to the Oracle JMS Connector.
52       */
53      private OracleJmsConnector connector;
54  
55      public OracleJmsConnection(OracleJmsConnector connector)
56      {
57          this.connector = connector;
58          connections = new ArrayList();
59      }
60  
61      /** Iterate through the open connections and start them one by one. */
62      public void start() throws JMSException
63      {
64          Connection jmsConnection = null;
65  
66          for (Iterator i = connections.iterator(); i.hasNext();)
67          {
68              jmsConnection = (Connection)i.next();
69              if (jmsConnection != null)
70              {
71                  jmsConnection.start();
72              }
73          }
74      }
75  
76      /** Iterate through the open connections and stop them one by one. */
77      public void stop() throws JMSException
78      {
79          Connection jmsConnection = null;
80  
81          for (Iterator i = connections.iterator(); i.hasNext();)
82          {
83              jmsConnection = (Connection)i.next();
84              if (jmsConnection != null)
85              {
86                  jmsConnection.stop();
87              }
88          }
89      }
90  
91      /** Iterate through the open connections and close them one by one. */
92      public void close() throws JMSException
93      {
94          Connection jmsConnection;
95  
96          // Iterate through the open connections
97          for (Iterator i = connections.iterator(); i.hasNext();)
98          {
99              jmsConnection = (Connection)i.next();
100             if (jmsConnection != null)
101             {
102                 try
103                 {
104                     // Close the JMS Session (and its underlying JDBC connection).
105                     connector.close(((AQjmsConnection)jmsConnection).getCurrentJmsSession());
106                     // Close the JMS Connection.
107                     jmsConnection.close();
108                 }
109                 catch (JMSException e)
110                 {
111                     logger.error("Unable to close Oracle JMS connection: " + e.getMessage());
112                 }
113             }
114         }
115     }
116 
117     protected QueueConnection getQueueConnection() throws JMSException
118     {
119         QueueConnection connection;
120 
121         if (connector.isMultipleSessionsPerConnection())
122         {
123             if (queueConnection == null)
124             {
125                 queueConnection = AQjmsQueueConnectionFactory.createQueueConnection(connector.getJdbcConnection());
126                 queueConnection.start();
127             }
128             connection = queueConnection;
129         }
130         else
131         {
132             connection = AQjmsQueueConnectionFactory.createQueueConnection(connector.getJdbcConnection());
133             connection.start();
134             // Add the connection to the list of open JMS connections.
135             connections.add(connection);
136         }
137         return connection;
138     }
139 
140     protected TopicConnection getTopicConnection() throws JMSException
141     {
142         TopicConnection connection;
143 
144         if (connector.isMultipleSessionsPerConnection())
145         {
146             if (topicConnection == null)
147             {
148                 topicConnection = AQjmsTopicConnectionFactory.createTopicConnection(connector.getJdbcConnection());
149                 topicConnection.start();
150             }
151             connection = topicConnection;
152         }
153         else
154         {
155             connection = AQjmsTopicConnectionFactory.createTopicConnection(connector.getJdbcConnection());
156             connection.start();
157             // Add the connection to the list of open JMS connections.
158             connections.add(connection);
159         }
160         connection.start();
161         return connection;
162     }
163 
164     public QueueSession createQueueSession(boolean transacted, int ackMode) throws JMSException
165     {
166         return getQueueConnection().createQueueSession(transacted, ackMode);
167     }
168 
169     public TopicSession createTopicSession(boolean transacted, int ackMode) throws JMSException
170     {
171         return getTopicConnection().createTopicSession(transacted, ackMode);
172     }
173 
174     public ConnectionConsumer createConnectionConsumer(Topic arg0,
175                                                        String arg1,
176                                                        ServerSessionPool arg2,
177                                                        int arg3) throws JMSException
178     {
179         return getTopicConnection().createConnectionConsumer(arg0, arg1, arg2, arg3);
180     }
181 
182     public ConnectionConsumer createDurableConnectionConsumer(Topic arg0,
183                                                               String arg1,
184                                                               String arg2,
185                                                               ServerSessionPool arg3,
186                                                               int arg4) throws JMSException
187     {
188         return getTopicConnection().createDurableConnectionConsumer(arg0, arg1, arg2, arg3, arg4);
189     }
190 
191     public ConnectionConsumer createConnectionConsumer(Queue arg0,
192                                                        String arg1,
193                                                        ServerSessionPool arg2,
194                                                        int arg3) throws JMSException
195     {
196         return getQueueConnection().createConnectionConsumer(arg0, arg1, arg2, arg3);
197     }
198 
199     // TODO How do we know which connection to use?
200     public String getClientID() throws JMSException
201     {
202         return null;
203     }
204 
205     // TODO How do we know which connection to use?
206     public ExceptionListener getExceptionListener() throws JMSException
207     {
208         return null;
209     }
210 
211     // TODO How do we know which connection to use?
212     public ConnectionMetaData getMetaData() throws JMSException
213     {
214         return null;
215     }
216 
217     public void setClientID(String arg0) throws JMSException
218     {
219         // TODO How do we know which connection to use?
220     }
221 
222     public void setExceptionListener(ExceptionListener arg0) throws JMSException
223     {
224         // TODO How do we know which connection to use?
225     }
226 
227     private static Log logger = LogFactory.getLog(OracleJmsConnection.class);
228 }