KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > server > JmsServerConnection


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JmsServerConnection.java,v 1.3 2005/05/24 13:09:09 tanderson Exp $
44  */

45 package org.exolab.jms.server;
46
47 import java.util.HashSet JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import javax.jms.InvalidClientIDException JavaDoc;
50 import javax.jms.JMSException JavaDoc;
51
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 /**
56  * Server implementation of the <code>javax.jms.Connection</code> interface
57  *
58  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
59  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
60  * @version $Revision: 1.3 $ $Date: 2005/05/24 13:09:09 $
61  * @see JmsServerConnectionManager
62  */

63 public class JmsServerConnection implements ServerConnection {
64
65     /**
66      * The connection manager responsible for this.
67      */

68     private final JmsServerConnectionManager _manager;
69
70     /**
71      * The connection identifier.
72      */

73     private final long _connectionId;
74
75     /**
76      * The client identifier. May be <code>null</code>.
77      */

78     private String JavaDoc _clientId;
79
80     /**
81      * The sessions associated with the connection.
82      */

83     private HashSet JavaDoc _sessions = new HashSet JavaDoc();
84
85     /**
86      * Indicates if message delivery has been stopped for this connection.
87      */

88     private boolean _stopped = true;
89
90     /**
91      * The logger.
92      */

93     private static final Log _log =
94             LogFactory.getLog(JmsServerConnection.class);
95
96     /**
97      * Construct a new <code>JmsServerConnection</code>.
98      *
99      * @param manager the connection manager
100      * @param connectionId the identifier for this connection
101      * @param clientId the client identifier. May be <code>null</code>
102      */

103     protected JmsServerConnection(JmsServerConnectionManager manager,
104                                   long connectionId, String JavaDoc clientId) {
105         _manager = manager;
106         _connectionId = connectionId;
107         _clientId = clientId;
108     }
109
110     /**
111      * Returns the connection identifier.
112      *
113      * @return the connection identifier
114      */

115     public long getConnectionId() {
116         return _connectionId;
117     }
118
119     /**
120      * Returns the client identifier.
121      *
122      * @return the client identifier
123      */

124     public String JavaDoc getClientID() {
125         return _clientId;
126     }
127
128     /**
129      * Sets the client identifier for this connection.
130      *
131      * @param clientID the unique client identifier
132      * @throws JMSException if the JMS provider fails to set the
133      * client ID for this connection due to
134      * some internal error.
135      * @throws InvalidClientIDException if the JMS client specifies an invalid
136      * or duplicate client ID.
137      * @throws IllegalStateException if the JMS client attempts to set a
138      * connection's client ID at the wrong time
139      * or when it has been administratively
140      * configured.
141      */

142     public void setClientID(String JavaDoc clientID) throws JMSException JavaDoc {
143         if (clientID == null) {
144             throw new InvalidClientIDException JavaDoc("Invalid clientID: " + clientID);
145         }
146         _manager.addClientID(clientID);
147         _clientId = clientID;
148     }
149
150     /**
151      * Create a new session
152      *
153      * @param transacted indicates whether the session is transacted
154      * @param acknowledgeMode indicates whether the consumer or the client will
155      * acknowledge any messages it receives; ignored if
156      * the session is transacted. Legal values are
157      * <code>Session.AUTO_ACKNOWLEDGE</code>,
158      * <code>Session.CLIENT_ACKNOWLEDGE</code>, and
159      * <code>Session.DUPS_OK_ACKNOWLEDGE</code>.
160      * @return a newly created session
161      * @throws JMSException for any JMS error
162      */

163     public synchronized ServerSession createSession(int acknowledgeMode,
164                                                     boolean transacted)
165             throws JMSException JavaDoc {
166         JmsServerSession session = new JmsServerSession(this, acknowledgeMode,
167                                                         transacted);
168         _sessions.add(session);
169         if (!_stopped) {
170             session.start();
171         }
172
173         return session;
174     }
175
176     /**
177      * Starts (or restarts) a connection's delivery of incoming messages. A call
178      * to <code>start</code> on a connection that has already been started is
179      * ignored.
180      *
181      * @todo - unused?
182      */

183     public synchronized void start() {
184         if (_stopped) {
185             Iterator JavaDoc iterator = _sessions.iterator();
186             while (iterator.hasNext()) {
187                 JmsServerSession session = (JmsServerSession) iterator.next();
188                 session.start();
189             }
190             _stopped = false;
191         }
192     }
193
194     /**
195      * Closes the connection.
196      */

197     public synchronized void close() {
198         Iterator JavaDoc iterator = _sessions.iterator();
199         while (iterator.hasNext()) {
200             JmsServerSession session = (JmsServerSession) iterator.next();
201             try {
202                 session.close();
203             } catch (JMSException JavaDoc exception) {
204                 _log.debug("Failed to close session", exception);
205             }
206         }
207         _sessions.clear();
208         _manager.closed(this);
209     }
210
211     /**
212      * Notify closure of a session
213      *
214      * @param session the closed session
215      */

216     public synchronized void closed(JmsServerSession session) {
217         _sessions.remove(session);
218     }
219
220 }
221
Popular Tags