1 45 package org.exolab.jms.server; 46 47 import java.util.HashMap ; 48 import java.util.HashSet ; 49 import javax.jms.InvalidClientIDException ; 50 import javax.jms.JMSException ; 51 import javax.jms.JMSSecurityException ; 52 53 import org.exolab.jms.authentication.AuthenticationMgr; 54 55 56 65 public class JmsServerConnectionManager implements ServerConnectionFactory { 66 67 70 private HashMap _connections = new HashMap (); 71 72 75 private HashSet _clientIDs = new HashSet (); 76 77 80 private long _seed = 0; 81 82 85 private static JmsServerConnectionManager _instance = 86 new JmsServerConnectionManager(); 87 88 91 private JmsServerConnectionManager() { 92 } 93 94 99 public static JmsServerConnectionManager instance() { 100 return _instance; 101 } 102 103 125 public ServerConnection createConnection(String clientID, String userName, 126 String password) 127 throws JMSException { 128 if (!AuthenticationMgr.instance().validateUser(userName, password)) { 129 throw new JMSSecurityException ("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 (connectionId), result); 139 } 140 141 return result; 142 } 143 144 152 public JmsServerConnection getConnection(long connectionId) { 153 JmsServerConnection result = null; 154 synchronized (_connections) { 155 Long key = new Long (connectionId); 156 result = (JmsServerConnection) _connections.get(key); 157 } 158 return result; 159 } 160 161 166 public void closed(JmsServerConnection connection) { 167 synchronized (_connections) { 168 Long key = new Long (connection.getConnectionId()); 169 _connections.remove(key); 170 _clientIDs.remove(connection.getClientID()); 171 } 172 } 173 174 181 public void addClientID(String clientID) throws InvalidClientIDException { 182 synchronized (_connections) { 183 if (clientID != null) { 184 if (!_clientIDs.add(clientID)) { 185 throw new InvalidClientIDException ( 186 "Duplicate clientID: " + clientID); 187 } 188 } 189 } 190 } 191 192 } 193 | Popular Tags |