1   //========================================================================
2   //$Id: SocketEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mortbay.io.bio;
17  
18  import java.io.IOException;
19  import java.net.InetAddress;
20  import java.net.InetSocketAddress;
21  import java.net.Socket;
22  
23  import org.mortbay.io.Portable;
24  import org.mortbay.log.Log;
25  
26  /**
27   * @author gregw
28   *
29   * To change the template for this generated type comment go to
30   * Window - Preferences - Java - Code Generation - Code and Comments
31   */
32  public class SocketEndPoint extends StreamEndPoint
33  {
34      Socket _socket;
35      InetSocketAddress _local;
36      InetSocketAddress _remote;
37  
38      /**
39       * 
40       */
41      public SocketEndPoint(Socket socket)
42      	throws IOException	
43      {
44          super(socket.getInputStream(),socket.getOutputStream());
45          _socket=socket;
46      }
47  
48      /* (non-Javadoc)
49       * @see org.mortbay.io.BufferIO#isClosed()
50       */
51      public boolean isOpen()
52      {
53          return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
54      }
55  
56      /* (non-Javadoc)
57       * @see org.mortbay.io.BufferIO#close()
58       */
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.mortbay.io.EndPoint#getLocalAddr()
85       */
86      public String getLocalAddr()
87      {
88          if (_local==null)
89              _local=(InetSocketAddress)_socket.getLocalSocketAddress();
90          
91         if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
92             return Portable.ALL_INTERFACES;
93          
94          return _local.getAddress().getHostAddress();
95      }
96  
97      /* ------------------------------------------------------------ */
98      /* 
99       * @see org.mortbay.io.EndPoint#getLocalHost()
100      */
101     public String getLocalHost()
102     {
103         if (_local==null)
104             _local=(InetSocketAddress)_socket.getLocalSocketAddress();
105         
106        if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
107            return Portable.ALL_INTERFACES;
108         
109         return _local.getAddress().getCanonicalHostName();
110     }
111 
112     /* ------------------------------------------------------------ */
113     /* 
114      * @see org.mortbay.io.EndPoint#getLocalPort()
115      */
116     public int getLocalPort()
117     {
118         if (_local==null)
119             _local=(InetSocketAddress)_socket.getLocalSocketAddress();
120         return _local.getPort();
121     }
122 
123     /* ------------------------------------------------------------ */
124     /* 
125      * @see org.mortbay.io.EndPoint#getRemoteAddr()
126      */
127     public String getRemoteAddr()
128     {
129         if (_remote==null)
130             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
131         
132         InetAddress addr = _remote.getAddress();
133         return ( addr == null ? null : addr.getHostAddress() );
134     }
135 
136     /* ------------------------------------------------------------ */
137     /* 
138      * @see org.mortbay.io.EndPoint#getRemoteHost()
139      */
140     public String getRemoteHost()
141     {
142         if (_remote==null)
143             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
144         
145         return _remote.getAddress().getCanonicalHostName();
146     }
147 
148     /* ------------------------------------------------------------ */
149     /* 
150      * @see org.mortbay.io.EndPoint#getRemotePort()
151      */
152     public int getRemotePort()
153     {
154         if (_remote==null)
155             _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
156         return _remote.getPort();
157     }
158 
159 
160     /* ------------------------------------------------------------ */
161     /* 
162      * @see org.mortbay.io.EndPoint#getConnection()
163      */
164     public Object getTransport()
165     {
166         return _socket;
167     }
168 }