KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > net > BaseEndpoint


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.net.InetAddress JavaDoc;
21 import java.util.concurrent.Executor JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.tomcat.util.res.StringManager;
26
27 /**
28  * APR tailored thread pool, providing the following services:
29  * <ul>
30  * <li>Socket acceptor thread</li>
31  * <li>Socket poller thread</li>
32  * <li>Sendfile thread</li>
33  * <li>Worker threads pool</li>
34  * </ul>
35  *
36  * When switching to Java 5, there's an opportunity to use the virtual
37  * machine's thread pool.
38  *
39  * @author Mladen Turk
40  * @author Remy Maucherat
41  */

42 public abstract class BaseEndpoint {
43
44
45     // -------------------------------------------------------------- Constants
46

47
48     protected static Log log = LogFactory.getLog(BaseEndpoint.class);
49
50     protected static StringManager sm =
51         StringManager.getManager("org.apache.tomcat.util.net.res");
52
53
54     /**
55      * The Request attribute key for the cipher suite.
56      */

57     public static final String JavaDoc CIPHER_SUITE_KEY = "javax.servlet.request.cipher_suite";
58
59     /**
60      * The Request attribute key for the key size.
61      */

62     public static final String JavaDoc KEY_SIZE_KEY = "javax.servlet.request.key_size";
63
64     /**
65      * The Request attribute key for the client certificate chain.
66      */

67     public static final String JavaDoc CERTIFICATE_KEY = "javax.servlet.request.X509Certificate";
68
69     /**
70      * The Request attribute key for the session id.
71      * This one is a Tomcat extension to the Servlet spec.
72      */

73     public static final String JavaDoc SESSION_ID_KEY = "javax.servlet.request.ssl_session";
74
75
76     // ----------------------------------------------------------------- Fields
77

78
79     /**
80      * Running state of the endpoint.
81      */

82     protected volatile boolean running = false;
83
84
85     /**
86      * Will be set to true whenever the endpoint is paused.
87      */

88     protected volatile boolean paused = false;
89
90
91     /**
92      * Track the initialization state of the endpoint.
93      */

94     protected boolean initialized = false;
95
96
97     /**
98      * Current worker threads busy count.
99      */

100     protected int curThreadsBusy = 0;
101
102
103     /**
104      * Current worker threads count.
105      */

106     protected int curThreads = 0;
107
108
109     /**
110      * Sequence number used to generate thread names.
111      */

112     protected int sequence = 0;
113
114
115     // ------------------------------------------------------------- Properties
116

117
118     /**
119      * External Executor based thread pool.
120      */

121     protected Executor JavaDoc executor = null;
122     public void setExecutor(Executor JavaDoc executor) { this.executor = executor; }
123     public Executor JavaDoc getExecutor() { return executor; }
124
125
126     /**
127      * Maximum amount of worker threads.
128      */

129     protected int maxThreads = 40;
130     public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; }
131     public int getMaxThreads() { return maxThreads; }
132
133
134     /**
135      * Priority of the acceptor and poller threads.
136      */

137     protected int threadPriority = Thread.NORM_PRIORITY;
138     public void setThreadPriority(int threadPriority) { this.threadPriority = threadPriority; }
139     public int getThreadPriority() { return threadPriority; }
140
141
142     /**
143      * Server socket port.
144      */

145     protected int port;
146     public int getPort() { return port; }
147     public void setPort(int port ) { this.port=port; }
148
149
150     /**
151      * Address for the server socket.
152      */

153     protected InetAddress JavaDoc address;
154     public InetAddress JavaDoc getAddress() { return address; }
155     public void setAddress(InetAddress JavaDoc address) { this.address = address; }
156
157
158     /**
159      * Allows the server developer to specify the backlog that
160      * should be used for server sockets. By default, this value
161      * is 100.
162      */

163     protected int backlog = 100;
164     public void setBacklog(int backlog) { if (backlog > 0) this.backlog = backlog; }
165     public int getBacklog() { return backlog; }
166
167
168     /**
169      * Socket TCP no delay.
170      */

171     protected boolean tcpNoDelay = false;
172     public boolean getTcpNoDelay() { return tcpNoDelay; }
173     public void setTcpNoDelay(boolean tcpNoDelay) { this.tcpNoDelay = tcpNoDelay; }
174
175
176     /**
177      * Socket linger.
178      */

179     protected int soLinger = 100;
180     public int getSoLinger() { return soLinger; }
181     public void setSoLinger(int soLinger) { this.soLinger = soLinger; }
182
183
184     /**
185      * Socket timeout.
186      */

187     protected int soTimeout = -1;
188     public int getSoTimeout() { return soTimeout; }
189     public void setSoTimeout(int soTimeout) { this.soTimeout = soTimeout; }
190
191
192     /**
193      * The default is true - the created threads will be
194      * in daemon mode. If set to false, the control thread
195      * will not be daemon - and will keep the process alive.
196      */

197     protected boolean daemon = true;
198     public void setDaemon(boolean b) { daemon = b; }
199     public boolean getDaemon() { return daemon; }
200
201
202     /**
203      * Name of the thread pool, which will be used for naming child threads.
204      */

205     protected String JavaDoc name = "TP";
206     public void setName(String JavaDoc name) { this.name = name; }
207     public String JavaDoc getName() { return name; }
208
209
210     /**
211      * Dummy maxSpareThreads property.
212      */

213     public int getMaxSpareThreads() { return 0; }
214
215
216     /**
217      * Dummy minSpareThreads property.
218      */

219     public int getMinSpareThreads() { return 0; }
220
221
222     // --------------------------------------------------------- Public Methods
223

224
225     /**
226      * Return the amount of threads that are managed by the pool.
227      *
228      * @return the amount of threads that are managed by the pool
229      */

230     public int getCurrentThreadCount() {
231         return curThreads;
232     }
233
234
235     /**
236      * Return the amount of threads currently busy.
237      *
238      * @return the amount of threads currently busy
239      */

240     public int getCurrentThreadsBusy() {
241         return curThreadsBusy;
242     }
243
244
245     /**
246      * Return the state of the endpoint.
247      *
248      * @return true if the endpoint is running, false otherwise
249      */

250     public boolean isRunning() {
251         return running;
252     }
253
254
255     /**
256      * Return the state of the endpoint.
257      *
258      * @return true if the endpoint is paused, false otherwise
259      */

260     public boolean isPaused() {
261         return paused;
262     }
263
264
265     // ----------------------------------------------- Public Lifecycle Methods
266

267
268     /**
269      * Initialize the endpoint.
270      */

271     public abstract void init()
272         throws Exception JavaDoc;
273
274
275     /**
276      * Start the APR endpoint, creating acceptor, poller and sendfile threads.
277      */

278     public abstract void start()
279         throws Exception JavaDoc;
280
281
282     /**
283      * Pause the endpoint, which will make it stop accepting new sockets.
284      */

285     public void pause() {
286         if (running && !paused) {
287             paused = true;
288             unlockAccept();
289         }
290     }
291
292
293     /**
294      * Resume the endpoint, which will make it start accepting new sockets
295      * again.
296      */

297     public void resume() {
298         if (running) {
299             paused = false;
300         }
301     }
302
303
304     /**
305      * Stop the endpoint. This will cause all processing threads to stop.
306      */

307     public abstract void stop();
308
309
310     /**
311      * Deallocate APR memory pools, and close server socket.
312      */

313     public abstract void destroy() throws Exception JavaDoc;
314
315
316     // ------------------------------------------------------ Protected Methods
317

318
319     /**
320      * Get a sequence number used for thread naming.
321      */

322     protected int getSequence() {
323         return sequence++;
324     }
325
326
327     /**
328      * Unlock the server socket accept using a bugus connection.
329      */

330     protected void unlockAccept() {
331         java.net.Socket JavaDoc s = null;
332         try {
333             // Need to create a connection to unlock the accept();
334
if (address == null) {
335                 s = new java.net.Socket JavaDoc("127.0.0.1", port);
336             } else {
337                 s = new java.net.Socket JavaDoc(address, port);
338                 // setting soLinger to a small value will help shutdown the
339
// connection quicker
340
s.setSoLinger(true, 0);
341             }
342         } catch(Exception JavaDoc e) {
343             if (log.isDebugEnabled()) {
344                 log.debug(sm.getString("endpoint.debug.unlock", "" + port), e);
345             }
346         } finally {
347             if (s != null) {
348                 try {
349                     s.close();
350                 } catch (Exception JavaDoc e) {
351                     // Ignore
352
}
353             }
354         }
355     }
356
357
358 }
359
Popular Tags