alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Digester example source code file (LoaderFromClass.java)

This example Commons Digester source code file (LoaderFromClass.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Digester tags/keywords

class, loaderfromclass, loaderfromclass, log, method, method, object, override, pluginexception, pluginexception, reflection, ruleloader, string, string, unable

The Commons Digester LoaderFromClass.java source code

/* $Id: LoaderFromClass.java 992060 2010-09-02 19:09:47Z simonetripodi $
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.digester.plugins.strategies;

import java.lang.reflect.Method;

import org.apache.commons.digester.Digester;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.digester.plugins.RuleLoader;
import org.apache.commons.digester.plugins.PluginException;

/**
 * A RuleLoader which invokes a static method on a target class, leaving that
 * method to actually instantiate and add new rules to a Digester instance.
 *
 * @since 1.6
 */

public class LoaderFromClass extends RuleLoader {
    
    private Class<?> rulesClass;
    private Method rulesMethod;
    
    /** Constructor. */
    public LoaderFromClass(Class<?> rulesClass, Method rulesMethod) {
        this.rulesClass = rulesClass;
        this.rulesMethod = rulesMethod;
    }
    
    /** Constructor. */
    public LoaderFromClass(Class<?> rulesClass, String methodName)
                throws PluginException {

        Method method = locateMethod(rulesClass, methodName);

        if (method == null) {
            throw new PluginException(
                "rule class " + rulesClass.getName()
                + " does not have method " + methodName
                + " or that method has an invalid signature.");
        }
        
        this.rulesClass = rulesClass;
        this.rulesMethod = method;        
    }
    
    /**
     * Just invoke the target method.
     */
    @Override
    public void addRules(Digester d, String path) throws PluginException {
        Log log = d.getLogger();
        boolean debug = log.isDebugEnabled();
        if (debug) {
            log.debug(
                "LoaderFromClass loading rules for plugin at path [" 
                + path + "]");
        }

        try {
            Object[] params = {d, path};
            rulesMethod.invoke(null, params);
        } catch (Exception e) {
            throw new PluginException(
                "Unable to invoke rules method " + rulesMethod
                + " on rules class " + rulesClass, e);
        } 
    }
    
    /**
     * Find a method on the specified class whose name matches methodName,
     * and whose signature is:
     * <code> public static void foo(Digester d, String patternPrefix);.
     *
     * @return null if no such method exists.
     */
    public static Method locateMethod(Class<?> rulesClass, String methodName) 
                            throws PluginException {

        Class<?>[] paramSpec = { Digester.class, String.class };
        Method rulesMethod = MethodUtils.getAccessibleMethod(
            rulesClass, methodName, paramSpec);
            
        return rulesMethod;
    }
}

Other Commons Digester examples (source code examples)

Here is a short list of links related to this Commons Digester LoaderFromClass.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.