View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 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  
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      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          super.setMaxIdleTime(_socket.getSoTimeout());
45      }
46      
47      /**
48       * 
49       */
50      protected SocketEndPoint(Socket socket, int maxIdleTime)
51          throws IOException      
52      {
53          super(socket.getInputStream(),socket.getOutputStream());
54          _socket=socket;
55          super.setMaxIdleTime(maxIdleTime);
56      }
57  
58      /* (non-Javadoc)
59       * @see org.eclipse.io.BufferIO#isClosed()
60       */
61      @Override
62      public boolean isOpen()
63      {
64          return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
65      }
66  
67      /* (non-Javadoc)
68       * @see org.eclipse.io.BufferIO#close()
69       */
70      @Override
71      public void close() throws IOException
72      {
73          if (!_socket.isClosed() && !_socket.isOutputShutdown())
74          {
75              try
76              {
77                  _socket.shutdownOutput();
78              }
79              catch(IOException e)
80              {
81                  Log.ignore(e);
82              }
83              catch(UnsupportedOperationException e)
84              {
85                  Log.ignore(e);
86              }
87          }
88          _socket.close();
89          _in=null;
90          _out=null;
91      }
92      
93  
94      /* ------------------------------------------------------------ */
95      /* 
96       * @see org.eclipse.io.EndPoint#getLocalAddr()
97       */
98      @Override
99      public String getLocalAddr()
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().getHostAddress();
108     }
109 
110     /* ------------------------------------------------------------ */
111     /* 
112      * @see org.eclipse.io.EndPoint#getLocalHost()
113      */
114     @Override
115     public String getLocalHost()
116     {
117         if (_local==null)
118             _local=(InetSocketAddress)_socket.getLocalSocketAddress();
119         
120        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
121            return StringUtil.ALL_INTERFACES;
122         
123         return _local.getAddress().getCanonicalHostName();
124     }
125 
126     /* ------------------------------------------------------------ */
127     /* 
128      * @see org.eclipse.io.EndPoint#getLocalPort()
129      */
130     @Override
131     public int getLocalPort()
132     {
133         if (_local==null)
134             _local=(InetSocketAddress)_socket.getLocalSocketAddress();
135         if (_local==null)
136             return -1;
137         return _local.getPort();
138     }
139 
140     /* ------------------------------------------------------------ */
141     /* 
142      * @see org.eclipse.io.EndPoint#getRemoteAddr()
143      */
144     @Override
145     public String getRemoteAddr()
146     {
147         if (_remote==null)
148             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
149         if (_remote==null)
150             return null;
151         InetAddress addr = _remote.getAddress();
152         return ( addr == null ? null : addr.getHostAddress() );
153     }
154 
155     /* ------------------------------------------------------------ */
156     /* 
157      * @see org.eclipse.io.EndPoint#getRemoteHost()
158      */
159     @Override
160     public String getRemoteHost()
161     {
162         if (_remote==null)
163             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
164         if (_remote==null)
165             return null;
166         return _remote.getAddress().getCanonicalHostName();
167     }
168 
169     /* ------------------------------------------------------------ */
170     /* 
171      * @see org.eclipse.io.EndPoint#getRemotePort()
172      */
173     @Override
174     public int getRemotePort()
175     {
176         if (_remote==null)
177             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
178         if (_remote==null)
179             return -1;
180         return _remote.getPort();
181     }
182 
183     /* ------------------------------------------------------------ */
184     /* 
185      * @see org.eclipse.io.EndPoint#getConnection()
186      */
187     @Override
188     public Object getTransport()
189     {
190         return _socket;
191     }
192 
193     /* ------------------------------------------------------------ */
194     /**
195      * @see org.eclipse.jetty.io.bio.StreamEndPoint#setMaxIdleTime(int)
196      */
197     @Override
198     public void setMaxIdleTime(int timeMs) throws IOException
199     {
200         if (timeMs!=getMaxIdleTime())
201             _socket.setSoTimeout(timeMs>0?timeMs:0);
202         super.setMaxIdleTime(timeMs);
203     }
204     
205     
206 }