KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: LoaderFromClass.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.lang.reflect.Method JavaDoc;
21
22 import org.apache.commons.digester.Digester;
23 import org.apache.commons.beanutils.MethodUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.digester.plugins.RuleLoader;
26 import org.apache.commons.digester.plugins.PluginException;
27
28 /**
29  * A RuleLoader which invokes a static method on a target class, leaving that
30  * method to actually instantiate and add new rules to a Digester instance.
31  *
32  * @since 1.6
33  */

34
35 public class LoaderFromClass extends RuleLoader {
36     
37     private Class JavaDoc rulesClass;
38     private Method JavaDoc rulesMethod;
39     
40     /** Constructor. */
41     public LoaderFromClass(Class JavaDoc rulesClass, Method JavaDoc rulesMethod) {
42         this.rulesClass = rulesClass;
43         this.rulesMethod = rulesMethod;
44     }
45     
46     /** Constructor. */
47     public LoaderFromClass(Class JavaDoc rulesClass, String JavaDoc methodName)
48                 throws PluginException {
49
50         Method JavaDoc method = locateMethod(rulesClass, methodName);
51
52         if (method == null) {
53             throw new PluginException(
54                 "rule class " + rulesClass.getName()
55                 + " does not have method " + methodName
56                 + " or that method has an invalid signature.");
57         }
58         
59         this.rulesClass = rulesClass;
60         this.rulesMethod = method;
61     }
62     
63     /**
64      * Just invoke the target method.
65      */

66     public void addRules(Digester d, String JavaDoc path) throws PluginException {
67         Log log = d.getLogger();
68         boolean debug = log.isDebugEnabled();
69         if (debug) {
70             log.debug(
71                 "LoaderFromClass loading rules for plugin at path ["
72                 + path + "]");
73         }
74
75         try {
76             Object JavaDoc[] params = {d, path};
77             Object JavaDoc none = rulesMethod.invoke(null, params);
78         } catch (Exception JavaDoc e) {
79             throw new PluginException(
80                 "Unable to invoke rules method " + rulesMethod
81                 + " on rules class " + rulesClass, e);
82         }
83     }
84     
85     /**
86      * Find a method on the specified class whose name matches methodName,
87      * and whose signature is:
88      * <code> public static void foo(Digester d, String patternPrefix);</code>.
89      *
90      * @return null if no such method exists.
91      */

92     public static Method JavaDoc locateMethod(Class JavaDoc rulesClass, String JavaDoc methodName)
93                             throws PluginException {
94
95         Class JavaDoc[] paramSpec = { Digester.class, String JavaDoc.class };
96         Method JavaDoc rulesMethod = MethodUtils.getAccessibleMethod(
97             rulesClass, methodName, paramSpec);
98             
99         return rulesMethod;
100     }
101 }
102
103
Popular Tags