01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: package org.apache.catalina.startup;
19:
20: import org.apache.catalina.Lifecycle;
21: import org.apache.catalina.LifecycleListener;
22: import org.apache.tomcat.util.digester.Rule;
23: import org.xml.sax.Attributes;
24:
25: /**
26: * <p>Rule that creates a new <code>LifecycleListener</code> instance,
27: * and associates it with the top object on the stack (which must
28: * implement <code>LifecycleListener</code>).</p>
29: */
30:
31: public class LifecycleListenerRule extends Rule {
32:
33: // ----------------------------------------------------------- Constructors
34:
35: /**
36: * Construct a new instance of this Rule.
37: *
38: * @param listenerClass Default name of the LifecycleListener
39: * implementation class to be created
40: * @param attributeName Name of the attribute that optionally
41: * includes an override name of the LifecycleListener class
42: */
43: public LifecycleListenerRule(String listenerClass,
44: String attributeName) {
45:
46: this .listenerClass = listenerClass;
47: this .attributeName = attributeName;
48:
49: }
50:
51: // ----------------------------------------------------- Instance Variables
52:
53: /**
54: * The attribute name of an attribute that can override the
55: * implementation class name.
56: */
57: private String attributeName;
58:
59: /**
60: * The name of the <code>LifecycleListener</code> implementation class.
61: */
62: private String listenerClass;
63:
64: // --------------------------------------------------------- Public Methods
65:
66: /**
67: * Handle the beginning of an XML element.
68: *
69: * @param attributes The attributes of this element
70: *
71: * @exception Exception if a processing error occurs
72: */
73: public void begin(String namespace, String name,
74: Attributes attributes) throws Exception {
75:
76: // Instantiate a new LifecyleListener implementation object
77: String className = listenerClass;
78: if (attributeName != null) {
79: String value = attributes.getValue(attributeName);
80: if (value != null)
81: className = value;
82: }
83: Class clazz = Class.forName(className);
84: LifecycleListener listener = (LifecycleListener) clazz
85: .newInstance();
86:
87: // Add this LifecycleListener to our associated component
88: Lifecycle lifecycle = (Lifecycle) digester.peek();
89: lifecycle.addLifecycleListener(listener);
90:
91: }
92:
93: }
|