1. /* $Id: FinderFromDfltClass.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.util.Properties;
  19. import org.apache.commons.digester.Digester;
  20. import org.apache.commons.digester.plugins.RuleFinder;
  21. import org.apache.commons.digester.plugins.RuleLoader;
  22. import org.apache.commons.digester.plugins.PluginException;
  23. /**
  24. * A rule-finding algorithm which looks for a method with a specific name
  25. * on a class whose name is derived from the plugin class name.
  26. *
  27. * @since 1.6
  28. */
  29. public class FinderFromDfltClass extends RuleFinder {
  30. public static String DFLT_RULECLASS_SUFFIX = "RuleInfo";
  31. public static String DFLT_METHOD_NAME = "addRules";
  32. private String rulesClassSuffix;
  33. private String methodName;
  34. /** See {@link #findLoader}. */
  35. public FinderFromDfltClass() {
  36. this(DFLT_RULECLASS_SUFFIX, DFLT_METHOD_NAME);
  37. }
  38. /**
  39. * Create a rule-finder which invokes a method on a class whenever
  40. * dynamic rules for a plugin need to be loaded. See the findRules
  41. * method for more info.
  42. *
  43. * @param rulesClassSuffix must be non-null.
  44. * @param methodName may be null.
  45. */
  46. public FinderFromDfltClass(String rulesClassSuffix, String methodName) {
  47. this.rulesClassSuffix = rulesClassSuffix;
  48. this.methodName = methodName;
  49. }
  50. /**
  51. * If there exists a class whose name is the plugin class name + the
  52. * suffix specified to the constructor, then load that class, locate
  53. * the appropriate rules-adding method on that class, and return an
  54. * object encapsulating that info.
  55. * <p>
  56. * If there is no such class, then just return null.
  57. * <p>
  58. * The returned object (when non-null) will invoke the target method
  59. * on the selected class whenever its addRules method is invoked. The
  60. * target method is expected to have the following prototype:
  61. * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
  62. */
  63. public RuleLoader findLoader(Digester digester, Class pluginClass, Properties p)
  64. throws PluginException {
  65. String rulesClassName = pluginClass.getName() + rulesClassSuffix;
  66. Class rulesClass = null;
  67. try {
  68. rulesClass = digester.getClassLoader().loadClass(rulesClassName);
  69. } catch(ClassNotFoundException cnfe) {
  70. // ok, ignore
  71. }
  72. if (rulesClass == null) {
  73. // nope, no rule-info class in the classpath
  74. return null;
  75. }
  76. if (methodName == null) {
  77. methodName = DFLT_METHOD_NAME;
  78. }
  79. return new LoaderFromClass(rulesClass, methodName);
  80. }
  81. }