KickJava   Java API By Example, From Geeks To Geeks.

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


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: JmsServerConnectionManager.java,v 1.3 2005/05/24 13:09:09 tanderson Exp $
44  */

45 package org.exolab.jms.server;
46
47 import java.util.HashMap JavaDoc;
48 import java.util.HashSet JavaDoc;
49 import javax.jms.InvalidClientIDException JavaDoc;
50 import javax.jms.JMSException JavaDoc;
51 import javax.jms.JMSSecurityException JavaDoc;
52
53 import org.exolab.jms.authentication.AuthenticationMgr;
54
55
56 /**
57  * The <code>JmsServerConnectionManager</code> is responsible for managing all
58  * connections to the server.
59  *
60  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
61  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
62  * @version $Revision: 1.3 $ $Date: 2005/05/24 13:09:09 $
63  * @see JmsServerConnection
64  */

65 public class JmsServerConnectionManager implements ServerConnectionFactory {
66
67     /**
68      * The set of active connections.
69      */

70     private HashMap JavaDoc _connections = new HashMap JavaDoc();
71
72     /**
73      * The set of client identifiers.
74      */

75     private HashSet JavaDoc _clientIDs = new HashSet JavaDoc();
76
77     /**
78      * Seed for connection identifiers.
79      */

80     private long _seed = 0;
81
82     /**
83      * Holds the singleton instance of the class.
84      */

85     private static JmsServerConnectionManager _instance =
86             new JmsServerConnectionManager();
87
88     /**
89      * Construct a new <code>JmsServerConnectionManager</code>.
90      */

91     private JmsServerConnectionManager() {
92     }
93
94     /**
95      * Returns the singleton instance of this class.
96      *
97      * @return the singleton instance
98      */

99     public static JmsServerConnectionManager instance() {
100         return _instance;
101     }
102
103     /**
104      * Creates a connection with the specified user identity.
105      * <p/>
106      * The connection is created in stopped mode. No messages will be delivered
107      * until the <code>Connection.start</code> method is explicitly called.
108      * <p/>
109      * If <code>clientID</code> is specified, it indicates the pre-configured
110      * client identifier associated with the client <code>ConnectionFactory</code>
111      * object.
112      *
113      * @param clientID the pre-configured client identifier. May be
114      * <code>null</code> <code>null</code>.
115      * @param userName the caller's user name
116      * @param password the caller's password
117      * @return a newly created connection
118      * @throws InvalidClientIDException if the JMS client specifies an invalid
119      * or duplicate client ID.
120      * @throws JMSException if the JMS provider fails to create the
121      * connection due to some internal error.
122      * @throws JMSSecurityException if client authentication fails due to an
123      * invalid user name or password.
124      */

125     public ServerConnection createConnection(String JavaDoc clientID, String JavaDoc userName,
126                                              String JavaDoc password)
127             throws JMSException JavaDoc {
128         if (!AuthenticationMgr.instance().validateUser(userName, password)) {
129             throw new JMSSecurityException JavaDoc("Failed to authenticate user " +
130                                            userName);
131         }
132
133         JmsServerConnection result = null;
134         synchronized (_connections) {
135             addClientID(clientID);
136             long connectionId = ++_seed;
137             result = new JmsServerConnection(this, connectionId, clientID);
138             _connections.put(new Long JavaDoc(connectionId), result);
139         }
140
141         return result;
142     }
143
144     /**
145      * Returns the connection associated with a particular connection
146      * identifier.
147      *
148      * @param connectionId the connection identifier
149      * @return the connection associated with <code>connectionId</code>, or
150      * <code>null</code> if none exists
151      */

152     public JmsServerConnection getConnection(long connectionId) {
153         JmsServerConnection result = null;
154         synchronized (_connections) {
155             Long JavaDoc key = new Long JavaDoc(connectionId);
156             result = (JmsServerConnection) _connections.get(key);
157         }
158         return result;
159     }
160
161     /**
162      * Notify closure of a connection.
163      *
164      * @param connection the connection that has been closed
165      */

166     public void closed(JmsServerConnection connection) {
167         synchronized (_connections) {
168             Long JavaDoc key = new Long JavaDoc(connection.getConnectionId());
169             _connections.remove(key);
170             _clientIDs.remove(connection.getClientID());
171         }
172     }
173
174     /**
175      * Register a client identifer.
176      *
177      * @param clientID the client identifier. If <code>null</code, it is
178      * ignored.
179      * @throws InvalidClientIDException if the identifier is a duplicate.
180      */

181     public void addClientID(String JavaDoc clientID) throws InvalidClientIDException JavaDoc {
182         synchronized (_connections) {
183             if (clientID != null) {
184                 if (!_clientIDs.add(clientID)) {
185                     throw new InvalidClientIDException JavaDoc(
186                             "Duplicate clientID: " + clientID);
187                 }
188             }
189         }
190     }
191
192 }
193
Popular Tags