1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.io.bio;
15
16 import java.io.IOException;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.net.Socket;
20
21 import org.eclipse.jetty.util.StringUtil;
22 import org.eclipse.jetty.util.log.Log;
23
24
25
26
27
28
29
30 public class SocketEndPoint extends StreamEndPoint
31 {
32 Socket _socket;
33 InetSocketAddress _local;
34 InetSocketAddress _remote;
35
36
37
38
39 public SocketEndPoint(Socket socket)
40 throws IOException
41 {
42 super(socket.getInputStream(),socket.getOutputStream());
43 _socket=socket;
44 }
45
46
47
48
49 public boolean isOpen()
50 {
51 return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
52 }
53
54
55
56
57 public void close() throws IOException
58 {
59 if (!_socket.isClosed() && !_socket.isOutputShutdown())
60 {
61 try
62 {
63 _socket.shutdownOutput();
64 }
65 catch(IOException e)
66 {
67 Log.ignore(e);
68 }
69 catch(UnsupportedOperationException e)
70 {
71 Log.ignore(e);
72 }
73 }
74 _socket.close();
75 _in=null;
76 _out=null;
77 }
78
79
80
81
82
83
84 public String getLocalAddr()
85 {
86 if (_local==null)
87 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
88
89 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
90 return StringUtil.ALL_INTERFACES;
91
92 return _local.getAddress().getHostAddress();
93 }
94
95
96
97
98
99 public String getLocalHost()
100 {
101 if (_local==null)
102 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
103
104 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
105 return StringUtil.ALL_INTERFACES;
106
107 return _local.getAddress().getCanonicalHostName();
108 }
109
110
111
112
113
114 public int getLocalPort()
115 {
116 if (_local==null)
117 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
118 if (_local==null)
119 return -1;
120 return _local.getPort();
121 }
122
123
124
125
126
127 public String getRemoteAddr()
128 {
129 if (_remote==null)
130 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
131 if (_remote==null)
132 return null;
133 InetAddress addr = _remote.getAddress();
134 return ( addr == null ? null : addr.getHostAddress() );
135 }
136
137
138
139
140
141 public String getRemoteHost()
142 {
143 if (_remote==null)
144 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
145 if (_remote==null)
146 return null;
147 return _remote.getAddress().getCanonicalHostName();
148 }
149
150
151
152
153
154 public int getRemotePort()
155 {
156 if (_remote==null)
157 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
158 if (_remote==null)
159 return -1;
160 return _remote.getPort();
161 }
162
163
164
165
166
167 public Object getTransport()
168 {
169 return _socket;
170 }
171 }