1 45 package org.exolab.jms.net.socket; 46 47 import java.io.IOException ; 48 import java.net.Socket ; 49 import java.security.Principal ; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 54 55 import org.exolab.jms.net.connector.Authenticator; 56 import org.exolab.jms.net.connector.ConnectException; 57 import org.exolab.jms.net.connector.ResourceException; 58 import org.exolab.jms.net.multiplexer.Endpoint; 59 import org.exolab.jms.net.multiplexer.MultiplexedManagedConnection; 60 import org.exolab.jms.net.uri.InvalidURIException; 61 import org.exolab.jms.net.uri.URI; 62 import org.exolab.jms.net.uri.URIHelper; 63 64 65 72 public abstract class SocketManagedConnection 73 extends MultiplexedManagedConnection { 74 75 78 private Socket _socket; 79 80 83 private URI _remoteURI; 84 85 88 private URI _localURI; 89 90 93 protected static final Log _log = 94 LogFactory.getLog(SocketManagedConnection.class); 95 96 103 public SocketManagedConnection(Principal principal, 104 SocketRequestInfo info) 105 throws ResourceException { 106 super(principal); 107 if (info == null) { 108 throw new IllegalArgumentException ("Argument 'info' is null"); 109 } 110 try { 111 Socket socket = createSocket(info); 112 init(info.getURI(), socket); 113 } catch (java.net.ConnectException exception) { 114 if (_log.isDebugEnabled()) { 115 _log.debug("Failed to connect to URI=" + info.getURI(), 116 exception); 117 } 118 throw new ConnectException("Failed to connect to URI=" 119 + info.getURI(), exception); 120 } catch (IOException exception) { 121 if (_log.isDebugEnabled()) { 122 _log.debug("Failed to connect to URI=" + info.getURI(), 123 exception); 124 } 125 throw new ResourceException("Failed to connect to URI=" 126 + info.getURI(), exception); 127 } 128 } 129 130 140 public SocketManagedConnection(URI uri, Socket socket, 141 Authenticator authenticator, 142 PooledExecutor pool) 143 throws ResourceException { 144 super(authenticator, pool); 145 if (uri == null) { 146 throw new IllegalArgumentException ("Argument 'uri' is null"); 147 } 148 if (socket == null) { 149 throw new IllegalArgumentException ("Argument 'socket' is null"); 150 } 151 if (authenticator == null) { 152 throw new IllegalArgumentException ( 153 "Argument 'authenticator' is null"); 154 } 155 init(uri, socket); 156 } 157 158 163 public URI getRemoteURI() { 164 return _remoteURI; 165 } 166 167 172 public URI getLocalURI() { 173 return _localURI; 174 } 175 176 183 protected Socket createSocket(SocketRequestInfo info) throws IOException { 184 Socket result; 185 try { 186 result = createSocket(info.getHost(), info.getPort()); 187 } catch (IOException exception) { 188 URI uri = info.getAlternativeURI(); 189 if (uri == null) { 190 throw exception; 191 } 192 if (_log.isDebugEnabled()) { 193 _log.debug("Failed to connect using URI=" + info.getURI() 194 + ", attempting URI=" + uri); 195 } 196 result = createSocket(uri.getHost(), uri.getPort()); 197 } 198 return result; 199 } 200 201 209 protected Socket createSocket(String host, int port) throws IOException { 210 return new Socket (host, port); 211 } 212 213 219 protected Endpoint createEndpoint() throws IOException { 220 return new SocketEndpoint(_remoteURI.getScheme(), _socket); 221 } 222 223 230 protected void init(URI uri, Socket socket) throws ResourceException { 231 _socket = socket; 232 try { 233 String localHost = socket.getLocalAddress().getHostAddress(); 234 int localPort = socket.getLocalPort(); 235 _localURI = URIHelper.create(uri.getScheme(), localHost, localPort); 236 237 String remoteHost = socket.getInetAddress().getHostAddress(); 238 int remotePort = socket.getPort(); 239 _remoteURI = URIHelper.create(uri.getScheme(), remoteHost, 240 remotePort); 241 } catch (InvalidURIException exception) { 242 throw new ResourceException("Failed to create URI", exception); 243 } 244 } 245 } 246 | Popular Tags |