KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > oracle > jms > OracleJmsConnection


1 /*
2  * $Id: OracleJmsConnection.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import javax.jms.Connection JavaDoc;
18 import javax.jms.ConnectionConsumer JavaDoc;
19 import javax.jms.ConnectionMetaData JavaDoc;
20 import javax.jms.ExceptionListener JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Queue JavaDoc;
23 import javax.jms.QueueConnection JavaDoc;
24 import javax.jms.QueueSession JavaDoc;
25 import javax.jms.ServerSessionPool JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnection JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29
30 import oracle.jms.AQjmsConnection;
31 import oracle.jms.AQjmsQueueConnectionFactory;
32 import oracle.jms.AQjmsTopicConnectionFactory;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 public class OracleJmsConnection implements TopicConnection JavaDoc, QueueConnection JavaDoc
38 {
39
40     private TopicConnection JavaDoc topicConnection;
41     private QueueConnection JavaDoc queueConnection;
42
43     /**
44      * Instead of a single JMS Connection, the Oracle JMS Connector maintains a list
45      * of open connections.
46      *
47      * @see OracleJmsConnector.multipleSessionsPerConnection
48      */

49     private List JavaDoc connections;
50
51     /**
52      * Holds a reference to the Oracle JMS Connector.
53      */

54     private OracleJmsConnector connector;
55
56     public OracleJmsConnection(OracleJmsConnector connector)
57     {
58         this.connector = connector;
59         connections = new ArrayList JavaDoc();
60     }
61
62     /** Iterate through the open connections and start them one by one. */
63     public void start() throws JMSException JavaDoc
64     {
65         Connection JavaDoc jmsConnection = null;
66
67         for (Iterator JavaDoc i = connections.iterator(); i.hasNext();)
68         {
69             jmsConnection = (Connection JavaDoc)i.next();
70             if (jmsConnection != null)
71             {
72                 jmsConnection.start();
73             }
74         }
75     }
76
77     /** Iterate through the open connections and stop them one by one. */
78     public void stop() throws JMSException JavaDoc
79     {
80         Connection JavaDoc jmsConnection = null;
81
82         for (Iterator JavaDoc i = connections.iterator(); i.hasNext();)
83         {
84             jmsConnection = (Connection JavaDoc)i.next();
85             if (jmsConnection != null)
86             {
87                 jmsConnection.stop();
88             }
89         }
90     }
91
92     /** Iterate through the open connections and close them one by one. */
93     public void close() throws JMSException JavaDoc
94     {
95         Connection JavaDoc jmsConnection;
96
97         // Iterate through the open connections
98
for (Iterator JavaDoc i = connections.iterator(); i.hasNext();)
99         {
100             jmsConnection = (Connection JavaDoc)i.next();
101             if (jmsConnection != null)
102             {
103                 try
104                 {
105                     // Close the JMS Session (and its underlying JDBC connection).
106
connector.close(((AQjmsConnection)jmsConnection).getCurrentJmsSession());
107                     // Close the JMS Connection.
108
jmsConnection.close();
109                 }
110                 catch (JMSException JavaDoc e)
111                 {
112                     logger.error("Unable to close Oracle JMS connection: " + e.getMessage());
113                 }
114             }
115         }
116     }
117
118     protected QueueConnection JavaDoc getQueueConnection() throws JMSException JavaDoc
119     {
120         QueueConnection JavaDoc connection;
121
122         if (connector.isMultipleSessionsPerConnection())
123         {
124             if (queueConnection == null)
125             {
126                 queueConnection = AQjmsQueueConnectionFactory.createQueueConnection(connector.getJdbcConnection());
127                 queueConnection.start();
128             }
129             connection = queueConnection;
130         }
131         else
132         {
133             connection = AQjmsQueueConnectionFactory.createQueueConnection(connector.getJdbcConnection());
134             connection.start();
135             // Add the connection to the list of open JMS connections.
136
connections.add(connection);
137         }
138         return connection;
139     }
140
141     protected TopicConnection JavaDoc getTopicConnection() throws JMSException JavaDoc
142     {
143         TopicConnection JavaDoc connection;
144
145         if (connector.isMultipleSessionsPerConnection())
146         {
147             if (topicConnection == null)
148             {
149                 topicConnection = AQjmsTopicConnectionFactory.createTopicConnection(connector.getJdbcConnection());
150                 topicConnection.start();
151             }
152             connection = topicConnection;
153         }
154         else
155         {
156             connection = AQjmsTopicConnectionFactory.createTopicConnection(connector.getJdbcConnection());
157             connection.start();
158             // Add the connection to the list of open JMS connections.
159
connections.add(connection);
160         }
161         connection.start();
162         return connection;
163     }
164
165     public QueueSession JavaDoc createQueueSession(boolean transacted, int ackMode) throws JMSException JavaDoc
166     {
167         return getQueueConnection().createQueueSession(transacted, ackMode);
168     }
169
170     public TopicSession JavaDoc createTopicSession(boolean transacted, int ackMode) throws JMSException JavaDoc
171     {
172         return getTopicConnection().createTopicSession(transacted, ackMode);
173     }
174
175     public ConnectionConsumer JavaDoc createConnectionConsumer(Topic JavaDoc arg0,
176                                                        String JavaDoc arg1,
177                                                        ServerSessionPool JavaDoc arg2,
178                                                        int arg3) throws JMSException JavaDoc
179     {
180         return getTopicConnection().createConnectionConsumer(arg0, arg1, arg2, arg3);
181     }
182
183     public ConnectionConsumer JavaDoc createDurableConnectionConsumer(Topic JavaDoc arg0,
184                                                               String JavaDoc arg1,
185                                                               String JavaDoc arg2,
186                                                               ServerSessionPool JavaDoc arg3,
187                                                               int arg4) throws JMSException JavaDoc
188     {
189         return getTopicConnection().createDurableConnectionConsumer(arg0, arg1, arg2, arg3, arg4);
190     }
191
192     public ConnectionConsumer JavaDoc createConnectionConsumer(Queue JavaDoc arg0,
193                                                        String JavaDoc arg1,
194                                                        ServerSessionPool JavaDoc arg2,
195                                                        int arg3) throws JMSException JavaDoc
196     {
197         return getQueueConnection().createConnectionConsumer(arg0, arg1, arg2, arg3);
198     }
199
200     // TODO How do we know which connection to use?
201
public String JavaDoc getClientID() throws JMSException JavaDoc
202     {
203         return null;
204     }
205
206     // TODO How do we know which connection to use?
207
public ExceptionListener JavaDoc getExceptionListener() throws JMSException JavaDoc
208     {
209         return null;
210     }
211
212     // TODO How do we know which connection to use?
213
public ConnectionMetaData JavaDoc getMetaData() throws JMSException JavaDoc
214     {
215         return null;
216     }
217
218     public void setClientID(String JavaDoc arg0) throws JMSException JavaDoc
219     {
220         // TODO How do we know which connection to use?
221
}
222
223     public void setExceptionListener(ExceptionListener JavaDoc arg0) throws JMSException JavaDoc
224     {
225         // TODO How do we know which connection to use?
226
}
227
228     private static Log logger = LogFactory.getLog(OracleJmsConnection.class);
229 }
230
Popular Tags