KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > NGSessionPool


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   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
19 package com.martiansoftware.nailgun;
20
21 /**
22  * Provides NGSession pooling functionality. One parameter, "maxIdle",
23  * governs its behavior by setting the maximum number of idle NGSession
24  * threads it will allow. It creates a pool of size maxIdle - 1, because
25  * one NGSession is kept "on deck" by the NGServer in order to eke out
26  * a little extra responsiveness.
27  *
28  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
29  */

30 class NGSessionPool {
31
32     /**
33      * number of sessions to store in the pool
34      */

35     int poolSize = 0;
36
37     /**
38      * the pool itself
39      */

40     NGSession[] pool = null;
41     
42     /**
43      * The number of sessions currently in the pool
44      */

45     int poolEntries = 0;
46
47     /**
48      * reference to server we're working for
49      */

50     NGServer server = null;
51     
52     /**
53      * have we been shut down?
54      */

55     boolean done = false;
56     
57     /**
58      * synchronization object
59      */

60     private Object JavaDoc lock = new Object JavaDoc();
61     
62     /**
63      * Creates a new NGSessionRunner operating for the specified server, with
64      * the specified number of threads
65      * @param server the server to work for
66      * @param poolSize the maximum number of idle threads to allow
67      */

68     NGSessionPool(NGServer server, int maxIdle) {
69         this.server = server;
70         this.poolSize = maxIdle - 1;
71     
72         pool = new NGSession[poolSize];
73         poolEntries = 0;
74     }
75
76     /**
77      * Returns an NGSession from the pool, or creates one if necessary
78      * @return an NGSession ready to work
79      */

80     NGSession take() {
81         NGSession result;
82         synchronized(lock) {
83             if (poolEntries == 0) {
84                 result = new NGSession(this, server);
85                 result.start();
86             } else {
87                 --poolEntries;
88                 result = pool[poolEntries];
89             }
90         }
91         return (result);
92     }
93     
94     /**
95      * Returns an NGSession to the pool. The pool may choose to shutdown
96      * the thread if the pool is full
97      * @param session the NGSession to return to the pool
98      */

99     void give(NGSession session) {
100         if (done || poolEntries == poolSize) {
101             session.shutdown();
102         } else {
103             synchronized(lock) {
104                 pool[poolEntries] = session;
105                 ++poolEntries;
106             }
107         }
108     }
109     
110     /**
111      * Shuts down the pool. Running nails are allowed to finish.
112      */

113     void shutdown() {
114         done = true;
115         synchronized(lock) {
116             while (poolEntries > 0) {
117                 take().shutdown();
118             }
119         }
120     }
121
122 }
123
Popular Tags