KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ManagedConnectionProxy


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.ConnectionConsumer JavaDoc;
25 import javax.jms.ConnectionMetaData JavaDoc;
26 import javax.jms.Destination JavaDoc;
27 import javax.jms.ExceptionListener JavaDoc;
28 import javax.jms.IllegalStateException JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueSession JavaDoc;
33 import javax.jms.ServerSessionPool JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.jms.TopicConnection JavaDoc;
37 import javax.jms.TopicSession JavaDoc;
38
39 import org.apache.activemq.ActiveMQQueueSession;
40 import org.apache.activemq.ActiveMQSession;
41 import org.apache.activemq.ActiveMQTopicSession;
42
43
44 /**
45  * Acts as a pass through proxy for a JMS Connection object.
46  * It intercepts events that are of interest of the ActiveMQManagedConnection.
47  *
48  * @version $Revision$
49  */

50 public class ManagedConnectionProxy implements Connection JavaDoc, QueueConnection JavaDoc, TopicConnection JavaDoc, ExceptionListener JavaDoc {
51
52     private ActiveMQManagedConnection managedConnection;
53     private ArrayList JavaDoc sessions = new ArrayList JavaDoc();
54     private ExceptionListener JavaDoc exceptionListener;
55
56     public ManagedConnectionProxy(ActiveMQManagedConnection managedConnection) {
57         this.managedConnection = managedConnection;
58     }
59
60     /**
61      * Used to let the ActiveMQManagedConnection that this connection
62      * handel is not needed by the app.
63      *
64      * @throws JMSException
65      */

66     public void close() throws JMSException JavaDoc {
67         if( managedConnection!=null ) {
68             managedConnection.proxyClosedEvent(this);
69         }
70     }
71
72     /**
73      * Called by the ActiveMQManagedConnection to invalidate this proxy.
74      */

75     public void cleanup() {
76         exceptionListener=null;
77         managedConnection = null;
78         for (Iterator JavaDoc iter = sessions.iterator(); iter.hasNext();) {
79             ManagedSessionProxy p = (ManagedSessionProxy) iter.next();
80             try {
81                 p.cleanup();
82             } catch (JMSException JavaDoc ignore) {
83             }
84             iter.remove();
85         }
86     }
87
88     /**
89      *
90      */

91     private Connection JavaDoc getConnection() throws JMSException JavaDoc {
92         if (managedConnection == null) {
93             throw new IllegalStateException JavaDoc("The Connection is closed");
94         }
95         return managedConnection.getPhysicalConnection();
96     }
97
98     /**
99      * @param transacted
100      * @param acknowledgeMode
101      * @return
102      * @throws JMSException
103      */

104     public Session JavaDoc createSession(boolean transacted, int acknowledgeMode)
105             throws JMSException JavaDoc {
106         return createSessionProxy(transacted, acknowledgeMode);
107     }
108
109     /**
110      * @param acknowledgeMode
111      * @param transacted
112      * @return
113      * @throws JMSException
114      */

115     private ManagedSessionProxy createSessionProxy(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
116         ActiveMQSession session = (ActiveMQSession) getConnection().createSession(transacted, acknowledgeMode);
117         ManagedTransactionContext txContext = new ManagedTransactionContext(managedConnection.getTransactionContext());
118         session.setTransactionContext(txContext);
119         ManagedSessionProxy p = new ManagedSessionProxy(session);
120         p.setUseSharedTxContext(managedConnection.isInManagedTx());
121         sessions.add(p);
122         return p;
123     }
124
125     public void setUseSharedTxContext(boolean enable) throws JMSException JavaDoc {
126         for (Iterator JavaDoc iter = sessions.iterator(); iter.hasNext();) {
127             ManagedSessionProxy p = (ManagedSessionProxy) iter.next();
128             p.setUseSharedTxContext(enable);
129         }
130     }
131
132     /**
133      * @param transacted
134      * @param acknowledgeMode
135      * @return
136      * @throws JMSException
137      */

138     public QueueSession JavaDoc createQueueSession(boolean transacted,
139                                            int acknowledgeMode) throws JMSException JavaDoc {
140         return new ActiveMQQueueSession(createSessionProxy(transacted, acknowledgeMode));
141     }
142
143     /**
144      * @param transacted
145      * @param acknowledgeMode
146      * @return
147      * @throws JMSException
148      */

149     public TopicSession JavaDoc createTopicSession(boolean transacted,
150                                            int acknowledgeMode) throws JMSException JavaDoc {
151         return new ActiveMQTopicSession(createSessionProxy(transacted, acknowledgeMode));
152     }
153
154     /**
155      * @return
156      * @throws JMSException
157      */

158     public String JavaDoc getClientID() throws JMSException JavaDoc {
159         return getConnection().getClientID();
160     }
161
162     /**
163      * @return
164      * @throws JMSException
165      */

166     public ExceptionListener JavaDoc getExceptionListener() throws JMSException JavaDoc {
167         return getConnection().getExceptionListener();
168     }
169
170     /**
171      * @return
172      * @throws JMSException
173      */

174     public ConnectionMetaData JavaDoc getMetaData() throws JMSException JavaDoc {
175         return getConnection().getMetaData();
176     }
177
178     /**
179      * @param clientID
180      * @throws JMSException
181      */

182     public void setClientID(String JavaDoc clientID) throws JMSException JavaDoc {
183         getConnection().setClientID(clientID);
184     }
185
186     /**
187      * @param listener
188      * @throws JMSException
189      */

190     public void setExceptionListener(ExceptionListener JavaDoc listener)
191             throws JMSException JavaDoc {
192         getConnection();
193         exceptionListener = listener;
194     }
195
196     /**
197      * @throws JMSException
198      */

199     public void start() throws JMSException JavaDoc {
200         getConnection().start();
201     }
202
203     /**
204      * @throws JMSException
205      */

206     public void stop() throws JMSException JavaDoc {
207         getConnection().stop();
208     }
209
210
211     /**
212      * @param queue
213      * @param messageSelector
214      * @param sessionPool
215      * @param maxMessages
216      * @return
217      * @throws JMSException
218      */

219     public ConnectionConsumer JavaDoc createConnectionConsumer(Queue JavaDoc queue,
220                                                        String JavaDoc messageSelector, ServerSessionPool JavaDoc sessionPool,
221                                                        int maxMessages) throws JMSException JavaDoc {
222         throw new JMSException JavaDoc("Not Supported.");
223     }
224
225     /**
226      * @param topic
227      * @param messageSelector
228      * @param sessionPool
229      * @param maxMessages
230      * @return
231      * @throws JMSException
232      */

233     public ConnectionConsumer JavaDoc createConnectionConsumer(Topic JavaDoc topic,
234                                                        String JavaDoc messageSelector, ServerSessionPool JavaDoc sessionPool,
235                                                        int maxMessages) throws JMSException JavaDoc {
236         throw new JMSException JavaDoc("Not Supported.");
237     }
238
239     /**
240      * @param destination
241      * @param messageSelector
242      * @param sessionPool
243      * @param maxMessages
244      * @return
245      * @throws JMSException
246      */

247     public ConnectionConsumer JavaDoc createConnectionConsumer(Destination JavaDoc destination,
248                                                        String JavaDoc messageSelector, ServerSessionPool JavaDoc sessionPool,
249                                                        int maxMessages) throws JMSException JavaDoc {
250         throw new JMSException JavaDoc("Not Supported.");
251     }
252
253     /**
254      * @param topic
255      * @param subscriptionName
256      * @param messageSelector
257      * @param sessionPool
258      * @param maxMessages
259      * @return
260      * @throws JMSException
261      */

262     public ConnectionConsumer JavaDoc createDurableConnectionConsumer(Topic JavaDoc topic,
263                                                               String JavaDoc subscriptionName, String JavaDoc messageSelector,
264                                                               ServerSessionPool JavaDoc sessionPool, int maxMessages) throws JMSException JavaDoc {
265         throw new JMSException JavaDoc("Not Supported.");
266     }
267
268     /**
269      * @return Returns the managedConnection.
270      */

271     public ActiveMQManagedConnection getManagedConnection() {
272         return managedConnection;
273     }
274
275     public void onException(JMSException JavaDoc e) {
276         if(exceptionListener!=null && managedConnection!=null) {
277             try {
278                 exceptionListener.onException(e);
279             } catch (Throwable JavaDoc ignore) {
280                 // We can never trust user code so ignore any exceptions.
281
}
282         }
283     }
284
285 }
286
Popular Tags