View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
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 javax.net.ssl.SSLSocket;
22  
23  import org.eclipse.jetty.util.StringUtil;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  
27  public class SocketEndPoint extends StreamEndPoint
28  {
29      private static final Logger LOG = Log.getLogger(SocketEndPoint.class);
30  
31      final Socket _socket;
32      final InetSocketAddress _local;
33      final InetSocketAddress _remote;
34  
35      /* ------------------------------------------------------------ */
36      /**
37       *
38       */
39      public SocketEndPoint(Socket socket)
40      	throws IOException
41      {
42          super(socket.getInputStream(),socket.getOutputStream());
43          _socket=socket;
44          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
45          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
46          super.setMaxIdleTime(_socket.getSoTimeout());
47      }
48  
49      /* ------------------------------------------------------------ */
50      /**
51       *
52       */
53      protected SocketEndPoint(Socket socket, int maxIdleTime)
54          throws IOException
55      {
56          super(socket.getInputStream(),socket.getOutputStream());
57          _socket=socket;
58          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
59          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
60          _socket.setSoTimeout(maxIdleTime>0?maxIdleTime:0);
61          super.setMaxIdleTime(maxIdleTime);
62      }
63  
64      /* ------------------------------------------------------------ */
65      /* (non-Javadoc)
66       * @see org.eclipse.io.BufferIO#isClosed()
67       */
68      @Override
69      public boolean isOpen()
70      {
71          return super.isOpen() && _socket!=null && !_socket.isClosed();
72      }
73  
74      /* ------------------------------------------------------------ */
75      @Override
76      public boolean isInputShutdown()
77      {
78          if (_socket instanceof SSLSocket)
79              return super.isInputShutdown();
80          return _socket.isClosed() || _socket.isInputShutdown();
81      }
82  
83      /* ------------------------------------------------------------ */
84      @Override
85      public boolean isOutputShutdown()
86      {
87          if (_socket instanceof SSLSocket)
88              return super.isOutputShutdown();
89  
90          return _socket.isClosed() || _socket.isOutputShutdown();
91      }
92  
93  
94      /* ------------------------------------------------------------ */
95      /*
96       */
97      protected final void shutdownSocketOutput() throws IOException
98      {
99          if (!_socket.isClosed())
100         {
101             if (!_socket.isOutputShutdown())
102                 _socket.shutdownOutput();
103             if (_socket.isInputShutdown())
104                 _socket.close();
105         }
106     }
107 
108     /* ------------------------------------------------------------ */
109     /*
110      * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
111      */
112     @Override
113     public void shutdownOutput() throws IOException
114     {
115         if (_socket instanceof SSLSocket)
116             super.shutdownOutput();
117         else
118             shutdownSocketOutput();
119     }
120 
121 
122     /* ------------------------------------------------------------ */
123     /*
124      */
125     public void shutdownSocketInput() throws IOException
126     {
127         if (!_socket.isClosed())
128         {
129             if (!_socket.isInputShutdown())
130                 _socket.shutdownInput();
131             if (_socket.isOutputShutdown())
132                 _socket.close();
133         }
134     }
135 
136     /* ------------------------------------------------------------ */
137     /*
138      * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
139      */
140     @Override
141     public void shutdownInput() throws IOException
142     {
143         if (_socket instanceof SSLSocket)
144             super.shutdownInput();
145         else
146             shutdownSocketInput();
147     }
148 
149     /* ------------------------------------------------------------ */
150     /* (non-Javadoc)
151      * @see org.eclipse.io.BufferIO#close()
152      */
153     @Override
154     public void close() throws IOException
155     {
156         _socket.close();
157         _in=null;
158         _out=null;
159     }
160 
161 
162     /* ------------------------------------------------------------ */
163     /*
164      * @see org.eclipse.io.EndPoint#getLocalAddr()
165      */
166     @Override
167     public String getLocalAddr()
168     {
169        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
170            return StringUtil.ALL_INTERFACES;
171 
172         return _local.getAddress().getHostAddress();
173     }
174 
175     /* ------------------------------------------------------------ */
176     /*
177      * @see org.eclipse.io.EndPoint#getLocalHost()
178      */
179     @Override
180     public String getLocalHost()
181     {
182        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
183            return StringUtil.ALL_INTERFACES;
184 
185         return _local.getAddress().getCanonicalHostName();
186     }
187 
188     /* ------------------------------------------------------------ */
189     /*
190      * @see org.eclipse.io.EndPoint#getLocalPort()
191      */
192     @Override
193     public int getLocalPort()
194     {
195         if (_local==null)
196             return -1;
197         return _local.getPort();
198     }
199 
200     /* ------------------------------------------------------------ */
201     /*
202      * @see org.eclipse.io.EndPoint#getRemoteAddr()
203      */
204     @Override
205     public String getRemoteAddr()
206     {
207         if (_remote==null)
208             return null;
209         InetAddress addr = _remote.getAddress();
210         return ( addr == null ? null : addr.getHostAddress() );
211     }
212 
213     /* ------------------------------------------------------------ */
214     /*
215      * @see org.eclipse.io.EndPoint#getRemoteHost()
216      */
217     @Override
218     public String getRemoteHost()
219     {
220         if (_remote==null)
221             return null;
222         return _remote.getAddress().getCanonicalHostName();
223     }
224 
225     /* ------------------------------------------------------------ */
226     /*
227      * @see org.eclipse.io.EndPoint#getRemotePort()
228      */
229     @Override
230     public int getRemotePort()
231     {
232         if (_remote==null)
233             return -1;
234         return _remote.getPort();
235     }
236 
237     /* ------------------------------------------------------------ */
238     /*
239      * @see org.eclipse.io.EndPoint#getConnection()
240      */
241     @Override
242     public Object getTransport()
243     {
244         return _socket;
245     }
246 
247     /* ------------------------------------------------------------ */
248     /**
249      * @see org.eclipse.jetty.io.bio.StreamEndPoint#setMaxIdleTime(int)
250      */
251     @Override
252     public void setMaxIdleTime(int timeMs) throws IOException
253     {
254         if (timeMs!=getMaxIdleTime())
255             _socket.setSoTimeout(timeMs>0?timeMs:0);
256         super.setMaxIdleTime(timeMs);
257     }
258 
259 
260     /* ------------------------------------------------------------ */
261     @Override
262     protected void idleExpired() throws IOException
263     {
264         try
265         {
266             if (!isInputShutdown())
267                 shutdownInput();
268         }
269         catch(IOException e)
270         {
271             LOG.ignore(e);
272             _socket.close();
273         }
274     }
275 
276     @Override
277     public String toString()
278     {
279         return _local + " <--> " + _remote;
280     }
281 }