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