ServerSocketFactory.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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
18/**
19* @author Boris V. Kuznetsov
20* @version $Revision$
21*/
22
23package javax.net;
24
25import java.io.IOException;
26import java.net.InetAddress;
27import java.net.ServerSocket;
28import java.net.SocketException;
29
30/**
31 * This abstract class defines methods to create server sockets. It can be
32 * subclassed to create specific server socket types.
33 *
34 * @since Android 1.0
35 */
36public abstract class ServerSocketFactory {
37    static ServerSocketFactory defaultFactory;
38
39    /**
40     * Creates a new {@code ServerSocketFactory} instance.
41     *
42     * @since Android 1.0
43     */
44    protected ServerSocketFactory() {
45    }
46
47    /**
48     * Gets the default server socket factory of the system which can be used to
49     * create new server sockets without creating a subclass of this factory.
50     *
51     * @return the system default server socket factory.
52     * @since Android 1.0
53     */
54    public static synchronized ServerSocketFactory getDefault() {
55        if (defaultFactory == null) {
56                defaultFactory = new DefaultServerSocketFactory();
57        }
58        return defaultFactory;
59    }
60
61    /**
62     * Creates a new server socket which is not bound to any local address. This
63     * method has to be overridden by a subclass otherwise a {@code
64     * SocketException} is thrown.
65     *
66     * @return the created unbound server socket.
67     * @throws IOException
68     *             if an error occurs while creating a new server socket.
69     * @since Android 1.0
70     */
71    public ServerSocket createServerSocket() throws IOException {
72        // follow RI's behavior
73        throw new SocketException("Unbound server sockets not implemented");
74    }
75
76    /**
77     * Creates a new server socket which is bound to the given port.
78     *
79     * @param port
80     *            the port on which the created socket has to listen.
81     * @return the created bound server socket.
82     * @throws IOException
83     *             if an error occurs while creating a new server socket.
84     * @since Android 1.0
85     */
86    public abstract ServerSocket createServerSocket(int port)
87            throws IOException;
88
89    /**
90     * Creates a new server socket which is bound to the given port and
91     * configures its maximum of queued connections.
92     *
93     * @param port
94     *            the port on which the created socket has to listen.
95     * @param backlog
96     *            the maximum of queued connections.
97     * @return the created bound server socket.
98     * @throws IOException
99     *             if an error occurs while creating a new server socket.
100     * @since Android 1.0
101     */
102    public abstract ServerSocket createServerSocket(int port, int backlog)
103            throws IOException;
104
105    /**
106     * Creates a new server socket which is bound to the given address on the
107     * specified port and configures its maximum of queued connections.
108     *
109     * @param port
110     *            the port on which the created socket has to listen.
111     * @param backlog
112     *            the maximum of queued connections.
113     * @param iAddress
114     *            the address of the network interface which is used by the
115     *            created socket.
116     * @return the created bound server socket.
117     * @throws IOException
118     *             if an error occurs while creating a new server socket.
119     * @since Android 1.0
120     */
121    public abstract ServerSocket createServerSocket(int port, int backlog,
122            InetAddress iAddress) throws IOException;
123
124}