Coverage details for com.martiansoftware.nailgun.NGSessionPool

LineHitsSource
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      */
351    int poolSize = 0;
36  
37     /**
38      * the pool itself
39      */
401    NGSession[] pool = null;
41     
42     /**
43      * The number of sessions currently in the pool
44      */
451    int poolEntries = 0;
46  
47     /**
48      * reference to server we're working for
49      */
501    NGServer server = null;
51     
52     /**
53      * have we been shut down?
54      */
551    boolean done = false;
56     
57     /**
58      * synchronization object
59      */
601    private Object lock = new Object();
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      */
681    NGSessionPool(NGServer server, int maxIdle) {
691        this.server = server;
701        this.poolSize = maxIdle - 1;
71     
721        pool = new NGSession[poolSize];
731        poolEntries = 0;
741    }
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;
820        synchronized(lock) {
830            if (poolEntries == 0) {
840                result = new NGSession(this, server);
850                result.start();
86             } else {
870                --poolEntries;
880                result = pool[poolEntries];
89             }
900        }
910        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) {
1000        if (done || poolEntries == poolSize) {
1010            session.shutdown();
102         } else {
1030            synchronized(lock) {
1040                pool[poolEntries] = session;
1050                ++poolEntries;
1060            }
107         }
1080    }
109     
110     /**
111      * Shuts down the pool. Running nails are allowed to finish.
112      */
113     void shutdown() {
1140        done = true;
1150        synchronized(lock) {
1160            while (poolEntries > 0) {
1170                take().shutdown();
118             }
1190        }
1200    }
121  
122 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.