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  import javax.net.ssl.SSLSocket;
21  
22  import org.eclipse.jetty.util.StringUtil;
23  import org.eclipse.jetty.util.log.Log;
24  import org.eclipse.jetty.util.log.Logger;
25  
26  /**
27   *
28   * To change the template for this generated type comment go to
29   * Window - Preferences - Java - Code Generation - Code and Comments
30   */
31  public class SocketEndPoint extends StreamEndPoint
32  {
33      private static final Logger LOG = Log.getLogger(SocketEndPoint.class);
34  
35      final Socket _socket;
36      final InetSocketAddress _local;
37      final InetSocketAddress _remote;
38  
39      /* ------------------------------------------------------------ */
40      /**
41       *
42       */
43      public SocketEndPoint(Socket socket)
44      	throws IOException
45      {
46          super(socket.getInputStream(),socket.getOutputStream());
47          _socket=socket;
48          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
49          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
50          super.setMaxIdleTime(_socket.getSoTimeout());
51      }
52  
53      /* ------------------------------------------------------------ */
54      /**
55       *
56       */
57      protected SocketEndPoint(Socket socket, int maxIdleTime)
58          throws IOException
59      {
60          super(socket.getInputStream(),socket.getOutputStream());
61          _socket=socket;
62          _local=(InetSocketAddress)_socket.getLocalSocketAddress();
63          _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
64          _socket.setSoTimeout(maxIdleTime>0?maxIdleTime:0);
65          super.setMaxIdleTime(maxIdleTime);
66      }
67  
68      /* ------------------------------------------------------------ */
69      /* (non-Javadoc)
70       * @see org.eclipse.io.BufferIO#isClosed()
71       */
72      @Override
73      public boolean isOpen()
74      {
75          return super.isOpen() && _socket!=null && !_socket.isClosed();
76      }
77  
78      /* ------------------------------------------------------------ */
79      @Override
80      public boolean isInputShutdown()
81      {
82          return !isOpen() || super.isInputShutdown();
83      }
84  
85      /* ------------------------------------------------------------ */
86      @Override
87      public boolean isOutputShutdown()
88      {
89          return !isOpen() || super.isOutputShutdown();
90      }
91  
92      /* ------------------------------------------------------------ */
93      /*
94       * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
95       */
96      @Override
97      public void shutdownOutput() throws IOException
98      {
99          if (!isOutputShutdown())
100         {
101             super.shutdownOutput();
102             if (!(_socket instanceof SSLSocket))
103                 _socket.shutdownOutput();
104         }
105     }
106 
107 
108     /* ------------------------------------------------------------ */
109     /*
110      * @see org.eclipse.jetty.io.bio.StreamEndPoint#shutdownOutput()
111      */
112     @Override
113     public void shutdownInput() throws IOException
114     {
115         if (!isInputShutdown())
116         {
117             super.shutdownInput();
118             if (!(_socket instanceof SSLSocket))
119                 _socket.shutdownInput();
120         }
121     }
122 
123     /* ------------------------------------------------------------ */
124     /* (non-Javadoc)
125      * @see org.eclipse.io.BufferIO#close()
126      */
127     @Override
128     public void close() throws IOException
129     {
130         _socket.close();
131         _in=null;
132         _out=null;
133     }
134 
135 
136     /* ------------------------------------------------------------ */
137     /*
138      * @see org.eclipse.io.EndPoint#getLocalAddr()
139      */
140     @Override
141     public String getLocalAddr()
142     {
143        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
144            return StringUtil.ALL_INTERFACES;
145 
146         return _local.getAddress().getHostAddress();
147     }
148 
149     /* ------------------------------------------------------------ */
150     /*
151      * @see org.eclipse.io.EndPoint#getLocalHost()
152      */
153     @Override
154     public String getLocalHost()
155     {
156        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
157            return StringUtil.ALL_INTERFACES;
158 
159         return _local.getAddress().getCanonicalHostName();
160     }
161 
162     /* ------------------------------------------------------------ */
163     /*
164      * @see org.eclipse.io.EndPoint#getLocalPort()
165      */
166     @Override
167     public int getLocalPort()
168     {
169         if (_local==null)
170             return -1;
171         return _local.getPort();
172     }
173 
174     /* ------------------------------------------------------------ */
175     /*
176      * @see org.eclipse.io.EndPoint#getRemoteAddr()
177      */
178     @Override
179     public String getRemoteAddr()
180     {
181         if (_remote==null)
182             return null;
183         InetAddress addr = _remote.getAddress();
184         return ( addr == null ? null : addr.getHostAddress() );
185     }
186 
187     /* ------------------------------------------------------------ */
188     /*
189      * @see org.eclipse.io.EndPoint#getRemoteHost()
190      */
191     @Override
192     public String getRemoteHost()
193     {
194         if (_remote==null)
195             return null;
196         return _remote.getAddress().getCanonicalHostName();
197     }
198 
199     /* ------------------------------------------------------------ */
200     /*
201      * @see org.eclipse.io.EndPoint#getRemotePort()
202      */
203     @Override
204     public int getRemotePort()
205     {
206         if (_remote==null)
207             return -1;
208         return _remote.getPort();
209     }
210 
211     /* ------------------------------------------------------------ */
212     /*
213      * @see org.eclipse.io.EndPoint#getConnection()
214      */
215     @Override
216     public Object getTransport()
217     {
218         return _socket;
219     }
220 
221     /* ------------------------------------------------------------ */
222     /**
223      * @see org.eclipse.jetty.io.bio.StreamEndPoint#setMaxIdleTime(int)
224      */
225     @Override
226     public void setMaxIdleTime(int timeMs) throws IOException
227     {
228         if (timeMs!=getMaxIdleTime())
229             _socket.setSoTimeout(timeMs>0?timeMs:0);
230         super.setMaxIdleTime(timeMs);
231     }
232 
233 
234     /* ------------------------------------------------------------ */
235     @Override
236     protected void idleExpired() throws IOException
237     {
238         try
239         {
240             if (!isInputShutdown())
241                 shutdownInput();
242         }
243         catch(IOException e)
244         {
245             LOG.ignore(e);
246             _socket.close();
247         }
248     }
249 
250 }