KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > LifecycleListenerRule


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
18
19 package org.apache.catalina.startup;
20
21
22 import org.apache.catalina.Lifecycle;
23 import org.apache.catalina.LifecycleListener;
24 import org.apache.tomcat.util.digester.Rule;
25 import org.xml.sax.Attributes JavaDoc;
26
27
28 /**
29  * <p>Rule that creates a new <code>LifecycleListener</code> instance,
30  * and associates it with the top object on the stack (which must
31  * implement <code>LifecycleListener</code>).</p>
32  */

33
34 public class LifecycleListenerRule extends Rule {
35
36
37     // ----------------------------------------------------------- Constructors
38

39
40     /**
41      * Construct a new instance of this Rule.
42      *
43      * @param listenerClass Default name of the LifecycleListener
44      * implementation class to be created
45      * @param attributeName Name of the attribute that optionally
46      * includes an override name of the LifecycleListener class
47      */

48     public LifecycleListenerRule(String JavaDoc listenerClass, String JavaDoc attributeName) {
49
50         this.listenerClass = listenerClass;
51         this.attributeName = attributeName;
52
53     }
54
55
56     // ----------------------------------------------------- Instance Variables
57

58
59     /**
60      * The attribute name of an attribute that can override the
61      * implementation class name.
62      */

63     private String JavaDoc attributeName;
64
65
66     /**
67      * The name of the <code>LifecycleListener</code> implementation class.
68      */

69     private String JavaDoc listenerClass;
70
71
72     // --------------------------------------------------------- Public Methods
73

74
75     /**
76      * Handle the beginning of an XML element.
77      *
78      * @param attributes The attributes of this element
79      *
80      * @exception Exception if a processing error occurs
81      */

82     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
83         throws Exception JavaDoc {
84
85         // Instantiate a new LifecyleListener implementation object
86
String JavaDoc className = listenerClass;
87         if (attributeName != null) {
88             String JavaDoc value = attributes.getValue(attributeName);
89             if (value != null)
90                 className = value;
91         }
92         Class JavaDoc clazz = Class.forName(className);
93         LifecycleListener listener =
94             (LifecycleListener) clazz.newInstance();
95
96         // Add this LifecycleListener to our associated component
97
Lifecycle lifecycle = (Lifecycle) digester.peek();
98         lifecycle.addLifecycleListener(listener);
99
100     }
101
102
103 }
104
Popular Tags