# Coverage details for com.martiansoftware.nailgun.NGSessionPool

Line | Hits | Source
--- | --- | ---
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 | 1 | `int poolSize = 0;`
36 |  | ``
37 |  | `/**`
38 |  | `* the pool itself`
39 |  | `*/`
40 | 1 | `NGSession[] pool = null;`
41 |  | ``
42 |  | `/**`
43 |  | `* The number of sessions currently in the pool`
44 |  | `*/`
45 | 1 | `int poolEntries = 0;`
46 |  | ``
47 |  | `/**`
48 |  | `* reference to server we're working for`
49 |  | `*/`
50 | 1 | `NGServer server = null;`
51 |  | ``
52 |  | `/**`
53 |  | `* have we been shut down?`
54 |  | `*/`
55 | 1 | `boolean done = false;`
56 |  | ``
57 |  | `/**`
58 |  | `* synchronization object`
59 |  | `*/`
60 | 1 | `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 |  | `*/`
68 | 1 | `NGSessionPool(NGServer server, int maxIdle) {`
69 | 1 | `this.server = server;`
70 | 1 | `this.poolSize = maxIdle - 1;`
71 |  | ``
72 | 1 | `pool = new NGSession[poolSize];`
73 | 1 | `poolEntries = 0;`
74 | 1 | `}`
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 | 0 | `synchronized(lock) {`
83 | 0 | `if (poolEntries == 0) {`
84 | 0 | `result = new NGSession(this, server);`
85 | 0 | `result.start();`
86 |  | `} else {`
87 | 0 | `--poolEntries;`
88 | 0 | `result = pool[poolEntries];`
89 |  | `}`
90 | 0 | `}`
91 | 0 | `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 | 0 | `if (done || poolEntries == poolSize) {`
101 | 0 | `session.shutdown();`
102 |  | `} else {`
103 | 0 | `synchronized(lock) {`
104 | 0 | `pool[poolEntries] = session;`
105 | 0 | `++poolEntries;`
106 | 0 | `}`
107 |  | `}`
108 | 0 | `}`
109 |  | ``
110 |  | `/**`
111 |  | `* Shuts down the pool. Running nails are allowed to finish.`
112 |  | `*/`
113 |  | `void shutdown() {`
114 | 0 | `done = true;`
115 | 0 | `synchronized(lock) {`
116 | 0 | `while (poolEntries > 0) {`
117 | 0 | `take().shutdown();`
118 |  | `}`
119 | 0 | `}`
120 | 0 | `}`
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.