KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > http > AbstractHTTPManagedConnection


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: AbstractHTTPManagedConnection.java,v 1.5 2005/05/27 13:58:01 tanderson Exp $
44  */

45 package org.exolab.jms.net.http;
46
47 import java.io.IOException JavaDoc;
48 import java.net.Socket JavaDoc;
49 import java.security.Principal JavaDoc;
50
51 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
52
53 import org.exolab.jms.common.uuid.UUIDGenerator;
54 import org.exolab.jms.net.connector.Authenticator;
55 import org.exolab.jms.net.connector.ConnectException;
56 import org.exolab.jms.net.connector.ResourceException;
57 import org.exolab.jms.net.connector.SecurityException;
58 import org.exolab.jms.net.multiplexer.Endpoint;
59 import org.exolab.jms.net.multiplexer.MultiplexedManagedConnection;
60 import org.exolab.jms.net.multiplexer.Multiplexer;
61 import org.exolab.jms.net.socket.SocketEndpoint;
62 import org.exolab.jms.net.uri.InvalidURIException;
63 import org.exolab.jms.net.uri.URI;
64 import org.exolab.jms.net.uri.URIHelper;
65
66
67 /**
68  * <code>AbstractHTTPManagedConnection</code> manages multiple
69  * <code>Connection</code> instances over a single HTTP connection.
70  *
71  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
72  * @version $Revision: 1.5 $ $Date: 2005/05/27 13:58:01 $
73  */

74 abstract class AbstractHTTPManagedConnection
75         extends MultiplexedManagedConnection {
76
77     /**
78      * The endpoint.
79      */

80     protected Endpoint _endpoint;
81
82     /**
83      * The remote address to which this is connected.
84      */

85     private URI _remoteURI;
86
87     /**
88      * The local address that this connection is bound to.
89      */

90     private URI _localURI;
91
92
93     /**
94      * Construct a new client <code>HTTPManagedConnection</code>.
95      *
96      * @param principal the security principal.
97      * @param info the connection request info
98      * @throws ResourceException if a socket cannot be created
99      */

100     public AbstractHTTPManagedConnection(Principal JavaDoc principal,
101                                          HTTPRequestInfo info)
102             throws ResourceException {
103         super(principal);
104         if (info == null) {
105             throw new IllegalArgumentException JavaDoc("Argument 'info' is null");
106         }
107         final URI uri = info.getURI();
108         try {
109             _endpoint = new HTTPEndpoint(info);
110         } catch (IOException JavaDoc exception) {
111             throw new ConnectException("Failed to connect to URI="
112                                        + info.getURI(), exception);
113         }
114         _remoteURI = URIHelper.convertHostToAddress(uri);
115
116         try {
117             _localURI = URIHelper.create(uri.getScheme(), null, -1,
118                                          UUIDGenerator.create());
119         } catch (InvalidURIException exception) {
120             throw new ResourceException("Failed to generate local URI",
121                                         exception);
122         }
123
124     }
125
126     /**
127      * Construct a new server <code>HTTPManagedConnection</code>.
128      *
129      * @param uri the URI the acceptor was listening on
130      * @param socket the socket
131      * @param authenticator the connection authenticator
132      * @param pool the thread pool for handling invocation requests.
133      * May be <code>null</code>
134      * @throws ResourceException if an error occurs accessing the socket
135      */

136     public AbstractHTTPManagedConnection(URI uri, Socket JavaDoc socket,
137                                          Authenticator authenticator,
138                                          PooledExecutor pool)
139         throws ResourceException {
140         super(authenticator, pool);
141         if (uri == null) {
142             throw new IllegalArgumentException JavaDoc("Argument 'uri' is null");
143         }
144         if (socket == null) {
145             throw new IllegalArgumentException JavaDoc("Argument 'socket' is null");
146         }
147         if (authenticator == null) {
148             throw new IllegalArgumentException JavaDoc(
149                     "Argument 'authenticator' is null");
150         }
151         String JavaDoc scheme = uri.getScheme();
152         int localPort = socket.getLocalPort();
153
154         try {
155             _localURI = URIHelper.create(scheme, uri.getHost(), localPort);
156         } catch (InvalidURIException exception) {
157             throw new ResourceException("Failed to generate local URI",
158                                         exception);
159         }
160         try {
161             _endpoint = new SocketEndpoint(scheme, socket);
162         } catch (IOException JavaDoc exception) {
163             throw new ResourceException("Failed to create endpoint", exception);
164         }
165     }
166
167     /**
168      * Returns the remote address to which this is connected.
169      *
170      * @return the remote address to which this is connected
171      */

172     public URI getRemoteURI() {
173         return _remoteURI;
174     }
175
176     /**
177      * Returns the local address that this connection is bound to.
178      *
179      * @return the local address that this connection is bound to
180      */

181     public URI getLocalURI() {
182         return _localURI;
183     }
184
185     /**
186      * Returns the endpoint to multiplex data over.
187      *
188      * @return the endpoint to multiplex data over
189      * @throws IOException for any I/O error
190      */

191     protected Endpoint createEndpoint() throws IOException JavaDoc {
192         return _endpoint;
193     }
194
195     /**
196      * Create a new client-side multiplexer.
197      *
198      * @param endpoint the endpoint to multiplex messages over
199      * @param principal the security principal
200      * @param pool thread pool for handling invocation requests
201      * @return a new client-side multiplexer
202      * @throws IOException if an I/O error occurs
203      * @throws SecurityException if connection is refused by the server
204      */

205     protected Multiplexer createMultiplexer(Endpoint endpoint,
206                                             Principal JavaDoc principal,
207                                             PooledExecutor pool)
208             throws IOException JavaDoc, SecurityException JavaDoc {
209         return new HTTPMultiplexer(this, endpoint, _localURI, principal, pool);
210     }
211
212     /**
213      * Create a new server-side multiplexer.
214      *
215      * @param endpoint the endpoint to multiplex messages over
216      * @param authenticator the connection authetnicator
217      * @param pool thread pool for handling invocation requests
218      * @throws IOException if an I/O error occurs
219      * @throws ResourceException if the authenticator cannot authenticate
220      * @return a new server-side multiplexer
221      */

222     protected Multiplexer createMultiplexer(Endpoint endpoint,
223                                             Authenticator authenticator,
224                                             PooledExecutor pool)
225             throws IOException JavaDoc, ResourceException {
226         HTTPMultiplexer result = new HTTPMultiplexer(this, endpoint,
227                                                      authenticator, pool);
228         _remoteURI = result.getClientURI();
229         return result;
230     }
231
232 }
233
Popular Tags