View Javadoc

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 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SocketEndpoint.java,v 1.2 2005/03/18 04:11:33 tanderson Exp $
44   */
45  
46  package org.exolab.jms.net.socket;
47  
48  import java.io.IOException;
49  import java.io.InputStream;
50  import java.io.OutputStream;
51  import java.net.Socket;
52  
53  import org.exolab.jms.net.multiplexer.Endpoint;
54  import org.exolab.jms.net.uri.URI;
55  import org.exolab.jms.net.uri.URIHelper;
56  
57  
58  /***
59   * Adapts a <code>Socket</code> to the {@link Endpoint} interface.
60   *
61   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   * @version $Revision: 1.2 $ $Date: 2005/03/18 04:11:33 $
63   */
64  public class SocketEndpoint implements Endpoint {
65  
66      /***
67       * The socket URI.
68       */
69      private final URI _uri;
70  
71      /***
72       * The underlying socket.
73       */
74      private Socket _socket;
75  
76      /***
77       * The socket input stream.
78       */
79      private InputStream _in;
80  
81      /***
82       * The socket output stream.
83       */
84      private OutputStream _out;
85  
86  
87      /***
88       * Construct a new <code>SocketEndpoint</code>.
89       *
90       * @param scheme the URI scheme
91       * @param socket the underlying socket
92       * @throws IOException for any I/O error
93       */
94      public SocketEndpoint(String scheme, Socket socket)
95              throws IOException {
96          _uri = URIHelper.create(scheme,
97                                  socket.getInetAddress().getHostAddress(),
98                                  socket.getPort());
99          _socket = socket;
100         _in = socket.getInputStream();
101         _out = socket.getOutputStream();
102     }
103 
104     /***
105      * Returns the URI that the endpoint is connected to
106      *
107      * @return the URI that the endpoint is connected to
108      */
109     public URI getURI() {
110         return _uri;
111     }
112 
113     /***
114      * Returns an input stream that reads from this endpoint
115      *
116      * @return an input stream that reads from this endpoint
117      */
118     public InputStream getInputStream() {
119         return _in;
120     }
121 
122     /***
123      * Returns an output stream that writes to this endpoint
124      *
125      * @return an output stream that writes to this endpoint
126      */
127     public OutputStream getOutputStream() {
128         return _out;
129     }
130 
131     /***
132      * Closes the endpoint
133      *
134      * @throws IOException if an I/O error occurs while closing the endpoint
135      */
136     public void close() throws IOException {
137         _socket.close();
138     }
139 }