FileDocCategorySizeDatePackage
SemaphoreValve.javaAPI DocApache Tomcat 6.0.148273Fri Jul 20 04:20:32 BST 2007org.apache.catalina.valves

SemaphoreValve

public class SemaphoreValve extends ValveBase implements org.apache.catalina.Lifecycle

Implementation of a Valve that limits concurrency.

This Valve may be attached to any Container, depending on the granularity of the concurrency control you wish to perform.

author
Remy Maucherat
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
private static final String
info
The descriptive information related to this implementation.
private org.apache.catalina.util.StringManager
sm
The string manager for this package.
protected Semaphore
semaphore
Semaphore.
protected org.apache.catalina.util.LifecycleSupport
lifecycle
The lifecycle event support for this component.
private boolean
started
Has this component been started yet?
protected int
concurrency
Concurrency level of the semaphore.
protected boolean
fairness
Fairness of the semaphore.
protected boolean
block
Block until a permit is available.
protected boolean
interruptible
Block interruptibly until a permit is available.
Constructors Summary
Methods Summary
public voidaddLifecycleListener(org.apache.catalina.LifecycleListener listener)
Add a lifecycle event listener to this component.

param
listener The listener to add


        lifecycle.addLifecycleListener(listener);

    
public booleancontrolConcurrency(org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response)
Subclass friendly method to add conditions.

        return true;
    
public org.apache.catalina.LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.


        return lifecycle.findLifecycleListeners();

    
public booleangetBlock()

        return block; 
public intgetConcurrency()

        return concurrency; 
public booleangetFairness()

        return fairness; 
public java.lang.StringgetInfo()
Return descriptive information about this Valve implementation.

        return (info);
    
public booleangetInterruptible()

        return interruptible; 
public voidinvoke(org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response)
Do concurrency control on the request using the semaphore.

param
request The servlet request to be processed
param
response The servlet response to be created
exception
IOException if an input/output error occurs
exception
ServletException if a servlet error occurs


        if (controlConcurrency(request, response)) {
            boolean shouldRelease = true;
            try {
                if (block) {
                    if (interruptible) {
                        try {
                            semaphore.acquire();
                        } catch (InterruptedException e) {
                            shouldRelease = false;
                            permitDenied(request, response);
                            return;
                        }  
                    } else {
                        semaphore.acquireUninterruptibly();
                    }
                } else {
                    if (!semaphore.tryAcquire()) {
                        shouldRelease = false;
                        permitDenied(request, response);
                        return;
                    }
                }
                getNext().invoke(request, response);
            } finally {
                if (shouldRelease) {
                    semaphore.release();
                }
            }
        } else {
            getNext().invoke(request, response);
        }

    
public voidpermitDenied(org.apache.catalina.connector.Request request, org.apache.catalina.connector.Response response)
Subclass friendly method to add error handling when a permit isn't granted.

    
public voidremoveLifecycleListener(org.apache.catalina.LifecycleListener listener)
Remove a lifecycle event listener from this component.

param
listener The listener to add


        lifecycle.removeLifecycleListener(listener);

    
public voidsetBlock(boolean block)

 this.block = block; 
public voidsetConcurrency(int concurrency)

 this.concurrency = concurrency; 
public voidsetFairness(boolean fairness)

 this.fairness = fairness; 
public voidsetInterruptible(boolean interruptible)

 this.interruptible = interruptible; 
public voidstart()
Prepare for the beginning of active use of the public methods of this component. This method should be called after configure(), and before any of the public methods of the component are utilized.

exception
LifecycleException if this component detects a fatal error that prevents this component from being used


        // Validate and update our current component state
        if (started)
            throw new LifecycleException
                (sm.getString("semaphoreValve.alreadyStarted"));
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

        semaphore = new Semaphore(concurrency, fairness);

    
public voidstop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component.

exception
LifecycleException if this component detects a fatal error that needs to be reported


        // Validate and update our current component state
        if (!started)
            throw new LifecycleException
                (sm.getString("semaphoreValve.notStarted"));
        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
        started = false;

        semaphore = null;