001: /*
002: * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package java.net;
026:
027: import java.io.*;
028: import java.security.PrivilegedAction;
029:
030: /*
031: * This class PlainSocketImpl simply delegates to the appropriate real
032: * SocketImpl. We do this because PlainSocketImpl is already extended
033: * by SocksSocketImpl.
034: * <p>
035: * There are two possibilities for the real SocketImpl,
036: * TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use
037: * DualStackPlainSocketImpl on systems that have a dual stack
038: * TCP implementation. Otherwise we create an instance of
039: * TwoStacksPlainSocketImpl and delegate to it.
040: *
041: * @version 1.2, 06/11/07
042: * @author Chris Hegarty
043: */
044:
045: class PlainSocketImpl extends AbstractPlainSocketImpl {
046: private AbstractPlainSocketImpl impl;
047:
048: /* the windows version. */
049: private static float version;
050:
051: /* java.net.preferIPv4Stack */
052: private static boolean preferIPv4Stack = false;
053:
054: /* If the version supports a dual stack TCP implementation */
055: private static boolean useDualStackImpl = false;
056:
057: static {
058: java.security.AccessController
059: .doPrivileged(new PrivilegedAction<Object>() {
060: public Object run() {
061: version = 0;
062: try {
063: version = Float.parseFloat(System
064: .getProperties().getProperty(
065: "os.version"));
066: preferIPv4Stack = Boolean
067: .parseBoolean(System
068: .getProperties()
069: .getProperty(
070: "java.net.preferIPv4Stack"));
071: } catch (NumberFormatException e) {
072: assert false : e;
073: }
074: return null; // nothing to return
075: }
076: });
077:
078: // (version >= 6.0) implies Vista or greater.
079: if (version >= 6.0 && !preferIPv4Stack) {
080: useDualStackImpl = true;
081: }
082: }
083:
084: /**
085: * Constructs an empty instance.
086: */
087: PlainSocketImpl() {
088: if (useDualStackImpl) {
089: impl = new DualStackPlainSocketImpl();
090: } else {
091: impl = new TwoStacksPlainSocketImpl();
092: }
093: }
094:
095: /**
096: * Constructs an instance with the given file descriptor.
097: */
098: PlainSocketImpl(FileDescriptor fd) {
099: if (useDualStackImpl) {
100: impl = new DualStackPlainSocketImpl(fd);
101: } else {
102: impl = new TwoStacksPlainSocketImpl(fd);
103: }
104: }
105:
106: // Override methods in SocketImpl that access impl's fields.
107:
108: protected FileDescriptor getFileDescriptor() {
109: return impl.getFileDescriptor();
110: }
111:
112: protected InetAddress getInetAddress() {
113: return impl.getInetAddress();
114: }
115:
116: protected int getPort() {
117: return impl.getPort();
118: }
119:
120: protected int getLocalPort() {
121: return impl.getLocalPort();
122: }
123:
124: void setSocket(Socket soc) {
125: impl.setSocket(soc);
126: }
127:
128: Socket getSocket() {
129: return impl.getSocket();
130: }
131:
132: void setServerSocket(ServerSocket soc) {
133: impl.setServerSocket(soc);
134: }
135:
136: ServerSocket getServerSocket() {
137: return impl.getServerSocket();
138: }
139:
140: public String toString() {
141: return impl.toString();
142: }
143:
144: // Override methods in AbstractPlainSocketImpl that access impl's fields.
145:
146: protected synchronized void create(boolean stream)
147: throws IOException {
148: impl.create(stream);
149: }
150:
151: protected void connect(String host, int port)
152: throws UnknownHostException, IOException {
153: impl.connect(host, port);
154: }
155:
156: protected void connect(InetAddress address, int port)
157: throws IOException {
158: impl.connect(address, port);
159: }
160:
161: protected void connect(SocketAddress address, int timeout)
162: throws IOException {
163: impl.connect(address, timeout);
164: }
165:
166: public void setOption(int opt, Object val) throws SocketException {
167: impl.setOption(opt, val);
168: }
169:
170: public Object getOption(int opt) throws SocketException {
171: return impl.getOption(opt);
172: }
173:
174: synchronized void doConnect(InetAddress address, int port,
175: int timeout) throws IOException {
176: impl.doConnect(address, port, timeout);
177: }
178:
179: protected synchronized void bind(InetAddress address, int lport)
180: throws IOException {
181: impl.bind(address, lport);
182: }
183:
184: protected synchronized void accept(SocketImpl s) throws IOException {
185: // pass in the real impl not the wrapper.
186: ((PlainSocketImpl) s).impl.address = new InetAddress();
187: ((PlainSocketImpl) s).impl.fd = new FileDescriptor();
188: impl.accept(((PlainSocketImpl) s).impl);
189: }
190:
191: void setFileDescriptor(FileDescriptor fd) {
192: impl.setFileDescriptor(fd);
193: }
194:
195: void setAddress(InetAddress address) {
196: impl.setAddress(address);
197: }
198:
199: void setPort(int port) {
200: impl.setPort(port);
201: }
202:
203: void setLocalPort(int localPort) {
204: impl.setLocalPort(localPort);
205: }
206:
207: protected synchronized InputStream getInputStream()
208: throws IOException {
209: return impl.getInputStream();
210: }
211:
212: void setInputStream(SocketInputStream in) {
213: impl.setInputStream(in);
214: }
215:
216: protected synchronized OutputStream getOutputStream()
217: throws IOException {
218: return impl.getOutputStream();
219: }
220:
221: protected void close() throws IOException {
222: impl.close();
223: }
224:
225: void reset() throws IOException {
226: impl.reset();
227: }
228:
229: protected void shutdownInput() throws IOException {
230: impl.shutdownInput();
231: }
232:
233: protected void shutdownOutput() throws IOException {
234: impl.shutdownOutput();
235: }
236:
237: protected void sendUrgentData(int data) throws IOException {
238: impl.sendUrgentData(data);
239: }
240:
241: FileDescriptor acquireFD() {
242: return impl.acquireFD();
243: }
244:
245: void releaseFD() {
246: impl.releaseFD();
247: }
248:
249: public boolean isConnectionReset() {
250: return impl.isConnectionReset();
251: }
252:
253: public boolean isConnectionResetPending() {
254: return impl.isConnectionResetPending();
255: }
256:
257: public void setConnectionReset() {
258: impl.setConnectionReset();
259: }
260:
261: public void setConnectionResetPending() {
262: impl.setConnectionResetPending();
263: }
264:
265: public boolean isClosedOrPending() {
266: return impl.isClosedOrPending();
267: }
268:
269: public int getTimeout() {
270: return impl.getTimeout();
271: }
272:
273: // Override methods in AbstractPlainSocketImpl that need to be implemented.
274:
275: void socketCreate(boolean isServer) throws IOException {
276: impl.socketCreate(isServer);
277: }
278:
279: void socketConnect(InetAddress address, int port, int timeout)
280: throws IOException {
281: impl.socketConnect(address, port, timeout);
282: }
283:
284: void socketBind(InetAddress address, int port) throws IOException {
285: impl.socketBind(address, port);
286: }
287:
288: void socketListen(int count) throws IOException {
289: impl.socketListen(count);
290: }
291:
292: void socketAccept(SocketImpl s) throws IOException {
293: impl.socketAccept(s);
294: }
295:
296: int socketAvailable() throws IOException {
297: return impl.socketAvailable();
298: }
299:
300: void socketClose0(boolean useDeferredClose) throws IOException {
301: impl.socketClose0(useDeferredClose);
302: }
303:
304: void socketShutdown(int howto) throws IOException {
305: impl.socketShutdown(howto);
306: }
307:
308: void socketSetOption(int cmd, boolean on, Object value)
309: throws SocketException {
310: socketSetOption(cmd, on, value);
311: }
312:
313: int socketGetOption(int opt, Object iaContainerObj)
314: throws SocketException {
315: return impl.socketGetOption(opt, iaContainerObj);
316: }
317:
318: int socketGetOption1(int opt, Object iaContainerObj,
319: FileDescriptor fd) throws SocketException {
320: return impl.socketGetOption1(opt, iaContainerObj, fd);
321: }
322:
323: void socketSendUrgentData(int data) throws IOException {
324: impl.socketSendUrgentData(data);
325: }
326: }
|