Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.catalina.valves;
 
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 
 

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(s):
Remy Maucherat
Version:
$Revision: 515 $ $Date: 2008-03-17 22:02:23 +0100 (Mon, 17 Mar 2008) $
 
 
 public class SemaphoreValve
     extends ValveBase
     implements Lifecycle {
 
 
     // ----------------------------------------------------- Instance Variables
 

    
The descriptive information related to this implementation.
 
     private static final String info =
         "org.apache.catalina.valves.SemaphoreValve/1.0";


    
Semaphore.
 
     protected Semaphore semaphore = null;
    

    
The lifecycle event support for this component.
 
     protected LifecycleSupport lifecycle = new LifecycleSupport(this);


    
Has this component been started yet?
 
     private boolean started = false;
 
 
     // ------------------------------------------------------------- Properties
 
    
    
Concurrency level of the semaphore.
 
     protected int concurrency = 10;
     public int getConcurrency() { return ; }
     public void setConcurrency(int concurrency) { this. = concurrency; }
    

    
Fairness of the semaphore.
 
     protected boolean fairness = false;
     public boolean getFairness() { return ; }
     public void setFairness(boolean fairness) { this. = fairness; }
    

    
Block until a permit is available.
    protected boolean block = true;
    public boolean getBlock() { return ; }
    public void setBlock(boolean block) { this. = block; }
    

    
Block interruptibly until a permit is available.
    protected boolean interruptible = false;
    public boolean getInterruptible() { return ; }
    public void setInterruptible(boolean interruptible) { this. = interruptible; }
    
    // ------------------------------------------------------ Lifecycle Methods


    
Add a lifecycle event listener to this component.

Parameters:
listener The listener to add
    public void addLifecycleListener(LifecycleListener listener) {
        .addLifecycleListener(listener);
    }


    
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.
        return .findLifecycleListeners();
    }


    
Remove a lifecycle event listener from this component.

Parameters:
listener The listener to add
    public void removeLifecycleListener(LifecycleListener listener) {
        .removeLifecycleListener(listener);
    }


    
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.

Throws:
org.apache.catalina.LifecycleException if this component detects a fatal error that prevents this component from being used
    public void start() throws LifecycleException {
        // Validate and update our current component state
        if ()
            throw new LifecycleException
                (.valveAlreadyStarted());
         = true;
         = new Semaphore();
    }


    
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.

Throws:
org.apache.catalina.LifecycleException if this component detects a fatal error that needs to be reported
    public void stop() throws LifecycleException {
        // Validate and update our current component state
        if (!)
            throw new LifecycleException
                (.valveNotStarted());
        .fireLifecycleEvent(null);
         = false;
         = null;
    }
    
    // --------------------------------------------------------- Public Methods


    
Return descriptive information about this Valve implementation.
    public String getInfo() {
        return ();
    }


    
Do concurrency control on the request using the semaphore.

Parameters:
request The servlet request to be processed
response The servlet response to be created
Throws:
java.io.IOException if an input/output error occurs
javax.servlet.ServletException if a servlet error occurs
    public void invoke(Request requestResponse response)
        throws IOExceptionServletException {
        if (controlConcurrency(requestresponse)) {
            boolean shouldRelease = true;
            try {
                if () {
                    if () {
                        try {
                            .acquire();
                        } catch (InterruptedException e) {
                            shouldRelease = false;
                            permitDenied(requestresponse);
                            return;
                        }  
                    } else {
                        .acquireUninterruptibly();
                    }
                } else {
                    if (!.tryAcquire()) {
                        shouldRelease = false;
                        permitDenied(requestresponse);
                        return;
                    }
                }
                getNext().invoke(requestresponse);
            } finally {
                if (shouldRelease) {
                    .release();
                }
            }
        } else {
            getNext().invoke(requestresponse);
        }
    }

    
    
Subclass friendly method to add conditions.
    public boolean controlConcurrency(Request requestResponse response) {
        return true;
    }
    

    
Subclass friendly method to add error handling when a permit isn't granted.
    public void permitDenied(Request requestResponse response)
        throws IOExceptionServletException {
    }
    
New to GrepCode? Check out our FAQ X