1 17 18 package org.apache.commons.digester.plugins.strategies; 19 20 import java.lang.reflect.Method ; 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 34 35 public class LoaderFromClass extends RuleLoader { 36 37 private Class rulesClass; 38 private Method rulesMethod; 39 40 41 public LoaderFromClass(Class rulesClass, Method rulesMethod) { 42 this.rulesClass = rulesClass; 43 this.rulesMethod = rulesMethod; 44 } 45 46 47 public LoaderFromClass(Class rulesClass, String methodName) 48 throws PluginException { 49 50 Method 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 66 public void addRules(Digester d, String 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 [] params = {d, path}; 77 Object none = rulesMethod.invoke(null, params); 78 } catch (Exception e) { 79 throw new PluginException( 80 "Unable to invoke rules method " + rulesMethod 81 + " on rules class " + rulesClass, e); 82 } 83 } 84 85 92 public static Method locateMethod(Class rulesClass, String methodName) 93 throws PluginException { 94 95 Class [] paramSpec = { Digester.class, String .class }; 96 Method rulesMethod = MethodUtils.getAccessibleMethod( 97 rulesClass, methodName, paramSpec); 98 99 return rulesMethod; 100 } 101 } 102 103 | Popular Tags |