KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > ra > JMSConnectionProxy


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.ra;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51
52 import javax.jms.Connection JavaDoc;
53 import javax.jms.ConnectionConsumer JavaDoc;
54 import javax.jms.ConnectionMetaData JavaDoc;
55 import javax.jms.Destination JavaDoc;
56 import javax.jms.ExceptionListener JavaDoc;
57 import javax.jms.IllegalStateException JavaDoc;
58 import javax.jms.JMSException JavaDoc;
59 import javax.jms.Queue JavaDoc;
60 import javax.jms.QueueConnection JavaDoc;
61 import javax.jms.QueueSession JavaDoc;
62 import javax.jms.ServerSessionPool JavaDoc;
63 import javax.jms.Session JavaDoc;
64 import javax.jms.Topic JavaDoc;
65 import javax.jms.TopicConnection JavaDoc;
66 import javax.jms.TopicSession JavaDoc;
67
68 import org.apache.commons.logging.Log;
69 import org.apache.commons.logging.LogFactory;
70 import org.mr.api.jms.MantaQueueSession;
71 import org.mr.api.jms.MantaSession;
72 import org.mr.api.jms.MantaTopicSession;
73
74
75 /**
76  * Acts as a pass through proxy for a JMS Connection object.
77  * It intercepts events that are of interest of the ManagedConnectionImpl.
78  */

79 public class JMSConnectionProxy implements Connection JavaDoc, QueueConnection JavaDoc, TopicConnection JavaDoc, ExceptionListener JavaDoc {
80     
81     private static final Log log = LogFactory.getLog(JMSConnectionProxy.class);
82     
83     private ManagedConnectionImpl managedConnection;
84     private ArrayList JavaDoc sessions = new ArrayList JavaDoc();
85     private ExceptionListener JavaDoc exceptionListener;
86     
87     public JMSConnectionProxy(ManagedConnectionImpl managedConnection) {
88         this.managedConnection = managedConnection;
89     }
90     
91     /**
92      * Used to let the ActiveMQManagedConnection that this connection
93      * handel is not needed by the app.
94      *
95      * @throws JMSException
96      */

97     public void close() throws JMSException JavaDoc {
98         if( managedConnection!=null ) {
99             managedConnection.proxyClosedEvent(this);
100         }
101     }
102     
103     /**
104      * Called by the ActiveMQManagedConnection to invalidate this proxy.
105      */

106     public void cleanup() {
107         exceptionListener=null;
108         managedConnection = null;
109         for (Iterator JavaDoc iter = sessions.iterator(); iter.hasNext();) {
110             JMSSessionProxy p = (JMSSessionProxy) iter.next();
111             try {
112                 p.cleanup();
113             } catch (JMSException JavaDoc ignore) {
114             }
115             iter.remove();
116         }
117     }
118     
119     /**
120      *
121      */

122     private Connection JavaDoc getConnection() throws JMSException JavaDoc {
123         if (managedConnection == null) {
124             throw new IllegalStateException JavaDoc("The Connection is closed");
125         }
126         return managedConnection.getPhysicalConnection();
127     }
128     
129     /**
130      * @param transacted
131      * @param acknowledgeMode
132      * @return
133      * @throws JMSException
134      */

135     public Session JavaDoc createSession(boolean transacted, int acknowledgeMode)
136     throws JMSException JavaDoc {
137         return createSessionProxy(transacted, acknowledgeMode);
138     }
139     
140     /**
141      * @param acknowledgeMode
142      * @param transacted
143      * @return
144      * @throws JMSException
145      */

146     private JMSSessionProxy createSessionProxy(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
147         MantaSession session = (MantaSession) getConnection().createSession(transacted, acknowledgeMode);
148         //RATransactionContext txContext = new RATransactionContext(managedConnection.getTransactionContext(), session);
149
RATransactionContext txContext = new RATransactionContext(managedConnection.getTransactionContext());
150         session.setTransactionContext(txContext);
151         JMSSessionProxy p = new JMSSessionProxy(session);
152         p.setUseSharedTxContext(managedConnection.isInManagedTx());
153         sessions.add(p);
154         return p;
155     }
156     
157     private JMSSessionProxy createQueueSessionProxy(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
158         MantaQueueSession session = (MantaQueueSession)((QueueConnection JavaDoc)getConnection()).createQueueSession(transacted, acknowledgeMode);
159         //RATransactionContext txContext = new RATransactionContext(managedConnection.getTransactionContext(), session);
160
RATransactionContext txContext = new RATransactionContext(managedConnection.getTransactionContext());
161         session.setTransactionContext(txContext);
162         JMSSessionProxy p = new JMSSessionProxy(session);
163         p.setUseSharedTxContext(managedConnection.isInManagedTx());
164         sessions.add(p);
165         return p;
166     }
167     
168     private JMSSessionProxy createTopicSessionProxy(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
169         MantaTopicSession session = (MantaTopicSession)((TopicConnection JavaDoc)getConnection()).createTopicSession(transacted, acknowledgeMode);
170         //RATransactionContext txContext = new RATransactionContext(managedConnection.getTransactionContext(), session);
171
RATransactionContext txContext = new RATransactionContext(managedConnection.getTransactionContext());
172         session.setTransactionContext(txContext);
173         JMSSessionProxy p = new JMSSessionProxy(session);
174         p.setUseSharedTxContext(managedConnection.isInManagedTx());
175         sessions.add(p);
176         return p;
177     }
178     
179     
180     
181     public void setUseSharedTxContext(boolean enable) throws JMSException JavaDoc {
182          for (Iterator JavaDoc iter = sessions.iterator(); iter.hasNext();) {
183              JMSSessionProxy p = (JMSSessionProxy) iter.next();
184              p.setUseSharedTxContext(enable);
185          }
186      }
187     
188     /**
189      * @param transacted
190      * @param acknowledgeMode
191      * @return
192      * @throws JMSException
193      */

194     public QueueSession JavaDoc createQueueSession(boolean transacted,
195             int acknowledgeMode) throws JMSException JavaDoc {
196         //return new MantaQueueSession(createSessionProxy(transacted, acknowledgeMode));
197
return createQueueSessionProxy(transacted, acknowledgeMode);
198     }
199     
200     /**
201      * @param transacted
202      * @param acknowledgeMode
203      * @return
204      * @throws JMSException
205      */

206     public TopicSession JavaDoc createTopicSession(boolean transacted,
207             int acknowledgeMode) throws JMSException JavaDoc {
208         //return new MantaTopicSession(createSessionProxy(transacted, acknowledgeMode));
209
return createTopicSessionProxy(transacted, acknowledgeMode);
210     }
211     
212     /**
213      * @return
214      * @throws JMSException
215      */

216     public String JavaDoc getClientID() throws JMSException JavaDoc {
217         return getConnection().getClientID();
218     }
219     
220     /**
221      * @return
222      * @throws JMSException
223      */

224     public ExceptionListener JavaDoc getExceptionListener() throws JMSException JavaDoc {
225         return getConnection().getExceptionListener();
226     }
227     
228     /**
229      * @return
230      * @throws JMSException
231      */

232     public ConnectionMetaData JavaDoc getMetaData() throws JMSException JavaDoc {
233         return getConnection().getMetaData();
234     }
235     
236     /**
237      * @param clientID
238      * @throws JMSException
239      */

240     public void setClientID(String JavaDoc clientID) throws JMSException JavaDoc {
241         getConnection().setClientID(clientID);
242     }
243     
244     /**
245      * @param listener
246      * @throws JMSException
247      */

248     public void setExceptionListener(ExceptionListener JavaDoc listener)
249     throws JMSException JavaDoc {
250         getConnection();
251         exceptionListener = listener;
252     }
253     
254     /**
255      * @throws JMSException
256      */

257     public void start() throws JMSException JavaDoc {
258         getConnection().start();
259     }
260     
261     /**
262      * @throws JMSException
263      */

264     public void stop() throws JMSException JavaDoc {
265         getConnection().stop();
266     }
267     
268     
269     /**
270      * @param queue
271      * @param messageSelector
272      * @param sessionPool
273      * @param maxMessages
274      * @return
275      * @throws JMSException
276      */

277     public ConnectionConsumer JavaDoc createConnectionConsumer(Queue JavaDoc queue,
278             String JavaDoc messageSelector,
279             ServerSessionPool JavaDoc sessionPool,
280             int maxMessages)
281     throws JMSException JavaDoc {
282         throw new JMSException JavaDoc("Not Supported.");
283     }
284     
285     /**
286      * @param topic
287      * @param messageSelector
288      * @param sessionPool
289      * @param maxMessages
290      * @return
291      * @throws JMSException
292      */

293     public ConnectionConsumer JavaDoc createConnectionConsumer(Topic JavaDoc topic,
294             String JavaDoc messageSelector,
295             ServerSessionPool JavaDoc sessionPool,
296             int maxMessages)
297     throws JMSException JavaDoc {
298         throw new JMSException JavaDoc("Not Supported.");
299     }
300     
301     /**
302      * @param destination
303      * @param messageSelector
304      * @param sessionPool
305      * @param maxMessages
306      * @return
307      * @throws JMSException
308      */

309     public ConnectionConsumer JavaDoc createConnectionConsumer(Destination JavaDoc destination,
310             String JavaDoc messageSelector,
311             ServerSessionPool JavaDoc sessionPool,
312             int maxMessages)
313     throws JMSException JavaDoc {
314         throw new JMSException JavaDoc("Not Supported.");
315     }
316     
317     /**
318      * @param topic
319      * @param subscriptionName
320      * @param messageSelector
321      * @param sessionPool
322      * @param maxMessages
323      * @return
324      * @throws JMSException
325      */

326     public ConnectionConsumer JavaDoc createDurableConnectionConsumer(Topic JavaDoc topic,
327             String JavaDoc subscriptionName,
328             String JavaDoc messageSelector,
329             ServerSessionPool JavaDoc sessionPool,
330             int maxMessages)
331     throws JMSException JavaDoc {
332         throw new JMSException JavaDoc("Not Supported.");
333     }
334     
335     /**
336      * @return Returns the managedConnection.
337      */

338     public ManagedConnectionImpl getManagedConnection() {
339         return managedConnection;
340     }
341     
342     public void onException(JMSException JavaDoc e) {
343         if(exceptionListener!=null && managedConnection!=null) {
344             try {
345                 exceptionListener.onException(e);
346             } catch (Throwable JavaDoc ignore) {
347                 // We can never trust user code so ignore any exceptions.
348
}
349         }
350     }
351     
352 }
353
Popular Tags