ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Sunde" <msu...@actional.com>
Subject RE: BeanShellDef - java.lang.LinkageError: duplicate class definition: AList
Date Fri, 23 Jan 2004 19:53:18 GMT
My attachement got removed so here it is:

beanshelldef.java
=======================================
import java.io.File;
import java.util.Set;
import java.util.HashSet;

import bsh.EvalError;
import bsh.Interpreter;

import org.apache.tools.ant.AntTypeDefinition;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ComponentHelper;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.taskdefs.AntlibDefinition;

/**
 * Class to define an ant definition using a beanshell script.
 * The beanshell interpreter must be 2.0 or higher.
 * The beanshell script must contain the class specified
 * by the "classname" attribute.
 * <p>
 * Note that if there is anything incorrect with the script
 * the warning message is quite cryptic.
 * </p>
 *
 * @author  Peter Reilly
 * @since   Ant 1.7
 * @see AntlibDefinition
 */
public class BeanShellDef extends AntlibDefinition {

    private String name;
    private String definitionString;
    private File   definitionFile;
    private String classname;

    /** Only define the types once so that we don't end up with
    * duplicate class definition errors. */
    static private Set definedTypes = new HashSet();

    /**
     * Name of the definition
     * @param name the name of the definition
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Name of the classname that the bean shell
     * definition contains.
     * @param classname the name of the class defined in the beanshell script
     */
    public void setClassname(String classname) {
        this.classname = classname;
    }

    /**
     * Set the definition file.
     * @param definitionFile a file containing beanshell script
     */
    public void setFile(File definitionFile) {
        this.definitionFile = definitionFile;
    }

    /**
     * Set the definition string.
     *
     * @param text a bean shell 2.0 string containing the definition
     *             of the classname
     */
    public void addText(String text) {
        this.definitionString = getProject().replaceProperties(text);
    }

    /**
     * define the beanshell definition.
     * check the attributes, get the class been defined and
     * set the definition.
     */
    public void execute() {
        if (classname == null) {
            throw new BuildException("Missing attribute classname");
        }
        if (name == null) {
            throw new BuildException("Missing attribute name");
        }
        if (definitionString == null && definitionFile == null) {
            throw new BuildException("Missing beanshell script or file");
        }

        synchronized (definedTypes)
        {
            if (!definedTypes.contains(classname))
            {
                definedTypes.add(classname);
                registerType();
            }
            else
            {
                log("Override ignored for type: " + classname,
                        Project.MSG_VERBOSE);
            }
        }
    }

    /**
     * define the beanshell definition.
     * check the attributes, get the class been defined and
     * set the definition. This method should only be called,
     * once otherwise we end up with a duplicate class
     * definition error.
     */
    protected void registerType() {
        Interpreter i = new Interpreter();  // Construct an interpreter
        if (definitionFile != null) {
            try {
                i.source(definitionFile.getAbsolutePath());
            } catch (Throwable t) {
                throw new BuildException(t);
            }
        }
        try {
            if (definitionString != null) {
                i.eval(definitionString);
            }
            Object o = i.eval("new " + classname + "()");
            Class cl = o.getClass();
            AntTypeDefinition def = new AntTypeDefinition();
            def.setName(ProjectHelper.genComponentName(getURI(), name));
            def.setClassName(classname);
            def.setClass(cl);
            ComponentHelper.getComponentHelper(getProject())
                .addDataTypeDefinition(def);
        } catch (BuildException ex) {
            throw ex;
        } catch (Throwable t) {
            throw new BuildException(t);
        }
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message