ValveBase.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  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. package org.apache.catalina.valves;

  18. import org.apache.catalina.Contained;
  19. import org.apache.catalina.Container;
  20. import org.apache.catalina.LifecycleException;
  21. import org.apache.catalina.LifecycleState;
  22. import org.apache.catalina.Pipeline;
  23. import org.apache.catalina.Valve;
  24. import org.apache.catalina.util.LifecycleMBeanBase;
  25. import org.apache.catalina.util.ToStringUtil;
  26. import org.apache.juli.logging.Log;
  27. import org.apache.tomcat.util.res.StringManager;

  28. /**
  29.  * Convenience base class for implementations of the <b>Valve</b> interface. A subclass <strong>MUST</strong> implement
  30.  * an <code>invoke()</code> method to provide the required functionality, and <strong>MAY</strong> implement the
  31.  * <code>Lifecycle</code> interface to provide configuration management and lifecycle support.
  32.  *
  33.  * @author Craig R. McClanahan
  34.  */
  35. public abstract class ValveBase extends LifecycleMBeanBase implements Contained, Valve {

  36.     protected static final StringManager sm = StringManager.getManager(ValveBase.class);


  37.     // ------------------------------------------------------ Constructor

  38.     public ValveBase() {
  39.         this(false);
  40.     }


  41.     public ValveBase(boolean asyncSupported) {
  42.         this.asyncSupported = asyncSupported;
  43.     }


  44.     // ------------------------------------------------------ Instance Variables

  45.     /**
  46.      * Does this valve support Servlet 3+ async requests?
  47.      */
  48.     protected boolean asyncSupported;


  49.     /**
  50.      * The Container whose pipeline this Valve is a component of.
  51.      */
  52.     protected Container container = null;


  53.     /**
  54.      * Container log
  55.      */
  56.     protected Log containerLog = null;


  57.     /**
  58.      * The next Valve in the pipeline this Valve is a component of.
  59.      */
  60.     protected Valve next = null;


  61.     // -------------------------------------------------------------- Properties

  62.     @Override
  63.     public Container getContainer() {
  64.         return container;
  65.     }


  66.     @Override
  67.     public void setContainer(Container container) {
  68.         this.container = container;
  69.     }


  70.     @Override
  71.     public boolean isAsyncSupported() {
  72.         return asyncSupported;
  73.     }


  74.     public void setAsyncSupported(boolean asyncSupported) {
  75.         this.asyncSupported = asyncSupported;
  76.     }


  77.     @Override
  78.     public Valve getNext() {
  79.         return next;
  80.     }


  81.     @Override
  82.     public void setNext(Valve valve) {
  83.         this.next = valve;
  84.     }


  85.     // ---------------------------------------------------------- Public Methods

  86.     /**
  87.      * {@inheritDoc}
  88.      * <p>
  89.      * The default implementation is NO-OP.
  90.      */
  91.     @Override
  92.     public void backgroundProcess() {
  93.         // NOOP by default
  94.     }


  95.     @Override
  96.     protected void initInternal() throws LifecycleException {
  97.         super.initInternal();
  98.         containerLog = getContainer().getLogger();
  99.     }


  100.     /**
  101.      * Start this component and implement the requirements of
  102.      * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
  103.      *
  104.      * @exception LifecycleException if this component detects a fatal error that prevents this component from being
  105.      *                                   used
  106.      */
  107.     @Override
  108.     protected void startInternal() throws LifecycleException {
  109.         setState(LifecycleState.STARTING);
  110.     }


  111.     /**
  112.      * Stop this component and implement the requirements of
  113.      * {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
  114.      *
  115.      * @exception LifecycleException if this component detects a fatal error that prevents this component from being
  116.      *                                   used
  117.      */
  118.     @Override
  119.     protected void stopInternal() throws LifecycleException {
  120.         setState(LifecycleState.STOPPING);
  121.     }


  122.     @Override
  123.     public String toString() {
  124.         return ToStringUtil.toString(this);
  125.     }


  126.     // -------------------- JMX and Registration --------------------

  127.     @Override
  128.     public String getObjectNameKeyProperties() {
  129.         StringBuilder name = new StringBuilder("type=Valve");

  130.         Container container = getContainer();

  131.         name.append(container.getMBeanKeyProperties());

  132.         int seq = 0;

  133.         // Pipeline may not be present in unit testing
  134.         Pipeline p = container.getPipeline();
  135.         if (p != null) {
  136.             for (Valve valve : p.getValves()) {
  137.                 // Skip null valves
  138.                 if (valve == null) {
  139.                     continue;
  140.                 }
  141.                 // Only compare valves in pipeline until we find this valve
  142.                 if (valve == this) {
  143.                     break;
  144.                 }
  145.                 if (valve.getClass() == this.getClass()) {
  146.                     // Duplicate valve earlier in pipeline
  147.                     // increment sequence number
  148.                     seq++;
  149.                 }
  150.             }
  151.         }

  152.         if (seq > 0) {
  153.             name.append(",seq=");
  154.             name.append(seq);
  155.         }

  156.         String className = this.getClass().getName();
  157.         int period = className.lastIndexOf('.');
  158.         if (period >= 0) {
  159.             className = className.substring(period + 1);
  160.         }
  161.         name.append(",name=");
  162.         name.append(className);

  163.         return name.toString();
  164.     }


  165.     @Override
  166.     public String getDomainInternal() {
  167.         Container c = getContainer();
  168.         if (c == null) {
  169.             return null;
  170.         } else {
  171.             return c.getDomain();
  172.         }
  173.     }
  174. }