KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StoreFactoryRule


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.storeconfig;
18
19 import org.apache.tomcat.util.digester.Rule;
20 import org.xml.sax.Attributes JavaDoc;
21
22 /**
23  * <p>
24  * Rule that creates a new <code>IStoreFactory</code> instance, and associates
25  * it with the top object on the stack (which must implement
26  * <code>IStoreFactory</code>).
27  * </p>
28  *
29  * @author Peter Rossbach
30  */

31
32 public class StoreFactoryRule extends Rule {
33
34     // ----------------------------------------------------------- Constructors
35

36     /**
37      * Construct a new instance of this Rule.
38      *
39      * @param storeFactoryClass
40      * Default name of the StoreFactory implementation class to be
41      * created
42      * @param attributeName
43      * Name of the attribute that optionally includes an override
44      * name of the IStoreFactory class
45      */

46     public StoreFactoryRule(String JavaDoc storeFactoryClass, String JavaDoc attributeName,
47             String JavaDoc storeAppenderClass, String JavaDoc appenderAttributeName) {
48
49         this.storeFactoryClass = storeFactoryClass;
50         this.attributeName = attributeName;
51         this.appenderAttributeName = appenderAttributeName;
52         this.storeAppenderClass = storeAppenderClass;
53
54     }
55
56     // ----------------------------------------------------- Instance Variables
57

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

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

69     private String JavaDoc storeFactoryClass;
70
71     private String JavaDoc storeAppenderClass;
72
73     // --------------------------------------------------------- Public Methods
74

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

84     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
85             throws Exception JavaDoc {
86
87         IStoreFactory factory = (IStoreFactory) newInstance(attributeName,
88                 storeFactoryClass, attributes);
89         StoreAppender storeAppender = (StoreAppender) newInstance(
90                 appenderAttributeName, storeAppenderClass, attributes);
91         factory.setStoreAppender(storeAppender);
92
93         // Add this StoreFactory to our associated component
94
StoreDescription desc = (StoreDescription) digester.peek(0);
95         StoreRegistry registry = (StoreRegistry) digester.peek(1);
96         factory.setRegistry(registry);
97         desc.setStoreFactory(factory);
98
99     }
100
101     /**
102      * create new instance from attribte className!
103      *
104      * @param attr class Name attribute
105      * @param defaultName Default Class
106      * @param attributes current digester attribute elements
107      * @return new config object instance
108      * @throws ClassNotFoundException
109      * @throws InstantiationException
110      * @throws IllegalAccessException
111      */

112     protected Object JavaDoc newInstance(String JavaDoc attr, String JavaDoc defaultName,
113             Attributes JavaDoc attributes) throws ClassNotFoundException JavaDoc,
114             InstantiationException JavaDoc, IllegalAccessException JavaDoc {
115         String JavaDoc className = defaultName;
116         if (attr != null) {
117             String JavaDoc value = attributes.getValue(attr);
118             if (value != null)
119                 className = value;
120         }
121         Class JavaDoc clazz = Class.forName(className);
122         return clazz.newInstance();
123     }
124 }
Popular Tags