Path: blob/master/src/java.base/share/classes/javax/net/SocketFactory.java
41152 views
/*1* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package javax.net;2728import java.io.IOException;29import java.net.InetAddress;30import java.net.Socket;31import java.net.SocketException;32import java.net.UnknownHostException;3334/**35* This class creates sockets. It may be subclassed by other factories,36* which create particular subclasses of sockets and thus provide a general37* framework for the addition of public socket-level functionality.38*39* <P> Socket factories are a simple way to capture a variety of policies40* related to the sockets being constructed, producing such sockets in41* a way which does not require special configuration of the code which42* asks for the sockets: <UL>43*44* <LI> Due to polymorphism of both factories and sockets, different45* kinds of sockets can be used by the same application code just46* by passing it different kinds of factories.47*48* <LI> Factories can themselves be customized with parameters used49* in socket construction. So for example, factories could be50* customized to return sockets with different networking timeouts51* or security parameters already configured.52*53* <LI> The sockets returned to the application can be subclasses54* of java.net.Socket, so that they can directly expose new APIs55* for features such as compression, security, record marking,56* statistics collection, or firewall tunneling.57*58* </UL>59*60* <P> Factory classes are specified by environment-specific configuration61* mechanisms. For example, the <em>getDefault</em> method could return62* a factory that was appropriate for a particular user or applet, and a63* framework could use a factory customized to its own purposes.64*65* @since 1.466* @see ServerSocketFactory67*68* @author David Brownell69*/70public abstract class SocketFactory71{72//73// NOTE: JDK 1.1 bug in class GC, this can get collected74// even though it's always accessible via getDefault().75//76private static SocketFactory theFactory;7778/**79* Creates a <code>SocketFactory</code>.80*/81protected SocketFactory() { /* NOTHING */ }828384/**85* Returns a copy of the environment's default socket factory.86*87* @return the default <code>SocketFactory</code>88*/89public static SocketFactory getDefault()90{91synchronized (SocketFactory.class) {92if (theFactory == null) {93//94// Different implementations of this method SHOULD95// work rather differently. For example, driving96// this from a system property, or using a different97// implementation than JavaSoft's.98//99theFactory = new DefaultSocketFactory();100}101}102103return theFactory;104}105106107/**108* Creates an unconnected socket.109*110* @return the unconnected socket111* @throws IOException if the socket cannot be created112* @see java.net.Socket#connect(java.net.SocketAddress)113* @see java.net.Socket#connect(java.net.SocketAddress, int)114* @see java.net.Socket#Socket()115*/116public Socket createSocket() throws IOException {117//118// bug 6771432:119// The Exception is used by HttpsClient to signal that120// unconnected sockets have not been implemented.121//122UnsupportedOperationException uop = new123UnsupportedOperationException();124SocketException se = new SocketException(125"Unconnected sockets not implemented");126se.initCause(uop);127throw se;128}129130131/**132* Creates a socket and connects it to the specified remote host133* at the specified remote port. This socket is configured using134* the socket options established for this factory.135* <p>136* If there is a security manager, its <code>checkConnect</code>137* method is called with the host address and <code>port</code>138* as its arguments. This could result in a SecurityException.139*140* @param host the server host name with which to connect, or141* <code>null</code> for the loopback address.142* @param port the server port143* @return the <code>Socket</code>144* @throws IOException if an I/O error occurs when creating the socket145* @throws SecurityException if a security manager exists and its146* <code>checkConnect</code> method doesn't allow the operation.147* @throws UnknownHostException if the host is not known148* @throws IllegalArgumentException if the port parameter is outside the149* specified range of valid port values, which is between 0 and150* 65535, inclusive.151* @see SecurityManager#checkConnect152* @see java.net.Socket#Socket(String, int)153*/154public abstract Socket createSocket(String host, int port)155throws IOException, UnknownHostException;156157158/**159* Creates a socket and connects it to the specified remote host160* on the specified remote port.161* The socket will also be bound to the local address and port supplied.162* This socket is configured using163* the socket options established for this factory.164* <p>165* If there is a security manager, its <code>checkConnect</code>166* method is called with the host address and <code>port</code>167* as its arguments. This could result in a SecurityException.168*169* @param host the server host name with which to connect, or170* <code>null</code> for the loopback address.171* @param port the server port172* @param localHost the local address the socket is bound to173* @param localPort the local port the socket is bound to174* @return the <code>Socket</code>175* @throws IOException if an I/O error occurs when creating the socket176* @throws SecurityException if a security manager exists and its177* <code>checkConnect</code> method doesn't allow the operation.178* @throws UnknownHostException if the host is not known179* @throws IllegalArgumentException if the port parameter or localPort180* parameter is outside the specified range of valid port values,181* which is between 0 and 65535, inclusive.182* @see SecurityManager#checkConnect183* @see java.net.Socket#Socket(String, int, java.net.InetAddress, int)184*/185public abstract Socket186createSocket(String host, int port, InetAddress localHost, int localPort)187throws IOException, UnknownHostException;188189190/**191* Creates a socket and connects it to the specified port number192* at the specified address. This socket is configured using193* the socket options established for this factory.194* <p>195* If there is a security manager, its <code>checkConnect</code>196* method is called with the host address and <code>port</code>197* as its arguments. This could result in a SecurityException.198*199* @param host the server host200* @param port the server port201* @return the <code>Socket</code>202* @throws IOException if an I/O error occurs when creating the socket203* @throws SecurityException if a security manager exists and its204* <code>checkConnect</code> method doesn't allow the operation.205* @throws IllegalArgumentException if the port parameter is outside the206* specified range of valid port values, which is between 0 and207* 65535, inclusive.208* @throws NullPointerException if <code>host</code> is null.209* @see SecurityManager#checkConnect210* @see java.net.Socket#Socket(java.net.InetAddress, int)211*/212public abstract Socket createSocket(InetAddress host, int port)213throws IOException;214215216/**217* Creates a socket and connect it to the specified remote address218* on the specified remote port. The socket will also be bound219* to the local address and port suplied. The socket is configured using220* the socket options established for this factory.221* <p>222* If there is a security manager, its <code>checkConnect</code>223* method is called with the host address and <code>port</code>224* as its arguments. This could result in a SecurityException.225*226* @param address the server network address227* @param port the server port228* @param localAddress the client network address229* @param localPort the client port230* @return the <code>Socket</code>231* @throws IOException if an I/O error occurs when creating the socket232* @throws SecurityException if a security manager exists and its233* <code>checkConnect</code> method doesn't allow the operation.234* @throws IllegalArgumentException if the port parameter or localPort235* parameter is outside the specified range of valid port values,236* which is between 0 and 65535, inclusive.237* @throws NullPointerException if <code>address</code> is null.238* @see SecurityManager#checkConnect239* @see java.net.Socket#Socket(java.net.InetAddress, int,240* java.net.InetAddress, int)241*/242public abstract Socket243createSocket(InetAddress address, int port,244InetAddress localAddress, int localPort)245throws IOException;246}247248249//250// The default factory has NO intelligence about policies like tunneling251// out through firewalls (e.g. SOCKS V4 or V5) or in through them252// (e.g. using SSL), or that some ports are reserved for use with SSL.253//254// Note that at least JDK 1.1 has a low level "plainSocketImpl" that255// knows about SOCKS V4 tunneling, so this isn't a totally bogus default.256//257// ALSO: we may want to expose this class somewhere so other folk258// can reuse it, particularly if we start to add highly useful features259// such as ability to set connect timeouts.260//261class DefaultSocketFactory extends SocketFactory {262263public Socket createSocket() {264return new Socket();265}266267public Socket createSocket(String host, int port)268throws IOException, UnknownHostException269{270return new Socket(host, port);271}272273public Socket createSocket(InetAddress address, int port)274throws IOException275{276return new Socket(address, port);277}278279public Socket createSocket(String host, int port,280InetAddress clientAddress, int clientPort)281throws IOException, UnknownHostException282{283return new Socket(host, port, clientAddress, clientPort);284}285286public Socket createSocket(InetAddress address, int port,287InetAddress clientAddress, int clientPort)288throws IOException289{290return new Socket(address, port, clientAddress, clientPort);291}292}293294295