001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.net;
027
028 import java.io.IOException;
029 import java.net.InetAddress;
030 import java.net.ServerSocket;
031 import java.net.SocketException;
032
033 /**
034 * This class creates server sockets. It may be subclassed by other
035 * factories, which create particular types of server sockets. This
036 * provides a general framework for the addition of public socket-level
037 * functionality. It is the server side analogue of a socket factory,
038 * and similarly provides a way to capture a variety of policies related
039 * to the sockets being constructed.
040 *
041 * <P> Like socket factories, server Socket factory instances have
042 * methods used to create sockets. There is also an environment
043 * specific default server socket factory; frameworks will often use
044 * their own customized factory.
045 *
046 * @since 1.4
047 * @see SocketFactory
048 *
049 * @version 1.27
050 * @author David Brownell
051 */
052 public abstract class ServerSocketFactory {
053 //
054 // NOTE: JDK 1.1 bug in class GC, this can get collected
055 // even though it's always accessible via getDefault().
056 //
057 private static ServerSocketFactory theFactory;
058
059 /**
060 * Creates a server socket factory.
061 */
062 protected ServerSocketFactory() { /* NOTHING */
063 }
064
065 /**
066 * Returns a copy of the environment's default socket factory.
067 *
068 * @return the <code>ServerSocketFactory</code>
069 */
070 public static ServerSocketFactory getDefault() {
071 synchronized (ServerSocketFactory.class) {
072 if (theFactory == null) {
073 //
074 // Different implementations of this method could
075 // work rather differently. For example, driving
076 // this from a system property, or using a different
077 // implementation than JavaSoft's.
078 //
079 theFactory = new DefaultServerSocketFactory();
080 }
081 }
082
083 return theFactory;
084 }
085
086 /**
087 * Returns an unbound server socket. The socket is configured with
088 * the socket options (such as accept timeout) given to this factory.
089 *
090 * @return the unbound socket
091 * @throws IOException if the socket cannot be created
092 * @see java.net.ServerSocket#bind(java.net.SocketAddress)
093 * @see java.net.ServerSocket#bind(java.net.SocketAddress, int)
094 * @see java.net.ServerSocket#ServerSocket()
095 */
096 public ServerSocket createServerSocket() throws IOException {
097 throw new SocketException(
098 "Unbound server sockets not implemented");
099 }
100
101 /**
102 * Returns a server socket bound to the specified port.
103 * The socket is configured with the socket options
104 * (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 its
108 * argument to ensure the operation is allowed. This could result
109 * in a SecurityException.
110 *
111 * @param port the port to listen to
112 * @return the <code>ServerSocket</code>
113 * @throws IOException for networking errors
114 * @throws SecurityException if a security manager exists and its
115 * <code>checkListen</code> method doesn't allow the operation.
116 * @throws IllegalArgumentException if the port parameter is outside the
117 * specified range of valid port values, which is between 0 and
118 * 65535, inclusive.
119 * @see SecurityManager#checkListen
120 * @see java.net.ServerSocket#ServerSocket(int)
121 */
122 public abstract ServerSocket createServerSocket(int port)
123 throws IOException;
124
125 /**
126 * Returns a server socket bound to the specified port, and uses the
127 * specified connection backlog. The socket is configured with
128 * the socket options (such as accept timeout) given to this factory.
129 * <P>
130 * The <code>backlog</code> argument must be a positive
131 * value greater than 0. If the value passed if equal or less
132 * than 0, then the default value will be assumed.
133 * <P>
134 * If there is a security manager, its <code>checkListen</code>
135 * method is called with the <code>port</code> argument as its
136 * argument to ensure the operation is allowed. This could result
137 * in a SecurityException.
138 *
139 * @param port the port to listen to
140 * @param backlog how many connections are queued
141 * @return the <code>ServerSocket</code>
142 * @throws IOException for networking errors
143 * @throws SecurityException if a security manager exists and its
144 * <code>checkListen</code> method doesn't allow the operation.
145 * @throws IllegalArgumentException if the port parameter is outside the
146 * specified range of valid port values, which is between 0 and
147 * 65535, inclusive.
148 * @see SecurityManager#checkListen
149 * @see java.net.ServerSocket#ServerSocket(int, int)
150 */
151 public abstract ServerSocket createServerSocket(int port,
152 int backlog) throws IOException;
153
154 /**
155 * Returns a server socket bound to the specified port,
156 * with a specified listen backlog and local IP.
157 * <P>
158 * The <code>ifAddress</code> argument can be used on a multi-homed
159 * host for a <code>ServerSocket</code> that will only accept connect
160 * requests to one of its addresses. If <code>ifAddress</code> is null,
161 * it will accept connections on all local addresses. The socket is
162 * configured with the socket options (such as accept timeout) given
163 * to this factory.
164 * <P>
165 * The <code>backlog</code> argument must be a positive
166 * value greater than 0. If the value passed if equal or less
167 * than 0, then the default value will be assumed.
168 * <P>
169 * If there is a security manager, its <code>checkListen</code>
170 * method is called with the <code>port</code> argument as its
171 * argument to ensure the operation is allowed. This could result
172 * in a SecurityException.
173 *
174 * @param port the port to listen to
175 * @param backlog how many connections are queued
176 * @param ifAddress the network interface address to use
177 * @return the <code>ServerSocket</code>
178 * @throws IOException for networking errors
179 * @throws SecurityException if a security manager exists and its
180 * <code>checkListen</code> method doesn't allow the operation.
181 * @throws IllegalArgumentException if the port parameter is outside the
182 * specified range of valid port values, which is between 0 and
183 * 65535, inclusive.
184 * @see SecurityManager#checkListen
185 * @see java.net.ServerSocket#ServerSocket(int, int, java.net.InetAddress)
186 */
187 public abstract ServerSocket createServerSocket(int port,
188 int backlog, InetAddress ifAddress) throws IOException;
189 }
190
191 //
192 // The default factory has NO intelligence. In fact it's not clear
193 // what sort of intelligence servers need; the onus is on clients,
194 // who have to know how to tunnel etc.
195 //
196 class DefaultServerSocketFactory extends ServerSocketFactory {
197
198 DefaultServerSocketFactory() {
199 /* NOTHING */
200 }
201
202 public ServerSocket createServerSocket() throws IOException {
203 return new ServerSocket();
204 }
205
206 public ServerSocket createServerSocket(int port) throws IOException {
207 return new ServerSocket(port);
208 }
209
210 public ServerSocket createServerSocket(int port, int backlog)
211 throws IOException {
212 return new ServerSocket(port, backlog);
213 }
214
215 public ServerSocket createServerSocket(int port, int backlog,
216 InetAddress ifAddress) throws IOException {
217 return new ServerSocket(port, backlog, ifAddress);
218 }
219 }
|