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    }
50
51    /**
52     * Creates a new server socket which is not bound to any local address. This
53     * method has to be overridden by a subclass otherwise a {@code
54     * SocketException} is thrown.
55     *
56     * @return the created unbound server socket.
57     * @throws IOException
58     *             if an error occurs while creating a new server socket.
59     */
60    public ServerSocket createServerSocket() throws IOException {
61        // follow RI's behavior
62        throw new SocketException("Unbound server sockets not implemented");
63    }
64
65    /**
66     * Creates a new server socket which is bound to the given port with a
67     * maximum backlog of 50 unaccepted connections.
68     *
69     * @param port the port on which the created socket has to listen.
70     * @return the created bound server socket.
71     * @throws IOException
72     *             if an error occurs while creating a new server socket.
73     */
74    public abstract ServerSocket createServerSocket(int port) throws IOException;
75
76    /**
77     * Creates a new server socket which is bound to the given port and
78     * configures its maximum of queued connections.
79     *
80     * @param port the port on which the created socket has to listen.
81     * @param backlog the maximum number of unaccepted connections. Passing 0 or
82     *     a negative value yields the default backlog of 50.
83     * @return the created bound server socket.
84     * @throws IOException if an error occurs while creating a new server socket.
85     */
86    public abstract ServerSocket createServerSocket(int port, int backlog) throws IOException;
87
88    /**
89     * Creates a new server socket which is bound to the given address on the
90     * specified port and configures its maximum of queued connections.
91     *
92     * @param port the port on which the created socket has to listen.
93     * @param backlog the maximum number of unaccepted connections. Passing 0 or
94     *     a negative value yields the default backlog of 50.
95     * @param iAddress the address of the network interface which is used by the
96     *     created socket.
97     * @return the created bound server socket.
98     * @throws IOException if an error occurs while creating a new server socket.
99     */
100    public abstract ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress)
101            throws IOException;
102
103}
104