001: //========================================================================
002: //$Id: SocketEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.io.bio;
017:
018: import java.io.IOException;
019: import java.net.InetAddress;
020: import java.net.InetSocketAddress;
021: import java.net.Socket;
022:
023: import org.mortbay.io.Portable;
024:
025: /**
026: * @author gregw
027: *
028: * To change the template for this generated type comment go to
029: * Window - Preferences - Java - Code Generation - Code and Comments
030: */
031: public class SocketEndPoint extends StreamEndPoint {
032: Socket _socket;
033: InetSocketAddress _local;
034: InetSocketAddress _remote;
035:
036: /**
037: *
038: */
039: public SocketEndPoint(Socket socket) throws IOException {
040: super (socket.getInputStream(), socket.getOutputStream());
041: _socket = socket;
042: }
043:
044: /* (non-Javadoc)
045: * @see org.mortbay.io.BufferIO#isClosed()
046: */
047: public boolean isClosed() {
048: return _socket == null || _socket.isClosed()
049: || _socket.isInputShutdown()
050: || _socket.isOutputShutdown();
051: }
052:
053: /* (non-Javadoc)
054: * @see org.mortbay.io.BufferIO#close()
055: */
056: public void close() throws IOException {
057: _socket.close();
058: _in = null;
059: _out = null;
060: }
061:
062: /* ------------------------------------------------------------ */
063: /*
064: * @see org.mortbay.io.EndPoint#getLocalAddr()
065: */
066: public String getLocalAddr() {
067: if (_local == null)
068: _local = (InetSocketAddress) _socket
069: .getLocalSocketAddress();
070:
071: if (_local == null || _local.getAddress() == null
072: || _local.getAddress().isAnyLocalAddress())
073: return Portable.ALL_INTERFACES;
074:
075: return _local.getAddress().getHostAddress();
076: }
077:
078: /* ------------------------------------------------------------ */
079: /*
080: * @see org.mortbay.io.EndPoint#getLocalHost()
081: */
082: public String getLocalHost() {
083: if (_local == null)
084: _local = (InetSocketAddress) _socket
085: .getLocalSocketAddress();
086:
087: if (_local == null || _local.getAddress() == null
088: || _local.getAddress().isAnyLocalAddress())
089: return Portable.ALL_INTERFACES;
090:
091: return _local.getAddress().getCanonicalHostName();
092: }
093:
094: /* ------------------------------------------------------------ */
095: /*
096: * @see org.mortbay.io.EndPoint#getLocalPort()
097: */
098: public int getLocalPort() {
099: if (_local == null)
100: _local = (InetSocketAddress) _socket
101: .getLocalSocketAddress();
102: return _local.getPort();
103: }
104:
105: /* ------------------------------------------------------------ */
106: /*
107: * @see org.mortbay.io.EndPoint#getRemoteAddr()
108: */
109: public String getRemoteAddr() {
110: if (_remote == null)
111: _remote = (InetSocketAddress) _socket
112: .getRemoteSocketAddress();
113:
114: InetAddress addr = _remote.getAddress();
115: return (addr == null ? null : addr.getHostAddress());
116: }
117:
118: /* ------------------------------------------------------------ */
119: /*
120: * @see org.mortbay.io.EndPoint#getRemoteHost()
121: */
122: public String getRemoteHost() {
123: if (_remote == null)
124: _remote = (InetSocketAddress) _socket
125: .getRemoteSocketAddress();
126:
127: return _remote.getAddress().getCanonicalHostName();
128: }
129:
130: /* ------------------------------------------------------------ */
131: /*
132: * @see org.mortbay.io.EndPoint#getRemotePort()
133: */
134: public int getRemotePort() {
135: if (_remote == null)
136: _remote = (InetSocketAddress) _socket
137: .getRemoteSocketAddress();
138: return _remote.getPort();
139: }
140:
141: /* ------------------------------------------------------------ */
142: /*
143: * @see org.mortbay.io.EndPoint#getConnection()
144: */
145: public Object getTransport() {
146: return _socket;
147: }
148: }
|