KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > strategies > FinderFromClass


1 /* $Id: FinderFromClass.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 package org.apache.commons.digester.plugins.strategies;
19
20 import java.util.Properties JavaDoc;
21 import org.apache.commons.digester.Digester;
22 import org.apache.commons.digester.plugins.RuleFinder;
23 import org.apache.commons.digester.plugins.RuleLoader;
24 import org.apache.commons.digester.plugins.PluginException;
25
26 /**
27  * A rule-finding algorithm which expects the caller to specify a classname and
28  * methodname as plugin properties.
29  *
30  * @since 1.6
31  */

32
33 public class FinderFromClass extends RuleFinder {
34     public static String JavaDoc DFLT_RULECLASS_ATTR = "ruleclass";
35     public static String JavaDoc DFLT_METHOD_ATTR = "method";
36     public static String JavaDoc DFLT_METHOD_NAME = "addRules";
37     
38     private String JavaDoc ruleClassAttr;
39     private String JavaDoc methodAttr;
40     private String JavaDoc dfltMethodName;
41     
42     /**
43      * See {@link #findLoader}.
44      */

45     public FinderFromClass() {
46         this(DFLT_RULECLASS_ATTR, DFLT_METHOD_ATTR, DFLT_METHOD_NAME);
47     }
48
49     /**
50      * Create a rule-finder which invokes a user-specified method on a
51      * user-specified class whenever dynamic rules for a plugin need to be
52      * loaded. See the findRules method for more info.
53      *
54      * @param ruleClassAttr must be non-null.
55      * @param methodAttr may be null.
56      * @param dfltMethodName may be null.
57      */

58     public FinderFromClass(String JavaDoc ruleClassAttr, String JavaDoc methodAttr,
59                 String JavaDoc dfltMethodName) {
60         this.ruleClassAttr = ruleClassAttr;
61         this.methodAttr = methodAttr;
62         this.dfltMethodName = dfltMethodName;
63     }
64     
65     /**
66      * If there exists a property with the name matching constructor param
67      * ruleClassAttr, then load the specified class, locate the appropriate
68      * rules-adding method on that class, and return an object encapsulating
69      * that info.
70      * <p>
71      * If there is no matching property provided, then just return null.
72      * <p>
73      * The returned object (when non-null) will invoke the target method
74      * on the selected class whenever its addRules method is invoked. The
75      * target method is expected to have the following prototype:
76      * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
77      * <p>
78      * The target method can be specified in several ways. If this object's
79      * constructor was passed a non-null methodAttr parameter, and the
80      * properties defines a value with that key, then that is taken as the
81      * target method name. If there is no matching property, or the constructor
82      * was passed null for methodAttr, then the dfltMethodName passed to the
83      * constructor is used as the name of the method on the target class. And
84      * if that was null, then DFLT_METHOD_NAME will be used.
85      * <p>
86      * When the user explicitly declares a plugin in the input xml, the
87      * xml attributes on the declaration tag are passed here as properties,
88      * so the user can select any class in the classpath (and any method on
89      * that class provided it has the correct prototype) as the source of
90      * dynamic rules for the plugged-in class.
91      */

92     public RuleLoader findLoader(Digester digester, Class JavaDoc pluginClass,
93                         Properties JavaDoc p) throws PluginException {
94
95         String JavaDoc ruleClassName = p.getProperty(ruleClassAttr);
96         if (ruleClassName == null) {
97             // nope, user hasn't requested dynamic rules to be loaded
98
// from a specific class.
99
return null;
100         }
101         
102         // ok, we are in business
103
String JavaDoc methodName = null;
104         if (methodAttr != null) {
105             methodName = p.getProperty(methodAttr);
106         }
107         if (methodName == null) {
108             methodName = dfltMethodName;
109         }
110         if (methodName == null) {
111             methodName = DFLT_METHOD_NAME;
112         }
113         
114         Class JavaDoc ruleClass;
115         try {
116             // load the plugin class object
117
ruleClass =
118                 digester.getClassLoader().loadClass(ruleClassName);
119         } catch(ClassNotFoundException JavaDoc cnfe) {
120             throw new PluginException(
121                 "Unable to load class " + ruleClassName, cnfe);
122         }
123
124         return new LoaderFromClass(ruleClass, methodName);
125     }
126 }
127
128
Popular Tags