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 package org.apache.tomcat.util.net;
19
20 import java.io.IOException;
21 import java.net.InetAddress;
22 import java.net.ServerSocket;
23 import java.net.Socket;
24 import java.util.Hashtable;
25
26 /**
27 * This class creates server sockets. It may be subclassed by other
28 * factories, which create particular types of server sockets. This
29 * provides a general framework for the addition of public socket-level
30 * functionality. It it is the server side analogue of a socket factory,
31 * and similarly provides a way to capture a variety of policies related
32 * to the sockets being constructed.
33 *
34 * <P> Like socket factories, Server Socket factory instances have two
35 * categories of methods. First are methods used to create sockets.
36 * Second are methods which set properties used in the production of
37 * sockets, such as networking options. There is also an environment
38 * specific default server socket factory; frameworks will often use
39 * their own customized factory.
40 *
41 * <P><hr><em> It may be desirable to move this interface into the
42 * <b>java.net</b> package, so that is not an extension but the preferred
43 * interface. Should this be serializable, making it a JavaBean which can
44 * be saved along with its networking configuration?
45 * </em>
46 *
47 * @author db@eng.sun.com
48 * @author Harish Prabandham
49 */
50 public abstract class ServerSocketFactory implements Cloneable {
51
52 //
53 // NOTE: JDK 1.1 bug in class GC, this can get collected
54 // even though it's always accessible via getDefault().
55 //
56
57 private static ServerSocketFactory theFactory;
58 protected Hashtable attributes=new Hashtable();
59
60 /**
61 * Constructor is used only by subclasses.
62 */
63
64 protected ServerSocketFactory () {
65 /* NOTHING */
66 }
67
68 /** General mechanism to pass attributes from the
69 * ServerConnector to the socket factory.
70 *
71 * Note that the "prefered" mechanism is to
72 * use bean setters and explicit methods, but
73 * this allows easy configuration via server.xml
74 * or simple Properties
75 */
76 public void setAttribute( String name, Object value ) {
77 if( name!=null && value !=null)
78 attributes.put( name, value );
79 }
80
81 /**
82 * Returns a copy of the environment's default socket factory.
83 */
84 public static synchronized ServerSocketFactory getDefault () {
85 //
86 // optimize typical case: no synch needed
87 //
88
89 if (theFactory == null) {
90 //
91 // Different implementations of this method could
92 // work rather differently. For example, driving
93 // this from a system property, or using a different
94 // implementation than JavaSoft's.
95 //
96
97 theFactory = new DefaultServerSocketFactory ();
98 }
99
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,
149 int backlog, InetAddress ifAddress)
150 throws IOException, 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)
171 throws IOException;
172 }
173