1
/* $Id: FinderFromClass.java 155412 2005-02-26 12:58:36Z dirkv $
3
* Copyright 2004 The Apache Software Foundation.
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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.digester.plugins.strategies;
20
import java.util.Properties;
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;
27
* A rule-finding algorithm which expects the caller to specify a classname and
28
* methodname as plugin properties.
33
public class FinderFromClass extends RuleFinder {
34
public static String DFLT_RULECLASS_ATTR = "ruleclass";
35
public static String DFLT_METHOD_ATTR = "method";
36
public static String DFLT_METHOD_NAME = "addRules";
38
private String ruleClassAttr;
39
private String methodAttr;
40
private String dfltMethodName;
43
* See {@link #findLoader}.
45
public FinderFromClass() {
46
this(DFLT_RULECLASS_ATTR, DFLT_METHOD_ATTR, DFLT_METHOD_NAME);
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.
54
* @param ruleClassAttr must be non-null.
55
* @param methodAttr may be null.
56
* @param dfltMethodName may be null.
58
public FinderFromClass(String ruleClassAttr, String methodAttr,
59
String dfltMethodName) {
60
this.ruleClassAttr = ruleClassAttr;
61
this.methodAttr = methodAttr;
62
this.dfltMethodName = dfltMethodName;
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
71
* If there is no matching property provided, then just return null.
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>
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.
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.
92
public RuleLoader findLoader(Digester digester, Class pluginClass,
93
Properties p) throws PluginException {
95
String ruleClassName = p.getProperty(ruleClassAttr);
96
if (ruleClassName == null) {
97
// nope, user hasn't requested dynamic rules to be loaded
98
// from a specific class.
102
// ok, we are in business
103
String methodName = null;
104
if (methodAttr != null) {
105
methodName = p.getProperty(methodAttr);
107
if (methodName == null) {
108
methodName = dfltMethodName;
110
if (methodName == null) {
111
methodName = DFLT_METHOD_NAME;
116
// load the plugin class object
118
digester.getClassLoader().loadClass(ruleClassName);
119
} catch(ClassNotFoundException cnfe) {
120
throw new PluginException(
121
"Unable to load class " + ruleClassName, cnfe);
124
return new LoaderFromClass(ruleClass, methodName);