1 45 package org.exolab.jms.net.connector; 46 47 import java.security.Principal ; 48 import java.util.Collection ; 49 import java.util.HashMap ; 50 import java.util.Map ; 51 import java.util.Iterator ; 52 53 import org.exolab.jms.net.uri.URI; 54 55 56 63 public abstract class AbstractConnectionManager 64 implements ConnectionManager, ConnectionFactory { 65 66 70 private final Map _factories = new HashMap (); 71 72 75 private final InvocationHandler _handler; 76 77 80 private final Authenticator _authenticator; 81 82 86 private final Map _connectionFactories = new HashMap (); 87 88 91 private final Map _properties; 92 93 96 private CallerListener _listener; 97 98 99 106 public AbstractConnectionManager(InvocationHandler handler, 107 Authenticator authenticator, 108 Map properties) { 109 if (handler == null) { 110 throw new IllegalArgumentException ("Argument 'handler' is null"); 111 } 112 if (authenticator == null) { 113 throw new IllegalArgumentException ( 114 "Argument 'authenticator' is null"); 115 } 116 _handler = handler; 117 _authenticator = authenticator; 118 _properties = properties; 119 } 120 121 131 public Connection allocateConnection(ManagedConnectionFactory factory, 132 Principal principal, 133 ConnectionRequestInfo info) 134 throws ResourceException { 135 136 ConnectionPool pool = getConnectionPool(factory); 137 ManagedConnection connection = pool.matchManagedConnections(principal, 138 info); 139 if (connection == null) { 140 connection = pool.createManagedConnection(principal, info); 141 } 142 return connection.getConnection(); 143 } 144 145 152 public void accept(ManagedConnectionFactory factory, 153 ConnectionRequestInfo info) throws ResourceException { 154 ConnectionPool pool = getConnectionPool(factory); 155 ManagedConnectionAcceptor acceptor = 156 pool.matchManagedConnectionAcceptors(info); 157 if (acceptor == null) { 158 acceptor = pool.createManagedConnectionAcceptor(_authenticator, 159 info); 160 acceptor.accept(pool.getManagedConnectionAcceptorListener()); 161 } 162 } 163 164 171 public boolean canConnect(URI uri) { 172 ConnectionFactory factory = getFactoryForConnect(uri); 173 return (factory != null); 174 } 175 176 185 public Connection getConnection(Principal principal, URI uri) 186 throws ResourceException { 187 return getConnection(principal, uri, null); 188 } 189 190 201 public Connection getConnection(Principal principal, URI uri, 202 Map properties) 203 throws ResourceException { 204 ConnectionFactory factory = getFactoryForConnect(uri); 205 if (factory == null) { 206 throw new ResourceException("No connector for URI=" + uri); 207 } 208 return factory.getConnection(principal, uri, properties); 209 } 210 211 219 public boolean canAccept(URI uri) { 220 ConnectionFactory factory = getFactoryForAccept(uri); 221 return (factory != null); 222 } 223 224 232 public void accept(URI uri) throws ResourceException { 233 accept(uri, null); 234 } 235 236 245 public void accept(URI uri, Map properties) 246 throws ResourceException { 247 ConnectionFactory factory = getFactoryForAccept(uri); 248 if (factory == null) { 249 throw new ResourceException("No connector for URI=" + uri); 250 } 251 factory.accept(uri, properties); 252 } 253 254 259 public synchronized void setCallerListener(CallerListener listener) { 260 _listener = listener; 261 Iterator iterator = _factories.values().iterator(); 262 while (iterator.hasNext()) { 263 ConnectionPool pool = (ConnectionPool) iterator.next(); 264 pool.setCallerListener(_listener); 265 } 266 } 267 268 273 public synchronized void close() throws ResourceException { 274 Iterator iterator = _factories.values().iterator(); 275 while (iterator.hasNext()) { 276 ConnectionPool pool = (ConnectionPool) iterator.next(); 277 pool.close(); 278 } 279 _factories.clear(); 280 } 281 282 290 protected synchronized ConnectionFactory getFactoryForConnect(URI uri) { 291 ConnectionFactory result = null; 292 Iterator iterator = _connectionFactories.keySet().iterator(); 293 while (iterator.hasNext()) { 294 ConnectionFactory factory = (ConnectionFactory) iterator.next(); 295 if (factory.canConnect(uri)) { 296 result = factory; 297 break; 298 } 299 } 300 return result; 301 } 302 303 311 protected synchronized ConnectionFactory getFactoryForAccept(URI uri) { 312 ConnectionFactory result = null; 313 Iterator iterator = _connectionFactories.keySet().iterator(); 314 while (iterator.hasNext()) { 315 ConnectionFactory factory = (ConnectionFactory) iterator.next(); 316 if (factory.canAccept(uri)) { 317 result = factory; 318 break; 319 } 320 } 321 return result; 322 } 323 324 330 protected synchronized void addManagedConnectionFactory( 331 ManagedConnectionFactory factory) throws ResourceException { 332 ConnectionPool pool = createConnectionPool(factory, _handler, this); 333 pool.setCallerListener(_listener); 334 _factories.put(factory, pool); 335 _connectionFactories.put(factory.createConnectionFactory(this), 336 factory); 337 } 338 339 344 protected synchronized Collection getManagedConnectionFactories() { 345 return _factories.keySet(); 346 } 347 348 358 protected ConnectionPool createConnectionPool( 359 ManagedConnectionFactory factory, InvocationHandler handler, 360 ConnectionFactory resolver) throws ResourceException { 361 return new DefaultConnectionPool(factory, handler, resolver, 362 _properties); 363 } 364 365 373 protected synchronized ConnectionPool getConnectionPool( 374 ManagedConnectionFactory factory) throws ResourceException { 375 ConnectionPool pool = (ConnectionPool) _factories.get(factory); 376 if (pool == null) { 377 throw new ResourceException("Connection pool not found"); 378 } 379 return pool; 380 } 381 382 } 383 | Popular Tags |