001 /* $Id: FinderFromDfltMethod.java 729125 2008-12-23 21:21:07Z rahul $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.commons.digester.plugins.strategies;
020
021 import java.util.Properties;
022 import java.lang.reflect.Method;
023
024 import org.apache.commons.digester.Digester;
025 import org.apache.commons.digester.plugins.RuleFinder;
026 import org.apache.commons.digester.plugins.RuleLoader;
027 import org.apache.commons.digester.plugins.PluginException;
028
029 /**
030 * A rule-finding algorithm which looks for a method with a specific name
031 * on the plugin class.
032 *
033 * @since 1.6
034 */
035
036 public class FinderFromDfltMethod extends RuleFinder {
037 public static String DFLT_METHOD_NAME = "addRules";
038
039 private String methodName;
040
041 /** See {@link #findLoader}. */
042 public FinderFromDfltMethod() {
043 this(DFLT_METHOD_NAME);
044 }
045
046 /**
047 * Create a rule-finder which invokes a specific method on the plugin
048 * class whenever dynamic rules for a plugin need to be loaded. See the
049 * findRules method for more info.
050 *
051 * @param methodName must be non-null.
052 */
053 public FinderFromDfltMethod(String methodName) {
054 this.methodName = methodName;
055 }
056
057 /**
058 * If there exists on the plugin class a method with name matching the
059 * constructor's methodName value then locate the appropriate Method on
060 * the plugin class and return an object encapsulating that info.
061 * <p>
062 * If there is no matching method then just return null.
063 * <p>
064 * The returned object (when non-null) will invoke the target method
065 * on the plugin class whenever its addRules method is invoked. The
066 * target method is expected to have the following prototype:
067 * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
068 */
069 public RuleLoader findLoader(Digester d, Class<?> pluginClass, Properties p)
070 throws PluginException {
071
072 Method rulesMethod = LoaderFromClass.locateMethod(pluginClass, methodName);
073 if (rulesMethod == null) {
074 return null;
075 }
076
077 return new LoaderFromClass(pluginClass, rulesMethod);
078 }
079 }
080