KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > session > ConnectionImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.session;
30
31 import com.caucho.jms.ConnectionFactoryImpl;
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.jms.*;
36 import javax.jms.IllegalStateException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * A connection.
44  */

45 public class ConnectionImpl implements Connection {
46   static final Logger JavaDoc log = Log.open(ConnectionImpl.class);
47   static final L10N L = new L10N(ConnectionImpl.class);
48
49   private static int _clientIdGenerator;
50
51   private ConnectionFactoryImpl _factory;
52   
53   private String JavaDoc _clientId;
54   private boolean _isClientIdSet;
55   
56   private ExceptionListener _exceptionListener;
57
58   private ArrayList JavaDoc<SessionImpl> _sessions = new ArrayList JavaDoc<SessionImpl>();
59
60   private HashMap JavaDoc<String JavaDoc,TopicSubscriber> _durableSubscriberMap =
61     new HashMap JavaDoc<String JavaDoc,TopicSubscriber>();
62
63   private volatile boolean _isActive;
64   private volatile boolean _isStopping;
65   protected volatile boolean _isClosed;
66
67   public ConnectionImpl(ConnectionFactoryImpl factory)
68   {
69     _factory = factory;
70   }
71
72   /**
73    * Returns the connection's client identifier.
74    */

75   public String JavaDoc getClientID()
76     throws JMSException
77   {
78     if (_isClosed)
79       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
80     
81     return _clientId;
82   }
83
84   /**
85    * Sets the connections client identifier.
86    *
87    * @param the new client identifier.
88    */

89   public void setClientID(String JavaDoc clientId)
90     throws JMSException
91   {
92     if (_isClosed)
93       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
94     
95     if (_isClientIdSet)
96       throw new IllegalStateException JavaDoc(L.l("Can't set client id '{0}' after the connection has been used.",
97                       clientId));
98
99     ConnectionImpl oldConn = _factory.findByClientID(clientId);
100
101     if (oldConn != null)
102       throw new InvalidClientIDException(L.l("'{0}' is a duplicate client id.",
103                          clientId));
104     
105     _clientId = clientId;
106     _isClientIdSet = true;
107   }
108
109   /**
110    * Returns the connection factory.
111    */

112   public ConnectionFactoryImpl getConnectionFactory()
113   {
114     return _factory;
115   }
116
117   /**
118    * Returns the connection's exception listener.
119    */

120   public ExceptionListener getExceptionListener()
121     throws JMSException
122   {
123     if (_isClosed)
124       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
125     
126     return _exceptionListener;
127   }
128
129   /**
130    * Returns the connection's exception listener.
131    */

132   public void setExceptionListener(ExceptionListener listener)
133     throws JMSException
134   {
135     if (_isClosed)
136       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
137
138     assignClientID();
139     
140     _exceptionListener = listener;
141   }
142
143   /**
144    * Returns the connection's metadata.
145    */

146   public ConnectionMetaData getMetaData()
147     throws JMSException
148   {
149     if (_isClosed)
150       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
151     
152     return new ConnectionMetaDataImpl();
153   }
154
155   /**
156    * Start (or restart) a connection.
157    */

158   public void start()
159     throws JMSException
160   {
161     if (_isClosed)
162       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
163     
164     assignClientID();
165
166     if (_isActive || _isStopping)
167       return;
168
169     _isActive = true;
170
171     for (int i = 0; i < _sessions.size(); i++) {
172       _sessions.get(i).start();
173     }
174   }
175
176   /**
177    * Stops the connection temporarily.
178    */

179   public void stop()
180     throws JMSException
181   {
182     if (_isClosed)
183       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
184     
185     if (_isStopping || ! _isActive)
186       return;
187     
188     assignClientID();
189
190     _isStopping = true;
191
192     try {
193       for (int i = 0; i < _sessions.size(); i++) {
194     _sessions.get(i).stop();
195       }
196     } finally {
197       _isActive = false;
198       _isStopping = false;
199     }
200   }
201
202   /**
203    * Returns true if the connection is started.
204    */

205   boolean isActive()
206   {
207     return _isActive;
208   }
209
210   /**
211    * Returns true if the connection is stopping.
212    */

213   boolean isStopping()
214   {
215     return _isStopping;
216   }
217
218   /**
219    * Creates a new connection session.
220    */

221   public Session createSession(boolean transacted, int acknowledgeMode)
222     throws JMSException
223   {
224     checkOpen();
225     
226     assignClientID();
227     
228     return new SessionImpl(this, transacted, acknowledgeMode);
229   }
230
231   /**
232    * Adds a session.
233    */

234   protected void addSession(SessionImpl session)
235   {
236     _sessions.add(session);
237     
238     if (_isActive)
239       session.start();
240   }
241
242   /**
243    * Removes a session.
244    */

245   void removeSession(SessionImpl session)
246   {
247     _sessions.remove(session);
248   }
249
250   /**
251    * Gets a durable subscriber.
252    */

253   TopicSubscriber getDurableSubscriber(String JavaDoc name)
254   {
255     return _durableSubscriberMap.get(name);
256   }
257
258   /**
259    * Adds a durable subscriber.
260    */

261   TopicSubscriber putDurableSubscriber(String JavaDoc name, TopicSubscriber subscriber)
262   {
263     return _durableSubscriberMap.put(name, subscriber);
264   }
265
266   /**
267    * Removes a durable subscriber.
268    */

269   TopicSubscriber removeDurableSubscriber(String JavaDoc name)
270   {
271     return _durableSubscriberMap.remove(name);
272   }
273
274   /**
275    * Creates a new consumer (optional)
276    */

277   public ConnectionConsumer
278     createConnectionConsumer(Destination destination,
279                  String JavaDoc messageSelector,
280                  ServerSessionPool sessionPool,
281                  int maxMessages)
282     throws JMSException
283   {
284     throw new UnsupportedOperationException JavaDoc();
285   }
286
287   /**
288    * Creates a new consumer (optional)
289    */

290   public ConnectionConsumer
291     createDurableConnectionConsumer(Topic topic, String JavaDoc name,
292                     String JavaDoc messageSelector,
293                     ServerSessionPool sessionPool,
294                     int maxMessages)
295     throws JMSException
296   {
297     if (_isClosed)
298       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
299     
300     throw new UnsupportedOperationException JavaDoc();
301   }
302
303   /**
304    * Closes the connection.
305    */

306   public void close()
307     throws JMSException
308   {
309     if (_isClosed)
310       return;
311     
312     stop();
313     
314     _isClosed = true;
315
316     _factory.removeConnection(this);
317
318     ArrayList JavaDoc<SessionImpl> sessions = new ArrayList JavaDoc<SessionImpl>(_sessions);
319     _sessions.clear();
320     
321     for (int i = 0; i < sessions.size(); i++) {
322       try {
323     sessions.get(i).close();
324       } catch (Throwable JavaDoc e) {
325     log.log(Level.WARNING, e.toString(), e);
326       }
327     }
328   }
329
330   /**
331    * Checks that the session is open.
332    */

333   protected void checkOpen()
334     throws IllegalStateException JavaDoc
335   {
336     if (_isClosed)
337       throw new IllegalStateException JavaDoc(L.l("connection is closed"));
338   }
339
340   /**
341    * Assigns a random client id.
342    *
343    * XXX: possibly wrong, i.e. shouldn't assign, for durable subscriptions
344    */

345   protected void assignClientID()
346   {
347     if (_clientId == null)
348       _clientId = "resin-temp-" + _clientIdGenerator++;
349     _isClientIdSet = true;
350   }
351
352   /**
353    * automatically close if GC.
354    */

355   public void finalize()
356   {
357     // possible deadlock with the close, since it triggers
358
// a rollback i.e. it's trying to do too much
359
/*
360     try {
361       close();
362     } catch (Throwable e) {
363     }
364     */

365   }
366 }
367
Popular Tags