1 45 package org.exolab.jms.net.vm; 46 47 import java.security.Principal ; 48 import java.util.HashMap ; 49 import java.util.Map ; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 import org.exolab.jms.net.connector.Authenticator; 55 import org.exolab.jms.net.connector.ConnectException; 56 import org.exolab.jms.net.connector.ManagedConnectionAcceptor; 57 import org.exolab.jms.net.connector.ManagedConnectionAcceptorListener; 58 import org.exolab.jms.net.connector.ResourceException; 59 import org.exolab.jms.net.connector.SecurityException; 60 import org.exolab.jms.net.connector.URIRequestInfo; 61 import org.exolab.jms.net.uri.URI; 62 63 64 72 class VMManagedConnectionAcceptor implements ManagedConnectionAcceptor { 73 74 77 private final Authenticator _authenticator; 78 79 82 private final URI _uri; 83 84 87 private ManagedConnectionAcceptorListener _listener; 88 89 92 private static Map _acceptors = new HashMap (); 93 94 97 private static final Log _log = 98 LogFactory.getLog(VMManagedConnectionAcceptor.class); 99 100 101 107 public VMManagedConnectionAcceptor(Authenticator authenticator, 108 URIRequestInfo info) { 109 _authenticator = authenticator; 110 _uri = info.getURI(); 111 } 112 113 119 public synchronized void accept(ManagedConnectionAcceptorListener listener) 120 throws ResourceException { 121 if (listener == null) { 122 throw new IllegalArgumentException ("Argument 'listener' is null"); 123 } 124 if (_listener != null) { 125 throw new ResourceException( 126 "Acceptor already accepting connections at URI=" + _uri); 127 } 128 synchronized (_acceptors) { 129 if (_acceptors.containsKey(_uri)) { 130 throw new ResourceException("Cannot accept connections on URI=" 131 + _uri 132 + ". Address in use."); 133 } 134 _acceptors.put(_uri, this); 135 } 136 _listener = listener; 137 138 if (_log.isDebugEnabled()) { 139 _log.debug("VM connector accepting requests at URI=" + _uri); 140 } 141 } 142 143 147 public synchronized void close() { 148 if (_listener != null) { 149 synchronized (_acceptors) { 150 _acceptors.remove(_uri); 151 } 152 } 153 _listener = null; 154 } 155 156 161 public URI getURI() { 162 return _uri; 163 } 164 165 178 protected static VMInvoker connect(Principal principal, URIRequestInfo info, 179 VMInvoker client, URI uri) 180 throws ResourceException { 181 VMManagedConnectionAcceptor acceptor; 182 URI acceptURI = info.getURI(); 183 synchronized (_acceptors) { 184 acceptor = (VMManagedConnectionAcceptor) _acceptors.get(acceptURI); 185 } 186 if (acceptor == null) { 187 throw new ConnectException("Connection refused, URI=" + acceptURI); 188 } 189 return acceptor.accept(principal, info, client, uri); 190 } 191 192 205 protected VMInvoker accept(Principal principal, URIRequestInfo info, 206 VMInvoker client, URI uri) 207 throws ResourceException { 208 if (!_authenticator.authenticate(principal)) { 209 throw new SecurityException ("Failed to authenticate: " + principal); 210 } 211 VMManagedConnection connection = 212 new VMManagedConnection(principal, info, client, uri); 213 VMInvoker invoker = new VMInvoker(connection); 214 ManagedConnectionAcceptorListener listener; 215 synchronized (this) { 216 listener = _listener; 217 } 218 if (listener == null) { 219 throw new ConnectException("Connection refused, URI=" + _uri); 220 } 221 listener.accepted(this, connection); 222 return invoker; 223 } 224 } 225 | Popular Tags |