001 /* $Id: FinderFromDfltClass.java 992106 2010-09-02 20:29:25Z 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 org.apache.commons.digester.Digester;
022 import org.apache.commons.digester.plugins.RuleFinder;
023 import org.apache.commons.digester.plugins.RuleLoader;
024 import org.apache.commons.digester.plugins.PluginException;
025
026 /**
027 * A rule-finding algorithm which looks for a method with a specific name
028 * on a class whose name is derived from the plugin class name.
029 *
030 * @since 1.6
031 */
032
033 public class FinderFromDfltClass extends RuleFinder {
034 public static String DFLT_RULECLASS_SUFFIX = "RuleInfo";
035 public static String DFLT_METHOD_NAME = "addRules";
036
037 private String rulesClassSuffix;
038 private String methodName;
039
040 /** See {@link #findLoader}. */
041 public FinderFromDfltClass() {
042 this(DFLT_RULECLASS_SUFFIX, DFLT_METHOD_NAME);
043 }
044
045 /**
046 * Create a rule-finder which invokes a method on a class whenever
047 * dynamic rules for a plugin need to be loaded. See the findRules
048 * method for more info.
049 *
050 * @param rulesClassSuffix must be non-null.
051 * @param methodName may be null.
052 */
053 public FinderFromDfltClass(String rulesClassSuffix, String methodName) {
054 this.rulesClassSuffix = rulesClassSuffix;
055 this.methodName = methodName;
056 }
057
058 /**
059 * If there exists a class whose name is the plugin class name + the
060 * suffix specified to the constructor, then load that class, locate
061 * the appropriate rules-adding method on that class, and return an
062 * object encapsulating that info.
063 * <p>
064 * If there is no such class, then just return null.
065 * <p>
066 * The returned object (when non-null) will invoke the target method
067 * on the selected class whenever its addRules method is invoked. The
068 * target method is expected to have the following prototype:
069 * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
070 */
071 @Override
072 public RuleLoader findLoader(Digester digester, Class<?> pluginClass, Properties p)
073 throws PluginException {
074
075 String rulesClassName = pluginClass.getName() + rulesClassSuffix;
076
077 Class<?> rulesClass = null;
078 try {
079 rulesClass = digester.getClassLoader().loadClass(rulesClassName);
080 } catch(ClassNotFoundException cnfe) {
081 // nope, no rule-info class in the classpath
082 return null;
083 }
084
085 if (methodName == null) {
086 methodName = DFLT_METHOD_NAME;
087 }
088
089 return new LoaderFromClass(rulesClass, methodName);
090 }
091 }
092