001 /* $Id: FinderFromDfltMethod.java 992060 2010-09-02 19:09:47Z simonetripodi $ 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 package org.apache.commons.digester.plugins.strategies; 019 020 import java.util.Properties; 021 import java.lang.reflect.Method; 022 023 import org.apache.commons.digester.Digester; 024 import org.apache.commons.digester.plugins.RuleFinder; 025 import org.apache.commons.digester.plugins.RuleLoader; 026 import org.apache.commons.digester.plugins.PluginException; 027 028 /** 029 * A rule-finding algorithm which looks for a method with a specific name 030 * on the plugin class. 031 * 032 * @since 1.6 033 */ 034 035 public class FinderFromDfltMethod extends RuleFinder { 036 public static String DFLT_METHOD_NAME = "addRules"; 037 038 private String methodName; 039 040 /** See {@link #findLoader}. */ 041 public FinderFromDfltMethod() { 042 this(DFLT_METHOD_NAME); 043 } 044 045 /** 046 * Create a rule-finder which invokes a specific method on the plugin 047 * class whenever dynamic rules for a plugin need to be loaded. See the 048 * findRules method for more info. 049 * 050 * @param methodName must be non-null. 051 */ 052 public FinderFromDfltMethod(String methodName) { 053 this.methodName = methodName; 054 } 055 056 /** 057 * If there exists on the plugin class a method with name matching the 058 * constructor's methodName value then locate the appropriate Method on 059 * the plugin class and return an object encapsulating that info. 060 * <p> 061 * If there is no matching method then just return null. 062 * <p> 063 * The returned object (when non-null) will invoke the target method 064 * on the plugin class whenever its addRules method is invoked. The 065 * target method is expected to have the following prototype: 066 * <code> public static void xxxxx(Digester d, String patternPrefix); </code> 067 */ 068 @Override 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