001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.net;
018:
019: import java.io.IOException;
020: import java.net.InetAddress;
021: import java.net.ServerSocket;
022: import java.net.Socket;
023: import java.util.Hashtable;
024:
025: /**
026: * This class creates server sockets. It may be subclassed by other
027: * factories, which create particular types of server sockets. This
028: * provides a general framework for the addition of public socket-level
029: * functionality. It it is the server side analogue of a socket factory,
030: * and similarly provides a way to capture a variety of policies related
031: * to the sockets being constructed.
032: *
033: * <P> Like socket factories, Server Socket factory instances have two
034: * categories of methods. First are methods used to create sockets.
035: * Second are methods which set properties used in the production of
036: * sockets, such as networking options. There is also an environment
037: * specific default server socket factory; frameworks will often use
038: * their own customized factory.
039: *
040: * <P><hr><em> It may be desirable to move this interface into the
041: * <b>java.net</b> package, so that is not an extension but the preferred
042: * interface. Should this be serializable, making it a JavaBean which can
043: * be saved along with its networking configuration?
044: * </em>
045: *
046: * @author db@eng.sun.com
047: * @author Harish Prabandham
048: */
049: public abstract class ServerSocketFactory implements Cloneable {
050:
051: //
052: // NOTE: JDK 1.1 bug in class GC, this can get collected
053: // even though it's always accessible via getDefault().
054: //
055:
056: private static ServerSocketFactory theFactory;
057: protected Hashtable attributes = new Hashtable();
058:
059: /**
060: * Constructor is used only by subclasses.
061: */
062:
063: protected ServerSocketFactory() {
064: /* NOTHING */
065: }
066:
067: /** General mechanism to pass attributes from the
068: * ServerConnector to the socket factory.
069: *
070: * Note that the "prefered" mechanism is to
071: * use bean setters and explicit methods, but
072: * this allows easy configuration via server.xml
073: * or simple Properties
074: */
075: public void setAttribute(String name, Object value) {
076: if (name != null && value != null)
077: attributes.put(name, value);
078: }
079:
080: /**
081: * Returns a copy of the environment's default socket factory.
082: */
083: public static synchronized ServerSocketFactory getDefault() {
084: //
085: // optimize typical case: no synch needed
086: //
087:
088: if (theFactory == null) {
089: //
090: // Different implementations of this method could
091: // work rather differently. For example, driving
092: // this from a system property, or using a different
093: // implementation than JavaSoft's.
094: //
095:
096: theFactory = new DefaultServerSocketFactory();
097: }
098:
099: try {
100: return (ServerSocketFactory) theFactory.clone();
101: } catch (CloneNotSupportedException e) {
102: throw new RuntimeException(e.getMessage());
103: }
104: }
105:
106: /**
107: * Returns a server socket which uses all network interfaces on
108: * the host, and is bound to a the specified port. The socket is
109: * configured with the socket options (such as accept timeout)
110: * given to this factory.
111: *
112: * @param port the port to listen to
113: * @exception IOException for networking errors
114: * @exception InstantiationException for construction errors
115: */
116: public abstract ServerSocket createSocket(int port)
117: throws IOException, InstantiationException;
118:
119: /**
120: * Returns a server socket which uses all network interfaces on
121: * the host, is bound to a the specified port, and uses the
122: * specified connection backlog. The socket is configured with
123: * the socket options (such as accept timeout) given to this factory.
124: *
125: * @param port the port to listen to
126: * @param backlog how many connections are queued
127: * @exception IOException for networking errors
128: * @exception InstantiationException for construction errors
129: */
130:
131: public abstract ServerSocket createSocket(int port, int backlog)
132: throws IOException, InstantiationException;
133:
134: /**
135: * Returns a server socket which uses only the specified network
136: * interface on the local host, is bound to a the specified port,
137: * and uses the specified connection backlog. The socket is configured
138: * with the socket options (such as accept timeout) given to this factory.
139: *
140: * @param port the port to listen to
141: * @param backlog how many connections are queued
142: * @param ifAddress the network interface address to use
143: * @exception IOException for networking errors
144: * @exception InstantiationException for construction errors
145: */
146:
147: public abstract ServerSocket createSocket(int port, int backlog,
148: InetAddress ifAddress) throws IOException,
149: InstantiationException;
150:
151: public void initSocket(Socket s) {
152: }
153:
154: /**
155: Wrapper function for accept(). This allows us to trap and
156: translate exceptions if necessary
157:
158: @exception IOException;
159: */
160: public abstract Socket acceptSocket(ServerSocket socket)
161: throws IOException;
162:
163: /**
164: Extra function to initiate the handshake. Sometimes necessary
165: for SSL
166:
167: @exception IOException;
168: */
169: public abstract void handshake(Socket sock) throws IOException;
170: }
|