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