KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > filters > SemaphoreValve


1 /*
2  * Copyright 1999-2001,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.jboss.web.tomcat.filters;
19
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24
25 import org.apache.catalina.Lifecycle;
26 import org.apache.catalina.LifecycleException;
27 import org.apache.catalina.LifecycleListener;
28 import org.apache.catalina.connector.Request;
29 import org.apache.catalina.connector.Response;
30 import org.apache.catalina.util.LifecycleSupport;
31 import org.apache.catalina.util.StringManager;
32 import org.apache.catalina.valves.ValveBase;
33
34 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore;
35 import EDU.oswego.cs.dl.util.concurrent.Sync;
36
37 /**
38  * <p>Implementation of a Valve that limits concurrency.</p>
39  *
40  * <p>This Valve may be attached to any Container, depending on the granularity
41  * of the concurrency control you wish to perform. This is a Java 1.4 port
42  * of the SemaphoreValve class that Remy implemented.</p>
43  *
44  * @author Remy Maucherat, Scott Marlow
45  * @version $Revision: 34135 $ $Date: 2005-07-28 12:31:21 -0400 (Thu, 28 Jul 2005) $
46  */

47
48 public class SemaphoreValve
49     extends ValveBase
50     implements Lifecycle {
51
52
53     // ----------------------------------------------------- Instance Variables
54

55
56     /**
57      * The descriptive information related to this implementation.
58      */

59     private static final String JavaDoc info =
60         "org.jboss.web.tomcat.filters.SemaphoreValve/1.0";
61
62
63     /**
64      * Semaphore.
65      */

66     protected Sync semaphore = null;
67     
68
69     /**
70      * The lifecycle event support for this component.
71      */

72     protected LifecycleSupport lifecycle = new LifecycleSupport(this);
73
74
75     /**
76      * Has this component been started yet?
77      */

78     private boolean started = false;
79
80
81     // ------------------------------------------------------------- Properties
82

83     
84     /**
85      * Concurrency level of the semaphore.
86      */

87     protected int concurrency = 10;
88     public int getConcurrency() { return concurrency; }
89     public void setConcurrency(int concurrency) { this.concurrency = concurrency; }
90     
91
92     // ------------------------------------------------------ Lifecycle Methods
93

94
95     /**
96      * Add a lifecycle event listener to this component.
97      *
98      * @param listener The listener to add
99      */

100     public void addLifecycleListener(LifecycleListener listener) {
101
102         lifecycle.addLifecycleListener(listener);
103
104     }
105
106
107     /**
108      * Get the lifecycle listeners associated with this lifecycle. If this
109      * Lifecycle has no listeners registered, a zero-length array is returned.
110      */

111     public LifecycleListener[] findLifecycleListeners() {
112
113         return lifecycle.findLifecycleListeners();
114
115     }
116
117
118     /**
119      * Remove a lifecycle event listener from this component.
120      *
121      * @param listener The listener to add
122      */

123     public void removeLifecycleListener(LifecycleListener listener) {
124
125         lifecycle.removeLifecycleListener(listener);
126
127     }
128
129
130     /**
131      * Prepare for the beginning of active use of the public methods of this
132      * component. This method should be called after <code>configure()</code>,
133      * and before any of the public methods of the component are utilized.
134      *
135      * @exception LifecycleException if this component detects a fatal error
136      * that prevents this component from being used
137      */

138     public void start() throws LifecycleException {
139
140         // Validate and update our current component state
141
if (started)
142             throw new LifecycleException
143                 ("Semaphore valve already started");
144         lifecycle.fireLifecycleEvent(START_EVENT, null);
145         started = true;
146
147         semaphore = new FIFOSemaphore(concurrency);
148
149     }
150
151
152     /**
153      * Gracefully terminate the active use of the public methods of this
154      * component. This method should be the last one called on a given
155      * instance of this component.
156      *
157      * @exception LifecycleException if this component detects a fatal error
158      * that needs to be reported
159      */

160     public void stop() throws LifecycleException {
161
162         // Validate and update our current component state
163
if (!started)
164             throw new LifecycleException
165                 ("Semaphore valve not started");
166         lifecycle.fireLifecycleEvent(STOP_EVENT, null);
167         started = false;
168
169         semaphore = null;
170
171     }
172
173     
174     // --------------------------------------------------------- Public Methods
175

176
177     /**
178      * Return descriptive information about this Valve implementation.
179      */

180     public String JavaDoc getInfo() {
181         return (info);
182     }
183
184
185     /**
186      * Do concurrency control on the request using the semaphore.
187      *
188      * @param request The servlet request to be processed
189      * @param response The servlet response to be created
190      *
191      * @exception IOException if an input/output error occurs
192      * @exception ServletException if a servlet error occurs
193      */

194     public void invoke(Request request, Response response)
195         throws IOException JavaDoc, ServletException JavaDoc {
196
197     boolean shouldRelease = false;
198         try {
199             semaphore.acquire();
200             shouldRelease = true;
201             // Perform the request
202
getNext().invoke(request, response);
203         } catch( java.lang.InterruptedException JavaDoc e) {
204             container.getLogger().error(e.getMessage(), e);
205         } finally {
206             if (shouldRelease)
207                 semaphore.release();
208         }
209
210     }
211
212
213 }
214
Popular Tags