/*
 * 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.startup;


import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleListener;
import org.apache.tomcat.util.digester.Rule;
import org.xml.sax.Attributes;


/**
 * <p>Rule that creates a new <code>LifecycleListener</code> instance,
 * and associates it with the top object on the stack (which must
 * implement <code>LifecycleListener</code>).</p>
 */

public class LifecycleListenerRule extends Rule {


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


    /**
     * Construct a new instance of this Rule.
     *
     * @param listenerClass Default name of the LifecycleListener
     *  implementation class to be created
     * @param attributeName Name of the attribute that optionally
     *  includes an override name of the LifecycleListener class
     */
    public LifecycleListenerRule(String listenerClass, String attributeName) {

        this.listenerClass = listenerClass;
        this.attributeName = attributeName;

    }


    // ----------------------------------------------------- Instance Variables


    /**
     * The attribute name of an attribute that can override the
     * implementation class name.
     */
    private String attributeName;


    /**
     * The name of the <code>LifecycleListener</code> implementation class.
     */
    private String listenerClass;


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


    /**
     * Handle the beginning of an XML element.
     *
     * @param attributes The attributes of this element
     *
     * @exception Exception if a processing error occurs
     */
    @Override
    public void begin(String namespace, String name, Attributes attributes)
        throws Exception {

        // Instantiate a new LifecyleListener implementation object
        String className = listenerClass;
        if (attributeName != null) {
            String value = attributes.getValue(attributeName);
            if (value != null)
                className = value;
        }
        Class<?> clazz = Class.forName(className);
        LifecycleListener listener =
            (LifecycleListener) clazz.newInstance();

        // Add this LifecycleListener to our associated component
        Lifecycle lifecycle = (Lifecycle) digester.peek();
        lifecycle.addLifecycleListener(listener);

    }


}

@@ -866,25 +866,25 @@
 atalina.
-Lifecycle
+Container
 ;%0Aimport
@@ -919,24 +919,74 @@
 leListener;%0A
+import org.apache.tomcat.util.IntrospectionUtils;%0A
 import org.a
@@ -1062,11 +1062,8 @@
 %0A * 
-%3Cp%3E
 Rule
@@ -1082,22 +1082,23 @@
 s a new 
-%3Ccode%3E
+%7B@link 
 Lifecycl
@@ -1110,28 +1110,9 @@
 ener
-%3C/code%3E instance,%0A *
+%7D
  and
@@ -1134,16 +1134,19 @@
 with the
+%0A *
  top obj
@@ -1162,17 +1162,16 @@
 e stack 
-(
 which mu
@@ -1176,64 +1176,552 @@
 must
-%0A * implement %3Ccode%3ELifecycleListener%3C/code%3E).%3C/p
+ implement %7B@link Container%7D. The%0A * implementation class to be used is determined by:%0A * %3Col%3E%0A * %3Cli%3EDoes the top element on the stack specify an implementation class using%0A *     the attribute specified when this rule was created?%3C/li%3E%0A * %3Cli%3EDoes the parent %7B@link Container%7D of the %7B@link Container%7D on the top of%0A *     the stack specify an implementation class using the attribute specified%0A *     when this rule was created?%3C/li%3E%0A * %3Cli%3EUse the default implementation class specified when this rule was%0A *     created.%3C/li%3E%0A * %3C/ol
 %3E%0A */%0A
-%0A
 publ
@@ -3098,285 +3098,1028 @@
     
-// Instantiate a new LifecyleListener implementation object%0A        String className = listenerClass;%0A        if (attributeName != null) %7B%0A            String value = attributes.getValue(attributeName);%0A            if (value != null)%0A                className = value;%0A        %7D
+Container c = (Container) digester.peek();%0A        Container p = null;%0A        Object obj = digester.peek(1);%0A        if (obj instanceof Container) %7B%0A            p = (Container) obj;%0A        %7D%0A%0A        String className = null;%0A        %0A        // Check the container for the specified attribute%0A        if (attributeName != null) %7B%0A            String value = attributes.getValue(attributeName);%0A            if (value != null)%0A                className = value;%0A        %7D%0A%0A        // Check the container's parent for the specified attribute%0A        if (p != null && className == null) %7B%0A            String configClass =%0A                (String) IntrospectionUtils.getProperty(p, attributeName);%0A            if (configClass != null && configClass.length() %3E 0) %7B%0A                className = configClass;%0A            %7D%0A        %7D%0A        %0A        // Use the default%0A        if (className == null) %7B%0A            className = listenerClass;%0A        %7D%0A        %0A        // Instantiate a new LifecyleListener implementation object
 %0A   
@@ -4335,76 +4335,9 @@
     
-Lifecycle lifecycle = (Lifecycle) digester.peek();%0A        lifecycle
+c
 .add
@@ -4365,17 +4365,16 @@
 tener);%0A
-%0A
     %7D%0A%0A%0A

Fix an issue where <Context .../> elements defined in server.xml did not use the configClass specified by the parent <Host .../> element