From 46b4505bc0c79ea737488639bf5031f01669c28a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 4 Sep 2000 12:20:12 +0000 Subject: [PATCH] Added infrastructure to create data types independent of tasks. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267950 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 1 + src/main/org/apache/tools/ant/Project.java | 90 ++++++++++++++++-- .../tools/ant/types/defaults.properties | 3 + .../org/apache/tools/ant/AllJUnitTests.java | 1 + .../org/apache/tools/ant/ProjectTest.java | 91 +++++++++++++++++++ 5 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 src/main/org/apache/tools/ant/types/defaults.properties create mode 100644 src/testcases/org/apache/tools/ant/ProjectTest.java diff --git a/build.xml b/build.xml index 9950d5b6e..6ad623a0f 100644 --- a/build.xml +++ b/build.xml @@ -285,6 +285,7 @@ + diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index fd6e0d1bb..94e60f0a3 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -98,6 +98,7 @@ public class Project { private Hashtable userProperties = new Hashtable(); private Hashtable references = new Hashtable(); private String defaultTarget; + private Hashtable dataClassDefinitions = new Hashtable(); private Hashtable taskClassDefinitions = new Hashtable(); private Hashtable targets = new Hashtable(); private Hashtable filters = new Hashtable(); @@ -160,16 +161,41 @@ public class Project { // ignore... } } + } catch (IOException ioe) { + throw new BuildException("Can't load default task list"); + } + + String dataDefs = "/org/apache/tools/ant/types/defaults.properties"; + + try{ + Properties props = new Properties(); + InputStream in = this.getClass().getResourceAsStream(dataDefs); + props.load(in); + in.close(); - Properties systemP = System.getProperties(); - Enumeration e = systemP.keys(); - while (e.hasMoreElements()) { - String name = (String) e.nextElement(); - String value = (String) systemP.get(name); - this.setProperty(name, value); + Enumeration enum = props.propertyNames(); + while (enum.hasMoreElements()) { + String key = (String) enum.nextElement(); + String value = props.getProperty(key); + try { + Class dataClass = Class.forName(value); + addDataTypeDefinition(key, dataClass); + } catch (NoClassDefFoundError ncdfe) { + // ignore... + } catch (ClassNotFoundException cnfe) { + // ignore... + } } } catch (IOException ioe) { - throw new BuildException("Can't load default task list"); + throw new BuildException("Can't load default datatype list"); + } + + Properties systemP = System.getProperties(); + Enumeration e = systemP.keys(); + while (e.hasMoreElements()) { + String name = (String) e.nextElement(); + String value = (String) systemP.get(name); + this.setProperty(name, value); } } @@ -326,6 +352,16 @@ public class Project { return taskClassDefinitions; } + public void addDataTypeDefinition(String typeName, Class typeClass) { + String msg = " +User datatype: " + typeName + " " + typeClass.getName(); + log(msg, MSG_VERBOSE); + dataClassDefinitions.put(typeName, typeClass); + } + + public Hashtable getDataTypeDefinitions() { + return dataClassDefinitions; + } + /** * This call expects to add a new Target. * @param target is the Target to be added to the current @@ -418,6 +454,46 @@ public class Project { } } + public Object createDataType(String typeName) throws BuildException { + Class c = (Class) dataClassDefinitions.get(typeName); + + if (c == null) + return null; + + try { + java.lang.reflect.Constructor ctor = null; + boolean noArg = false; + // DataType can have a "no arg" constructor or take a single + // Project argument. + try { + ctor = c.getConstructor(new Class[0]); + noArg = true; + } catch (NoSuchMethodException nse) { + ctor = c.getConstructor(new Class[] {getClass()}); + noArg = false; + } + + Object o = null; + if (noArg) { + o = ctor.newInstance(new Object[0]); + } else { + o = ctor.newInstance(new Object[] {this}); + } + String msg = " +DataType: " + typeName; + log (msg, MSG_VERBOSE); + return o; + } catch (java.lang.reflect.InvocationTargetException ite) { + Throwable t = ite.getTargetException(); + String msg = "Could not create datatype of type: " + + typeName + " due to " + t; + throw new BuildException(msg, t); + } catch (Throwable t) { + String msg = "Could not create datatype of type: " + + typeName + " due to " + t; + throw new BuildException(msg, t); + } + } + public void executeTargets(Vector targetNames) throws BuildException { Throwable error = null; diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties new file mode 100644 index 000000000..48dd09a47 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -0,0 +1,3 @@ +path=org.apache.tools.ant.types.Path +fileset=org.apache.tools.ant.types.FileSet +patternset=org.apache.tools.ant.types.PatternSet diff --git a/src/testcases/org/apache/tools/ant/AllJUnitTests.java b/src/testcases/org/apache/tools/ant/AllJUnitTests.java index e0496eb27..87f569a03 100644 --- a/src/testcases/org/apache/tools/ant/AllJUnitTests.java +++ b/src/testcases/org/apache/tools/ant/AllJUnitTests.java @@ -71,6 +71,7 @@ public class AllJUnitTests extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(IntrospectionHelperTest.class); + suite.addTest(new TestSuite(org.apache.tools.ant.ProjectTest.class)); suite.addTest(org.apache.tools.ant.types.AllJUnitTests.suite()); return suite; } diff --git a/src/testcases/org/apache/tools/ant/ProjectTest.java b/src/testcases/org/apache/tools/ant/ProjectTest.java new file mode 100644 index 000000000..0e558f589 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/ProjectTest.java @@ -0,0 +1,91 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant; + +import org.apache.tools.ant.types.*; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Very limited test class for Project. Waiting to be extended. + * + * @author Stefan Bodewig stefan.bodewig@megabit.net + */ +public class ProjectTest extends TestCase { + + private Project p; + + public ProjectTest(String name) { + super(name); + } + + public void setUp() { + p = new Project(); + p.init(); + } + + public void testDataTypes() throws BuildException { + assertNull("dummy is not a known data type", + p.createDataType("dummy")); + Object o = p.createDataType("fileset"); + assertNotNull("fileset is a known type", o); + assert("fileset creates FileSet", o instanceof FileSet); + assert("PatternSet", + p.createDataType("patternset") instanceof PatternSet); + assert("Path", p.createDataType("path") instanceof Path); + } +}