Path: blob/master/src/java.base/share/classes/javax/net/ServerSocketFactory.java
41152 views
/*1* Copyright (c) 1997, 2007, 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*/2425package javax.net;2627import java.io.IOException;28import java.net.InetAddress;29import java.net.ServerSocket;30import java.net.SocketException;3132/**33* This class creates server sockets. It may be subclassed by other34* factories, which create particular types of server sockets. This35* provides a general framework for the addition of public socket-level36* functionality. It is the server side analogue of a socket factory,37* and similarly provides a way to capture a variety of policies related38* to the sockets being constructed.39*40* <P> Like socket factories, server Socket factory instances have41* methods used to create sockets. There is also an environment42* specific default server socket factory; frameworks will often use43* their own customized factory.44*45* @since 1.446* @see SocketFactory47*48* @author David Brownell49*/50public abstract class ServerSocketFactory51{52//53// NOTE: JDK 1.1 bug in class GC, this can get collected54// even though it's always accessible via getDefault().55//56private static ServerSocketFactory theFactory;575859/**60* Creates a server socket factory.61*/62protected ServerSocketFactory() { /* NOTHING */ }6364/**65* Returns a copy of the environment's default socket factory.66*67* @return the <code>ServerSocketFactory</code>68*/69public static ServerSocketFactory getDefault()70{71synchronized (ServerSocketFactory.class) {72if (theFactory == null) {73//74// Different implementations of this method could75// work rather differently. For example, driving76// this from a system property, or using a different77// implementation than JavaSoft's.78//79theFactory = new DefaultServerSocketFactory();80}81}8283return theFactory;84}858687/**88* Returns an unbound server socket. The socket is configured with89* the socket options (such as accept timeout) given to this factory.90*91* @return the unbound socket92* @throws IOException if the socket cannot be created93* @see java.net.ServerSocket#bind(java.net.SocketAddress)94* @see java.net.ServerSocket#bind(java.net.SocketAddress, int)95* @see java.net.ServerSocket#ServerSocket()96*/97public ServerSocket createServerSocket() throws IOException {98throw new SocketException("Unbound server sockets not implemented");99}100101/**102* Returns a server socket bound to the specified port.103* The socket is configured with the socket options104* (such as accept timeout) given to this factory.105* <P>106* If there is a security manager, its <code>checkListen</code>107* method is called with the <code>port</code> argument as its108* argument to ensure the operation is allowed. This could result109* in a SecurityException.110*111* @param port the port to listen to112* @return the <code>ServerSocket</code>113* @throws IOException for networking errors114* @throws SecurityException if a security manager exists and its115* <code>checkListen</code> method doesn't allow the operation.116* @throws IllegalArgumentException if the port parameter is outside the117* specified range of valid port values, which is between 0 and118* 65535, inclusive.119* @see SecurityManager#checkListen120* @see java.net.ServerSocket#ServerSocket(int)121*/122public abstract ServerSocket createServerSocket(int port)123throws IOException;124125126/**127* Returns a server socket bound to the specified port, and uses the128* specified connection backlog. The socket is configured with129* the socket options (such as accept timeout) given to this factory.130* <P>131* The <code>backlog</code> argument must be a positive132* value greater than 0. If the value passed if equal or less133* than 0, then the default value will be assumed.134* <P>135* If there is a security manager, its <code>checkListen</code>136* method is called with the <code>port</code> argument as its137* argument to ensure the operation is allowed. This could result138* in a SecurityException.139*140* @param port the port to listen to141* @param backlog how many connections are queued142* @return the <code>ServerSocket</code>143* @throws IOException for networking errors144* @throws SecurityException if a security manager exists and its145* <code>checkListen</code> method doesn't allow the operation.146* @throws IllegalArgumentException if the port parameter is outside the147* specified range of valid port values, which is between 0 and148* 65535, inclusive.149* @see SecurityManager#checkListen150* @see java.net.ServerSocket#ServerSocket(int, int)151*/152public abstract ServerSocket153createServerSocket(int port, int backlog)154throws IOException;155156157/**158* Returns a server socket bound to the specified port,159* with a specified listen backlog and local IP.160* <P>161* The <code>ifAddress</code> argument can be used on a multi-homed162* host for a <code>ServerSocket</code> that will only accept connect163* requests to one of its addresses. If <code>ifAddress</code> is null,164* it will accept connections on all local addresses. The socket is165* configured with the socket options (such as accept timeout) given166* to this factory.167* <P>168* The <code>backlog</code> argument must be a positive169* value greater than 0. If the value passed if equal or less170* than 0, then the default value will be assumed.171* <P>172* If there is a security manager, its <code>checkListen</code>173* method is called with the <code>port</code> argument as its174* argument to ensure the operation is allowed. This could result175* in a SecurityException.176*177* @param port the port to listen to178* @param backlog how many connections are queued179* @param ifAddress the network interface address to use180* @return the <code>ServerSocket</code>181* @throws IOException for networking errors182* @throws SecurityException if a security manager exists and its183* <code>checkListen</code> method doesn't allow the operation.184* @throws IllegalArgumentException if the port parameter is outside the185* specified range of valid port values, which is between 0 and186* 65535, inclusive.187* @see SecurityManager#checkListen188* @see java.net.ServerSocket#ServerSocket(int, int, java.net.InetAddress)189*/190public abstract ServerSocket191createServerSocket(int port, int backlog, InetAddress ifAddress)192throws IOException;193}194195196//197// The default factory has NO intelligence. In fact it's not clear198// what sort of intelligence servers need; the onus is on clients,199// who have to know how to tunnel etc.200//201class DefaultServerSocketFactory extends ServerSocketFactory {202203DefaultServerSocketFactory()204{205/* NOTHING */206}207208public ServerSocket createServerSocket()209throws IOException210{211return new ServerSocket();212}213214public ServerSocket createServerSocket(int port)215throws IOException216{217return new ServerSocket(port);218}219220public ServerSocket createServerSocket(int port, int backlog)221throws IOException222{223return new ServerSocket(port, backlog);224}225226public ServerSocket227createServerSocket(int port, int backlog, InetAddress ifAddress)228throws IOException229{230return new ServerSocket(port, backlog, ifAddress);231}232}233234235