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