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