comparison src/com/go/trove/net/FilteredSocket.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
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: 1.1 $-->, <!--$$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("Socket is null");
85 }
86 mSocket = socket;
87 }
88
89 public void setOption(int optId, Object value) throws SocketException {
90 switch (optId) {
91 case TCP_NODELAY:
92 mSocket.setTcpNoDelay(((Boolean)value).booleanValue());
93 return;
94 case SO_LINGER:
95 if (value instanceof Boolean) {
96 mSocket.setSoLinger(((Boolean)value).booleanValue(), 0);
97 }
98 else {
99 mSocket.setSoLinger(true, ((Integer)value).intValue());
100 }
101 return;
102 case SO_TIMEOUT:
103 mSocket.setSoTimeout(((Integer)value).intValue());
104 return;
105 case SO_SNDBUF:
106 mSocket.setSendBufferSize(((Integer)value).intValue());
107 return;
108 case SO_RCVBUF:
109 mSocket.setReceiveBufferSize(((Integer)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 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(mSocket.getSoLinger());
127 case SO_TIMEOUT:
128 return new Integer(mSocket.getSoTimeout());
129 case SO_SNDBUF:
130 return new Integer(mSocket.getSendBufferSize());
131 case SO_RCVBUF:
132 return new Integer(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 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 connect(SocketAddress host,
153 int port) throws IOException {
154 error();
155 }
156
157 protected void bind(InetAddress host, int port) throws IOException {
158 error();
159 }
160
161 protected void listen(int backlog) throws IOException {
162 error();
163 }
164
165 protected void accept(SocketImpl s) throws IOException {
166 error();
167 }
168
169 protected InputStream getInputStream() throws IOException {
170 return mSocket.getInputStream();
171 }
172
173 protected OutputStream getOutputStream() throws IOException {
174 return mSocket.getOutputStream();
175 }
176
177 protected int available() throws IOException {
178 return getInputStream().available();
179 }
180
181 protected void close() throws IOException {
182 mSocket.close();
183 }
184
185 protected InetAddress getInetAddress() {
186 return mSocket.getInetAddress();
187 }
188
189 protected int getPort() {
190 return mSocket.getPort();
191 }
192
193 protected int getLocalPort() {
194 return mSocket.getLocalPort();
195 }
196
197 private void error() throws IOException {
198 throw new IOException("Socket already connected");
199 }
200
201 protected void sendUrgentData(int aByte) throws IOException {
202 throw new UnsupportedOperationException();
203 }
204 }
205 }