1. /* $Id: FinderFromClass.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 expects the caller to specify a classname and
  25. * methodname as plugin properties.
  26. *
  27. * @since 1.6
  28. */
  29. public class FinderFromClass extends RuleFinder {
  30. public static String DFLT_RULECLASS_ATTR = "ruleclass";
  31. public static String DFLT_METHOD_ATTR = "method";
  32. public static String DFLT_METHOD_NAME = "addRules";
  33. private String ruleClassAttr;
  34. private String methodAttr;
  35. private String dfltMethodName;
  36. /**
  37. * See {@link #findLoader}.
  38. */
  39. public FinderFromClass() {
  40. this(DFLT_RULECLASS_ATTR, DFLT_METHOD_ATTR, DFLT_METHOD_NAME);
  41. }
  42. /**
  43. * Create a rule-finder which invokes a user-specified method on a
  44. * user-specified class whenever dynamic rules for a plugin need to be
  45. * loaded. See the findRules method for more info.
  46. *
  47. * @param ruleClassAttr must be non-null.
  48. * @param methodAttr may be null.
  49. * @param dfltMethodName may be null.
  50. */
  51. public FinderFromClass(String ruleClassAttr, String methodAttr,
  52. String dfltMethodName) {
  53. this.ruleClassAttr = ruleClassAttr;
  54. this.methodAttr = methodAttr;
  55. this.dfltMethodName = dfltMethodName;
  56. }
  57. /**
  58. * If there exists a property with the name matching constructor param
  59. * ruleClassAttr, then load the specified class, locate the appropriate
  60. * rules-adding method on that class, and return an object encapsulating
  61. * that info.
  62. * <p>
  63. * If there is no matching property provided, then just return null.
  64. * <p>
  65. * The returned object (when non-null) will invoke the target method
  66. * on the selected class whenever its addRules method is invoked. The
  67. * target method is expected to have the following prototype:
  68. * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
  69. * <p>
  70. * The target method can be specified in several ways. If this object's
  71. * constructor was passed a non-null methodAttr parameter, and the
  72. * properties defines a value with that key, then that is taken as the
  73. * target method name. If there is no matching property, or the constructor
  74. * was passed null for methodAttr, then the dfltMethodName passed to the
  75. * constructor is used as the name of the method on the target class. And
  76. * if that was null, then DFLT_METHOD_NAME will be used.
  77. * <p>
  78. * When the user explicitly declares a plugin in the input xml, the
  79. * xml attributes on the declaration tag are passed here as properties,
  80. * so the user can select any class in the classpath (and any method on
  81. * that class provided it has the correct prototype) as the source of
  82. * dynamic rules for the plugged-in class.
  83. */
  84. public RuleLoader findLoader(Digester digester, Class pluginClass,
  85. Properties p) throws PluginException {
  86. String ruleClassName = p.getProperty(ruleClassAttr);
  87. if (ruleClassName == null) {
  88. // nope, user hasn't requested dynamic rules to be loaded
  89. // from a specific class.
  90. return null;
  91. }
  92. // ok, we are in business
  93. String methodName = null;
  94. if (methodAttr != null) {
  95. methodName = p.getProperty(methodAttr);
  96. }
  97. if (methodName == null) {
  98. methodName = dfltMethodName;
  99. }
  100. if (methodName == null) {
  101. methodName = DFLT_METHOD_NAME;
  102. }
  103. Class ruleClass;
  104. try {
  105. // load the plugin class object
  106. ruleClass =
  107. digester.getClassLoader().loadClass(ruleClassName);
  108. } catch(ClassNotFoundException cnfe) {
  109. throw new PluginException(
  110. "Unable to load class " + ruleClassName, cnfe);
  111. }
  112. return new LoaderFromClass(ruleClass, methodName);
  113. }
  114. }