FileDocCategorySizeDatePackage
StandardPipeline.javaAPI DocApache Tomcat 6.0.1417353Fri Jul 20 04:20:34 BST 2007org.apache.catalina.core

StandardPipeline

public class StandardPipeline extends Object implements org.apache.catalina.Contained, org.apache.catalina.Lifecycle, org.apache.catalina.Pipeline
Standard implementation of a processing Pipeline that will invoke a series of Valves that have been configured to be called in order. This implementation can be used for any type of Container. IMPLEMENTATION WARNING - This implementation assumes that no calls to addValve() or removeValve are allowed while a request is currently being processed. Otherwise, the mechanism by which per-thread state is maintained will need to be modified.
author
Craig R. McClanahan

Fields Summary
private static org.apache.juli.logging.Log
log
protected org.apache.catalina.Valve
basic
The basic Valve (if any) associated with this Pipeline.
protected org.apache.catalina.Container
container
The Container with which this Pipeline is associated.
protected String
info
Descriptive information about this implementation.
protected org.apache.catalina.util.LifecycleSupport
lifecycle
The lifecycle event support for this component.
protected static org.apache.catalina.util.StringManager
sm
The string manager for this package.
protected boolean
started
Has this component been started yet?
protected org.apache.catalina.Valve
first
The first valve associated with this Pipeline.
Constructors Summary
public StandardPipeline()
Construct a new StandardPipeline instance with no associated Container.


    // ----------------------------------------------------------- Constructors


                  
      

        this(null);

    
public StandardPipeline(org.apache.catalina.Container container)
Construct a new StandardPipeline instance that is associated with the specified Container.

param
container The container we should be associated with


        super();
        setContainer(container);

    
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 voidaddValve(org.apache.catalina.Valve valve)

Add a new Valve to the end of the pipeline associated with this Container. Prior to adding the Valve, the Valve's setContainer() method will be called, if it implements Contained, with the owning Container as an argument. The method may throw an IllegalArgumentException if this Valve chooses not to be associated with this Container, or IllegalStateException if it is already associated with a different Container.

param
valve Valve to be added
exception
IllegalArgumentException if this Container refused to accept the specified Valve
exception
IllegalArgumentException if the specifie Valve refuses to be associated with this Container
exception
IllegalStateException if the specified Valve is already associated with a different Container

    
        // Validate that we can add this Valve
        if (valve instanceof Contained)
            ((Contained) valve).setContainer(this.container);

        // Start the new component if necessary
        if (started) {
            if (valve instanceof Lifecycle) {
                try {
                    ((Lifecycle) valve).start();
                } catch (LifecycleException e) {
                    log.error("StandardPipeline.addValve: start: ", e);
                }
            }
            // Register the newly added valve
            registerValve(valve);
        }

        // Add this Valve to the set associated with this Pipeline
        if (first == null) {
        	first = valve;
        	valve.setNext(basic);
        } else {
            Valve current = first;
            while (current != null) {
				if (current.getNext() == basic) {
					current.setNext(valve);
					valve.setNext(basic);
					break;
				}
				current = current.getNext();
			}
        }

    
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 org.apache.catalina.ValvegetBasic()

Return the Valve instance that has been distinguished as the basic Valve for this Pipeline (if any).


        return (this.basic);

    
public org.apache.catalina.ContainergetContainer()
Return the Container with which this Pipeline is associated.


        return (this.container);

    
public org.apache.catalina.ValvegetFirst()

        if (first != null) {
            return first;
        } else {
            return basic;
        }
    
public java.lang.StringgetInfo()
Return descriptive information about this implementation class.



    // --------------------------------------------------------- Public Methods


                
       

        return (this.info);

    
public javax.management.ObjectName[]getValveObjectNames()


    	ArrayList valveList = new ArrayList();
        Valve current = first;
        if (current == null) {
        	current = basic;
        }
        while (current != null) {
        	if (current instanceof ValveBase) {
        		valveList.add(((ValveBase) current).getObjectName());
        	}
        	current = current.getNext();
        }

        return ((ObjectName[]) valveList.toArray(new ObjectName[0]));

    
public org.apache.catalina.Valve[]getValves()
Return the set of Valves in the pipeline associated with this Container, including the basic Valve (if any). If there are no such Valves, a zero-length array is returned.


    	ArrayList valveList = new ArrayList();
        Valve current = first;
        if (current == null) {
        	current = basic;
        }
        while (current != null) {
        	valveList.add(current);
        	current = current.getNext();
        }

        return ((Valve[]) valveList.toArray(new Valve[0]));

    
private voidregisterValve(org.apache.catalina.Valve valve)


        if( valve instanceof ValveBase &&
                ((ValveBase)valve).getObjectName()==null ) {
            try {
                
                String domain=((ContainerBase)container).getDomain();
                if( container instanceof StandardContext ) {
                    domain=((StandardContext)container).getEngineName();
                }
                if( container instanceof StandardWrapper) {
                    Container ctx=((StandardWrapper)container).getParent();
                    domain=((StandardContext)ctx).getEngineName();
                }
                ObjectName vname=((ValveBase)valve).createObjectName(
                        domain,
                        ((ContainerBase)container).getJmxName());
                if( vname != null ) {
                    ((ValveBase)valve).setObjectName(vname);
                    Registry.getRegistry(null, null).registerComponent
                        (valve, vname, valve.getClass().getName());
                    ((ValveBase)valve).setController
                        (((ContainerBase)container).getJmxName());
                }
            } catch( Throwable t ) {
                log.info( "Can't register valve " + valve , t );
            }
        }
    
public voidremoveLifecycleListener(org.apache.catalina.LifecycleListener listener)
Remove a lifecycle event listener from this component.

param
listener The listener to remove


        lifecycle.removeLifecycleListener(listener);

    
public voidremoveValve(org.apache.catalina.Valve valve)
Remove the specified Valve from the pipeline associated with this Container, if it is found; otherwise, do nothing. If the Valve is found and removed, the Valve's setContainer(null) method will be called if it implements Contained.

param
valve Valve to be removed


        Valve current;
        if(first == valve) {
            first = first.getNext();
            current = null;
        } else {
            current = first;
        }
        while (current != null) {
            if (current.getNext() == valve) {
                current.setNext(valve.getNext());
                break;
            }
            current = current.getNext();
        }

        if (first == basic) first = null;

        if (valve instanceof Contained)
            ((Contained) valve).setContainer(null);

        // Stop this valve if necessary
        if (started) {
            if (valve instanceof Lifecycle) {
                try {
                    ((Lifecycle) valve).stop();
                } catch (LifecycleException e) {
                    log.error("StandardPipeline.removeValve: stop: ", e);
                }
            }
            // Unregister the removed valave
            unregisterValve(valve);
        }
    
    
public voidsetBasic(org.apache.catalina.Valve valve)

Set the Valve instance that has been distinguished as the basic Valve for this Pipeline (if any). Prioer to setting the basic Valve, the Valve's setContainer() will be called, if it implements Contained, with the owning Container as an argument. The method may throw an IllegalArgumentException if this Valve chooses not to be associated with this Container, or IllegalStateException if it is already associated with a different Container.

param
valve Valve to be distinguished as the basic Valve


        // Change components if necessary
        Valve oldBasic = this.basic;
        if (oldBasic == valve)
            return;

        // Stop the old component if necessary
        if (oldBasic != null) {
            if (started && (oldBasic instanceof Lifecycle)) {
                try {
                    ((Lifecycle) oldBasic).stop();
                } catch (LifecycleException e) {
                    log.error("StandardPipeline.setBasic: stop", e);
                }
            }
            if (oldBasic instanceof Contained) {
                try {
                    ((Contained) oldBasic).setContainer(null);
                } catch (Throwable t) {
                    ;
                }
            }
        }

        // Start the new component if necessary
        if (valve == null)
            return;
        if (valve instanceof Contained) {
            ((Contained) valve).setContainer(this.container);
        }
        if (valve instanceof Lifecycle) {
            try {
                ((Lifecycle) valve).start();
            } catch (LifecycleException e) {
                log.error("StandardPipeline.setBasic: start", e);
                return;
            }
        }

        // Update the pipeline
        Valve current = first;
        while (current != null) {
        	if (current.getNext() == oldBasic) {
        		current.setNext(valve);
        		break;
        	}
        	current = current.getNext();
        }
        
        this.basic = valve;

    
public voidsetContainer(org.apache.catalina.Container container)
Set the Container with which this Pipeline is associated.

param
container The new associated container


        this.container = container;

    
public synchronized voidstart()
Prepare for active use of the public methods of this Component.

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


        // Validate and update our current component state
        if (started)
            throw new LifecycleException
                (sm.getString("standardPipeline.alreadyStarted"));

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);

        started = true;

        // Start the Valves in our pipeline (including the basic), if any
        Valve current = first;
        if (current == null) {
        	current = basic;
        }
        while (current != null) {
            if (current instanceof Lifecycle)
                ((Lifecycle) current).start();
            registerValve(current);
        	current = current.getNext();
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(START_EVENT, null);

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);

    
public synchronized voidstop()
Gracefully shut down active use of the public methods 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("standardPipeline.notStarted"));

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
        started = false;

        // Stop the Valves in our pipeline (including the basic), if any
        Valve current = first;
        if (current == null) {
        	current = basic;
        }
        while (current != null) {
            if (current instanceof Lifecycle)
                ((Lifecycle) current).stop();
            unregisterValve(current);
        	current = current.getNext();
        }

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
    
private voidunregisterValve(org.apache.catalina.Valve valve)

        if( valve instanceof ValveBase ) {
            try {
                ValveBase vb=(ValveBase)valve;
                if( vb.getController()!=null &&
                        vb.getController() == 
                        ((ContainerBase)container).getJmxName() ) {
                    
                    ObjectName vname=vb.getObjectName();
                    Registry.getRegistry(null, null).getMBeanServer()
                        .unregisterMBean(vname);
                    ((ValveBase)valve).setObjectName(null);
                }
            } catch( Throwable t ) {
                log.info( "Can't unregister valve " + valve , t );
            }
        }