ServerSocketFactory.java revision 6b811c5daec1b28e6f63b57f98a032236f2c3cf7
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package javax.net; 19 20import java.io.IOException; 21import java.net.InetAddress; 22import java.net.ServerSocket; 23import java.net.SocketException; 24 25/** 26 * This abstract class defines methods to create server sockets. It can be 27 * subclassed to create specific server socket types. 28 */ 29public abstract class ServerSocketFactory { 30 private static ServerSocketFactory defaultFactory; 31 32 /** 33 * Gets the default server socket factory of the system which can be used to 34 * create new server sockets without creating a subclass of this factory. 35 * 36 * @return the system default server socket factory. 37 */ 38 public static synchronized ServerSocketFactory getDefault() { 39 if (defaultFactory == null) { 40 defaultFactory = new DefaultServerSocketFactory(); 41 } 42 return defaultFactory; 43 } 44 45 /** 46 * Creates a new {@code ServerSocketFactory} instance. 47 */ 48 protected ServerSocketFactory() { 49 super(); 50 } 51 52 /** 53 * Creates a new server socket which is not bound to any local address. This 54 * method has to be overridden by a subclass otherwise a {@code 55 * SocketException} is thrown. 56 * 57 * @return the created unbound server socket. 58 * @throws IOException 59 * if an error occurs while creating a new server socket. 60 */ 61 public ServerSocket createServerSocket() throws IOException { 62 // follow RI's behavior 63 throw new SocketException("Unbound server sockets not implemented"); 64 } 65 66 /** 67 * Creates a new server socket which is bound to the given port. 68 * 69 * @param port 70 * the port on which the created socket has to listen. 71 * @return the created bound server socket. 72 * @throws IOException 73 * if an error occurs while creating a new server socket. 74 */ 75 public abstract ServerSocket createServerSocket(int port) throws IOException; 76 77 /** 78 * Creates a new server socket which is bound to the given port and 79 * configures its maximum of queued connections. 80 * 81 * @param port 82 * the port on which the created socket has to listen. 83 * @param backlog 84 * the maximum of queued connections. 85 * @return the created bound server socket. 86 * @throws IOException 87 * if an error occurs while creating a new server socket. 88 */ 89 public abstract ServerSocket createServerSocket(int port, int backlog) throws IOException; 90 91 /** 92 * Creates a new server socket which is bound to the given address on the 93 * specified port and configures its maximum of queued connections. 94 * 95 * @param port 96 * the port on which the created socket has to listen. 97 * @param backlog 98 * the maximum of queued connections. 99 * @param iAddress 100 * the address of the network interface which is used by the 101 * created socket. 102 * @return the created bound server socket. 103 * @throws IOException 104 * if an error occurs while creating a new server socket. 105 */ 106 public abstract ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress) 107 throws IOException; 108 109} 110