ServerSocketFactory.java revision 4d743816cad738bfeccf6dc210fc7c9bd84a8777
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 with a
68     * maximum backlog of 50 unaccepted connections.
69     *
70     * @param port 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 the port on which the created socket has to listen.
82     * @param backlog the maximum number of unaccepted connections. Passing 0 or
83     *     a negative value yields the default backlog of 50.
84     * @return the created bound server socket.
85     * @throws IOException if an error occurs while creating a new server socket.
86     */
87    public abstract ServerSocket createServerSocket(int port, int backlog) throws IOException;
88
89    /**
90     * Creates a new server socket which is bound to the given address on the
91     * specified port and configures its maximum of queued connections.
92     *
93     * @param port the port on which the created socket has to listen.
94     * @param backlog the maximum number of unaccepted connections. Passing 0 or
95     *     a negative value yields the default backlog of 50.
96     * @param iAddress the address of the network interface which is used by the
97     *     created socket.
98     * @return the created bound server socket.
99     * @throws IOException if an error occurs while creating a new server socket.
100     */
101    public abstract ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress)
102            throws IOException;
103
104}
105