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      }
45  
46      /* (non-Javadoc)
47       * @see org.eclipse.io.BufferIO#isClosed()
48       */
49      @Override
50      public boolean isOpen()
51      {
52          return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
53      }
54  
55      /* (non-Javadoc)
56       * @see org.eclipse.io.BufferIO#close()
57       */
58      @Override
59      public void close() throws IOException
60      {
61          if (!_socket.isClosed() && !_socket.isOutputShutdown())
62          {
63              try
64              {
65                  _socket.shutdownOutput();
66              }
67              catch(IOException e)
68              {
69                  Log.ignore(e);
70              }
71              catch(UnsupportedOperationException e)
72              {
73                  Log.ignore(e);
74              }
75          }
76          _socket.close();
77          _in=null;
78          _out=null;
79      }
80      
81  
82      /* ------------------------------------------------------------ */
83      /* 
84       * @see org.eclipse.io.EndPoint#getLocalAddr()
85       */
86      @Override
87      public String getLocalAddr()
88      {
89          if (_local==null)
90              _local=(InetSocketAddress)_socket.getLocalSocketAddress();
91          
92         if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
93             return StringUtil.ALL_INTERFACES;
94          
95          return _local.getAddress().getHostAddress();
96      }
97  
98      /* ------------------------------------------------------------ */
99      /* 
100      * @see org.eclipse.io.EndPoint#getLocalHost()
101      */
102     @Override
103     public String getLocalHost()
104     {
105         if (_local==null)
106             _local=(InetSocketAddress)_socket.getLocalSocketAddress();
107         
108        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
109            return StringUtil.ALL_INTERFACES;
110         
111         return _local.getAddress().getCanonicalHostName();
112     }
113 
114     /* ------------------------------------------------------------ */
115     /* 
116      * @see org.eclipse.io.EndPoint#getLocalPort()
117      */
118     @Override
119     public int getLocalPort()
120     {
121         if (_local==null)
122             _local=(InetSocketAddress)_socket.getLocalSocketAddress();
123         if (_local==null)
124             return -1;
125         return _local.getPort();
126     }
127 
128     /* ------------------------------------------------------------ */
129     /* 
130      * @see org.eclipse.io.EndPoint#getRemoteAddr()
131      */
132     @Override
133     public String getRemoteAddr()
134     {
135         if (_remote==null)
136             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
137         if (_remote==null)
138             return null;
139         InetAddress addr = _remote.getAddress();
140         return ( addr == null ? null : addr.getHostAddress() );
141     }
142 
143     /* ------------------------------------------------------------ */
144     /* 
145      * @see org.eclipse.io.EndPoint#getRemoteHost()
146      */
147     @Override
148     public String getRemoteHost()
149     {
150         if (_remote==null)
151             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
152         if (_remote==null)
153             return null;
154         return _remote.getAddress().getCanonicalHostName();
155     }
156 
157     /* ------------------------------------------------------------ */
158     /* 
159      * @see org.eclipse.io.EndPoint#getRemotePort()
160      */
161     @Override
162     public int getRemotePort()
163     {
164         if (_remote==null)
165             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
166         if (_remote==null)
167             return -1;
168         return _remote.getPort();
169     }
170 
171     /* ------------------------------------------------------------ */
172     /* 
173      * @see org.eclipse.io.EndPoint#getConnection()
174      */
175     @Override
176     public Object getTransport()
177     {
178         return _socket;
179     }
180 }