1. /* $Id: LoaderFromClass.java,v 1.5 2004/05/10 06:34:01 skitching Exp $
  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. package org.apache.commons.digester.plugins.strategies;
  18. import java.lang.reflect.Method;
  19. import org.apache.commons.digester.Digester;
  20. import org.apache.commons.beanutils.MethodUtils;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.digester.plugins.RuleLoader;
  23. import org.apache.commons.digester.plugins.PluginException;
  24. /**
  25. * A RuleLoader which invokes a static method on a target class, leaving that
  26. * method to actually instantiate and add new rules to a Digester instance.
  27. *
  28. * @since 1.6
  29. */
  30. public class LoaderFromClass extends RuleLoader {
  31. private Class rulesClass;
  32. private Method rulesMethod;
  33. /** Constructor. */
  34. public LoaderFromClass(Class rulesClass, Method rulesMethod) {
  35. this.rulesClass = rulesClass;
  36. this.rulesMethod = rulesMethod;
  37. }
  38. /** Constructor. */
  39. public LoaderFromClass(Class rulesClass, String methodName)
  40. throws PluginException {
  41. Method method = locateMethod(rulesClass, methodName);
  42. if (method == null) {
  43. throw new PluginException(
  44. "rule class " + rulesClass.getName()
  45. + " does not have method " + methodName
  46. + " or that method has an invalid signature.");
  47. }
  48. this.rulesClass = rulesClass;
  49. this.rulesMethod = method;
  50. }
  51. /**
  52. * Just invoke the target method.
  53. */
  54. public void addRules(Digester d, String path) throws PluginException {
  55. Log log = d.getLogger();
  56. boolean debug = log.isDebugEnabled();
  57. if (debug) {
  58. log.debug(
  59. "LoaderFromClass loading rules for plugin at path ["
  60. + path + "]");
  61. }
  62. try {
  63. Object[] params = {d, path};
  64. Object none = rulesMethod.invoke(null, params);
  65. } catch (Exception e) {
  66. throw new PluginException(
  67. "Unable to invoke rules method " + rulesMethod
  68. + " on rules class " + rulesClass, e);
  69. }
  70. }
  71. /**
  72. * Find a method on the specified class whose name matches methodName,
  73. * and whose signature is:
  74. * <code> public static void foo(Digester d, String patternPrefix);</code>.
  75. *
  76. * @return null if no such method exists.
  77. */
  78. public static Method locateMethod(Class rulesClass, String methodName)
  79. throws PluginException {
  80. Class[] paramSpec = { Digester.class, String.class };
  81. Method rulesMethod = MethodUtils.getAccessibleMethod(
  82. rulesClass, methodName, paramSpec);
  83. return rulesMethod;
  84. }
  85. }