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