KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > socket > SocketManagedConnection


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 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: SocketManagedConnection.java,v 1.2 2005/04/19 03:53:14 tanderson Exp $
44  */

45 package org.exolab.jms.net.socket;
46
47 import java.io.IOException JavaDoc;
48 import java.net.Socket JavaDoc;
49 import java.security.Principal JavaDoc;
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 /**
66  * <code>SocketManagedConnection</code> manages multiple <code>Connection</code>
67  * instances over a single socket.
68  *
69  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
70  * @version $Revision: 1.2 $ $Date: 2005/04/19 03:53:14 $
71  */

72 public abstract class SocketManagedConnection
73         extends MultiplexedManagedConnection {
74
75     /**
76      * The underlying socket.
77      */

78     private Socket JavaDoc _socket;
79
80     /**
81      * The remote address to which this is connected.
82      */

83     private URI _remoteURI;
84
85     /**
86      * The the local address that this connection is bound to.
87      */

88     private URI _localURI;
89
90     /**
91      * The logger.
92      */

93     protected static final Log _log =
94             LogFactory.getLog(SocketManagedConnection.class);
95
96     /**
97      * Construct a new client <code>SocketManagedConnection</code>.
98      *
99      * @param principal the security principal
100      * @param info the connection request info
101      * @throws ResourceException if a socket cannot be created
102      */

103     public SocketManagedConnection(Principal JavaDoc principal,
104                                    SocketRequestInfo info)
105             throws ResourceException {
106         super(principal);
107         if (info == null) {
108             throw new IllegalArgumentException JavaDoc("Argument 'info' is null");
109         }
110         try {
111             Socket JavaDoc socket = createSocket(info);
112             init(info.getURI(), socket);
113         } catch (java.net.ConnectException JavaDoc 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 JavaDoc 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     /**
131      * Construct a new server <code>SocketManagedConnection</code>.
132      *
133      * @param uri the URI the acceptor was listening on
134      * @param socket the socket
135      * @param authenticator the connection authenticator
136      * @param pool the thread pool for handling invocation requests.
137      * May be <code>null</code>
138      * @throws ResourceException if an error occurs accessing the socket
139      */

140     public SocketManagedConnection(URI uri, Socket JavaDoc socket,
141                                    Authenticator authenticator,
142                                    PooledExecutor pool)
143             throws ResourceException {
144         super(authenticator, pool);
145         if (uri == null) {
146             throw new IllegalArgumentException JavaDoc("Argument 'uri' is null");
147         }
148         if (socket == null) {
149             throw new IllegalArgumentException JavaDoc("Argument 'socket' is null");
150         }
151         if (authenticator == null) {
152             throw new IllegalArgumentException JavaDoc(
153                     "Argument 'authenticator' is null");
154         }
155         init(uri, socket);
156     }
157
158     /**
159      * Returns the remote address to which this is connected.
160      *
161      * @return the remote address to which this is connected
162      */

163     public URI getRemoteURI() {
164         return _remoteURI;
165     }
166
167     /**
168      * Returns the local address that this connection is bound to.
169      *
170      * @return the local address that this connection is bound to
171      */

172     public URI getLocalURI() {
173         return _localURI;
174     }
175
176     /**
177      * Creates a new socket.
178      *
179      * @param info the connection request info
180      * @return a new socket
181      * @throws IOException for any I/O error
182      */

183     protected Socket JavaDoc createSocket(SocketRequestInfo info) throws IOException JavaDoc {
184         Socket JavaDoc result;
185         try {
186             result = createSocket(info.getHost(), info.getPort());
187         } catch (IOException JavaDoc 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     /**
202      * Creates a new socket.
203      *
204      * @param host the host to connect to
205      * @param port the port to connect to
206      * @return a new socket
207      * @throws IOException for any I/O error
208      */

209     protected Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc {
210         return new Socket JavaDoc(host, port);
211     }
212
213     /**
214      * Returns the endpoint to multiplex data over.
215      *
216      * @return the endpoint to multiplex data over
217      * @throws IOException for any I/O error
218      */

219     protected Endpoint createEndpoint() throws IOException JavaDoc {
220         return new SocketEndpoint(_remoteURI.getScheme(), _socket);
221     }
222
223     /**
224      * Initialises this connection.
225      *
226      * @param uri the URI representing this connection
227      * @param socket the socket
228      * @throws ResourceException for any error
229      */

230     protected void init(URI uri, Socket JavaDoc socket) throws ResourceException {
231         _socket = socket;
232         try {
233             String JavaDoc localHost = socket.getLocalAddress().getHostAddress();
234             int localPort = socket.getLocalPort();
235             _localURI = URIHelper.create(uri.getScheme(), localHost, localPort);
236
237             String JavaDoc 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