KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > net > FilteredSocket


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.net;
54
55 import java.net.*;
56 import java.io.*;
57
58 /******************************************************************************
59  * A Socket wrapper that passes all calls to an internal Socket. This class is
60  * designed for subclasses to override or hook into the behavior of a Socket
61  * instance.
62  *
63  * @author Brian S O'Neill
64  * @version
65  * <!--$$Revision:--> 5 <!-- $-->, <!--$$JustDate:--> 00/12/05 <!-- $-->
66  */

67 public class FilteredSocket extends Socket {
68     protected final Socket mSocket;
69
70     public FilteredSocket(Socket socket) throws SocketException {
71         super(new Impl(socket));
72         mSocket = socket;
73     }
74
75     // Implementation note: Java's socket API could use some improvement.
76
// For example, java.net.Socket should be an interface. This would avoid
77
// needing the ugly SocketImpl class provided instead.
78

79     private static class Impl extends SocketImpl {
80         private Socket mSocket;
81
82         public Impl(Socket socket) {
83             if (socket == null) {
84                 throw new IllegalArgumentException JavaDoc("Socket is null");
85             }
86             mSocket = socket;
87         }
88
89         public void setOption(int optId, Object JavaDoc value) throws SocketException {
90             switch (optId) {
91             case TCP_NODELAY:
92                 mSocket.setTcpNoDelay(((Boolean JavaDoc)value).booleanValue());
93                 return;
94             case SO_LINGER:
95                 if (value instanceof Boolean JavaDoc) {
96                     mSocket.setSoLinger(((Boolean JavaDoc)value).booleanValue(), 0);
97                 }
98                 else {
99                     mSocket.setSoLinger(true, ((Integer JavaDoc)value).intValue());
100                 }
101                 return;
102             case SO_TIMEOUT:
103                 mSocket.setSoTimeout(((Integer JavaDoc)value).intValue());
104                 return;
105             case SO_SNDBUF:
106                 mSocket.setSendBufferSize(((Integer JavaDoc)value).intValue());
107                 return;
108             case SO_RCVBUF:
109                 mSocket.setReceiveBufferSize(((Integer JavaDoc)value).intValue());
110                 return;
111             case SO_BINDADDR:
112             case SO_REUSEADDR:
113             case IP_MULTICAST_IF:
114             default:
115                 throw new SocketException("Invalid option: " + optId);
116             }
117         }
118
119         public Object JavaDoc getOption(int optId) throws SocketException {
120             switch (optId) {
121             case TCP_NODELAY:
122                 return mSocket.getTcpNoDelay() ? Boolean.TRUE : Boolean.FALSE;
123             case SO_BINDADDR:
124                 return mSocket.getLocalAddress();
125             case SO_LINGER:
126                 return new Integer JavaDoc(mSocket.getSoLinger());
127             case SO_TIMEOUT:
128                 return new Integer JavaDoc(mSocket.getSoTimeout());
129             case SO_SNDBUF:
130                 return new Integer JavaDoc(mSocket.getSendBufferSize());
131             case SO_RCVBUF:
132                 return new Integer JavaDoc(mSocket.getReceiveBufferSize());
133             case SO_REUSEADDR:
134             case IP_MULTICAST_IF:
135             default:
136                 throw new SocketException("Invalid option: " + optId);
137             }
138         }
139
140         protected void create(boolean stream) throws IOException {
141             error();
142         }
143
144         protected void connect(String JavaDoc host, int port) throws IOException {
145             error();
146         }
147
148         protected void connect(InetAddress host, int port) throws IOException {
149             error();
150         }
151
152         protected void bind(InetAddress host, int port) throws IOException {
153             error();
154         }
155
156         protected void listen(int backlog) throws IOException {
157             error();
158         }
159
160         protected void accept(SocketImpl s) throws IOException {
161             error();
162         }
163
164         protected InputStream getInputStream() throws IOException {
165             return mSocket.getInputStream();
166         }
167
168         protected OutputStream getOutputStream() throws IOException {
169             return mSocket.getOutputStream();
170         }
171
172         protected int available() throws IOException {
173             return getInputStream().available();
174         }
175
176         protected void close() throws IOException {
177             mSocket.close();
178         }
179
180         protected InetAddress getInetAddress() {
181             return mSocket.getInetAddress();
182         }
183
184         protected int getPort() {
185             return mSocket.getPort();
186         }
187
188         protected int getLocalPort() {
189             return mSocket.getLocalPort();
190         }
191
192         private void error() throws IOException {
193             throw new IOException("Socket already connected");
194         }
195     }
196 }
197
Popular Tags