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