1 //========================================================================
2 //$Id: SocketEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
3 //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4 //------------------------------------------------------------------------
5 //Licensed under the Apache License, Version 2.0 (the "License");
6 //you may not use this file except in compliance with the License.
7 //You may obtain a copy of the License at
8 //http://www.apache.org/licenses/LICENSE-2.0
9 //Unless required by applicable law or agreed to in writing, software
10 //distributed under the License is distributed on an "AS IS" BASIS,
11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //See the License for the specific language governing permissions and
13 //limitations under the License.
14 //========================================================================
15
16 package org.mortbay.io.bio;
17
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import java.net.Socket;
22
23 import org.mortbay.io.Portable;
24 import org.mortbay.log.Log;
25
26 /**
27 * @author gregw
28 *
29 * To change the template for this generated type comment go to
30 * Window - Preferences - Java - Code Generation - Code and Comments
31 */
32 public class SocketEndPoint extends StreamEndPoint
33 {
34 final Socket _socket;
35 final InetSocketAddress _local;
36 final InetSocketAddress _remote;
37
38 /**
39 *
40 */
41 public SocketEndPoint(Socket socket)
42 throws IOException
43 {
44 super(socket.getInputStream(),socket.getOutputStream());
45 _socket=socket;
46 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
47 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
48 }
49
50 /* (non-Javadoc)
51 * @see org.mortbay.io.BufferIO#isClosed()
52 */
53 public boolean isOpen()
54 {
55 return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
56 }
57
58 /* ------------------------------------------------------------ */
59 /*
60 * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
61 */
62 public void shutdownOutput() throws IOException
63 {
64 if (!_socket.isClosed() && !_socket.isOutputShutdown())
65 _socket.shutdownOutput();
66 }
67
68 /* ------------------------------------------------------------ */
69 /* (non-Javadoc)
70 * @see org.eclipse.io.BufferIO#close()
71 */
72 public void close() throws IOException
73 {
74 _socket.close();
75 _in=null;
76 _out=null;
77 }
78
79
80 /* ------------------------------------------------------------ */
81 /*
82 * @see org.mortbay.io.EndPoint#getLocalAddr()
83 */
84 public String getLocalAddr()
85 {
86 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
87 return Portable.ALL_INTERFACES;
88
89 return _local.getAddress().getHostAddress();
90 }
91
92 /* ------------------------------------------------------------ */
93 /*
94 * @see org.mortbay.io.EndPoint#getLocalHost()
95 */
96 public String getLocalHost()
97 {
98 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
99 return Portable.ALL_INTERFACES;
100
101 return _local.getAddress().getCanonicalHostName();
102 }
103
104 /* ------------------------------------------------------------ */
105 /*
106 * @see org.mortbay.io.EndPoint#getLocalPort()
107 */
108 public int getLocalPort()
109 {
110 if (_local==null)
111 return -1;
112 return _local.getPort();
113 }
114
115 /* ------------------------------------------------------------ */
116 /*
117 * @see org.mortbay.io.EndPoint#getRemoteAddr()
118 */
119 public String getRemoteAddr()
120 {
121 if (_remote==null)
122 return null;
123 InetAddress addr = _remote.getAddress();
124 return ( addr == null ? null : addr.getHostAddress() );
125 }
126
127 /* ------------------------------------------------------------ */
128 /*
129 * @see org.mortbay.io.EndPoint#getRemoteHost()
130 */
131 public String getRemoteHost()
132 {
133 if (_remote==null)
134 return null;
135 return _remote.getAddress().getCanonicalHostName();
136 }
137
138 /* ------------------------------------------------------------ */
139 /*
140 * @see org.mortbay.io.EndPoint#getRemotePort()
141 */
142 public int getRemotePort()
143 {
144 if (_remote==null)
145 return -1;
146 return _remote.getPort();
147 }
148
149 /* ------------------------------------------------------------ */
150 /*
151 * @see org.mortbay.io.EndPoint#getConnection()
152 */
153 public Object getTransport()
154 {
155 return _socket;
156 }
157 }