From 42d4199104ee00135db43ba0d7b320244c666a3f Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Wed, 6 Feb 2002 12:52:58 +0000 Subject: [PATCH] Made unset property behaviour a config item Allowed Ant1 compat layer to create current Ant1 tasks git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271178 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/ant/antcore/config/AntConfig.java | 54 +++++- .../ant/antcore/config/AntConfigHandler.java | 37 +++- .../execution/ExecutionDataService.java | 19 +- .../ant/antcore/execution/ExecutionFrame.java | 9 +- .../org/apache/tools/ant/Project.java | 181 ++++++++++++------ .../ant1compat/org/apache/tools/ant/Task.java | 8 +- .../cli/org/apache/ant/cli/Commandline.java | 1 - .../java/start/org/apache/ant/start/Main.java | 1 - 8 files changed, 234 insertions(+), 76 deletions(-) diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfig.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfig.java index 18dd2f623..db777bc81 100755 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfig.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfig.java @@ -82,10 +82,22 @@ public class AntConfig { private Map libPaths = new HashMap(); /** Indicates if remote libraries may be used */ - private boolean allowRemoteLibs = false; + private boolean remoteLibs = false; /** Indicates if remote projects may be used */ - private boolean allowRemoteProjects = false; + private boolean remoteProjects = false; + + /** Indicates if unset properties are ignored */ + private boolean unsetProperties = true; + + /** + * Indicate if unset properties are OK. + * + * @return true if unset properties will not cause an exception + */ + public boolean isUnsetPropertiesAllowed() { + return unsetProperties; + } /** * Indicate if the use of remote library's is allowe dby this config. @@ -93,16 +105,16 @@ public class AntConfig { * @return true if this config allows the use of remote libraries, */ public boolean isRemoteLibAllowed() { - return allowRemoteLibs; + return remoteLibs; } /** - * Indicate if this config allows the execution of a remote project + * Indicate if this config allows the execution of a remote project * * @return true if remote projects are allowed */ public boolean isRemoteProjectAllowed() { - return allowRemoteProjects; + return remoteProjects; } /** @@ -140,6 +152,34 @@ public class AntConfig { return libPaths.keySet().iterator(); } + /** + * Allow remote libraries to be used + * + * @param allowRemoteLibs true if remote libraries may be used. + */ + public void allowRemoteLibs(boolean allowRemoteLibs) { + this.remoteLibs = allowRemoteLibs; + } + + /** + * Allow remote projects to be used + * + * @param allowRemoteProjects true if remote projects may be executed. + */ + public void allowRemoteProjects(boolean allowRemoteProjects) { + this.remoteProjects = allowRemoteProjects; + } + + /** + * Allow properties to be used even when they have not been set + * + * @param allowUnsetProperties true if un set properties should not + * cause an exception + */ + public void allowUnsetProperties(boolean allowUnsetProperties) { + this.unsetProperties = allowUnsetProperties; + } + /** * Add an additional set of paths for the given library. * @@ -207,6 +247,10 @@ public class AntConfig { combined.addAll(currentList); libPaths.put(libraryId, combined); } + + remoteLibs = otherConfig.remoteLibs; + remoteProjects = otherConfig.remoteProjects; + unsetProperties = otherConfig.unsetProperties; } /** diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfigHandler.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfigHandler.java index b2c1cdf39..06fc30cb4 100755 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfigHandler.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/config/AntConfigHandler.java @@ -64,6 +64,18 @@ import org.xml.sax.SAXParseException; * @created 20 January 2002 */ public class AntConfigHandler extends ElementHandler { + /** The allowRemoteProject attribute name */ + public final static String REMOTE_PROJECT_ATTR = "allow-remote-project"; + + /** The allowRemoteLibrary attribute name */ + public final static String REMOTE_LIBRARY_ATTR = "allow-remote-library"; + + /** The allowReportProject attribute name */ + public final static String UNSET_PROPS_ATTR = "allow-unset-properties"; + + /** The list of allowed Attributes */ + public final static String[] ALLOWED_ATTRIBUTES + = {REMOTE_PROJECT_ATTR, REMOTE_LIBRARY_ATTR, UNSET_PROPS_ATTR}; /** * The config object which is contructed from the XML representation of * the config @@ -89,8 +101,11 @@ public class AntConfigHandler extends ElementHandler { public void processElement(String elementName) throws SAXParseException { config = new AntConfig(); - } - + config.allowRemoteLibs(getBooleanAttribute(REMOTE_LIBRARY_ATTR)); + config.allowRemoteProjects(getBooleanAttribute(REMOTE_PROJECT_ATTR)); + config.allowUnsetProperties(getBooleanAttribute(UNSET_PROPS_ATTR)); + } + /** * Start a new element in the ant config. * @@ -136,6 +151,24 @@ public class AntConfigHandler extends ElementHandler { } } + /** + * Validate that the given attribute and value are valid. + * + * @param attributeName The name of the attributes + * @param attributeValue The value of the attributes + * @exception SAXParseException if the attribute is not allowed on the + * element. + */ + protected void validateAttribute(String attributeName, + String attributeValue) + throws SAXParseException { + for (int i = 0; i < ALLOWED_ATTRIBUTES.length; ++i) { + if (attributeName.equals(ALLOWED_ATTRIBUTES[i])) { + return; + } + } + throwInvalidAttribute(attributeName); + } } diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionDataService.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionDataService.java index 02e2a45b2..edabad719 100644 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionDataService.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionDataService.java @@ -72,13 +72,20 @@ public class ExecutionDataService implements DataService { /** The ExecutionFrame this service instance is working for */ private ExecutionFrame frame; + /** all properties to be unset without throwing an exception */ + private boolean allowUnsetProperties; + /** * Constructor * * @param frame the frame containing this context + * @param allowUnsetProperties true if the reference to an unset + * property should not throw an exception */ - public ExecutionDataService(ExecutionFrame frame) { + public ExecutionDataService(ExecutionFrame frame, + boolean allowUnsetProperties) { this.frame = frame; + this.allowUnsetProperties = allowUnsetProperties; } /** @@ -169,10 +176,14 @@ public class ExecutionDataService implements DataService { if (fragment == null) { String propertyName = (String)j.next(); if (!isDataValueSet(propertyName)) { - throw new ExecutionException("Property \"" + propertyName - + "\" has not been set"); + if (!allowUnsetProperties) { + throw new ExecutionException("Property \"" + + propertyName + "\" has not been set"); + } + fragment = "${" + propertyName + "}"; + } else { + fragment = getDataValue(propertyName).toString(); } - fragment = getDataValue(propertyName).toString(); } sb.append(fragment); } diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionFrame.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionFrame.java index 5cc8abf7f..4be06ddcc 100755 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionFrame.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionFrame.java @@ -168,9 +168,6 @@ public class ExecutionFrame { this.standardLibs = standardLibs; this.config = config; this.initConfig = initConfig; - - configureServices(); - componentManager.setStandardLibraries(standardLibs); } /** @@ -206,6 +203,9 @@ public class ExecutionFrame { referencedFrames.put(referenceName, referencedFrame); } + + configureServices(); + componentManager.setStandardLibraries(standardLibs); } /** @@ -747,7 +747,8 @@ public class ExecutionFrame { fileService = new ExecutionFileService(this); componentManager = new ComponentManager(this, config.isRemoteLibAllowed()); - dataService = new ExecutionDataService(this); + dataService = new ExecutionDataService(this, + config.isUnsetPropertiesAllowed()); services.put(FileService.class, fileService); services.put(ComponentService.class, componentManager); diff --git a/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Project.java b/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Project.java index 3e3ed5824..2f991d4f4 100644 --- a/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Project.java +++ b/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Project.java @@ -54,17 +54,18 @@ package org.apache.tools.ant; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; import java.util.Hashtable; -import java.util.Map; import java.util.Iterator; +import java.util.Map; +import java.util.Properties; import org.apache.ant.common.antlib.AntContext; import org.apache.ant.common.service.DataService; import org.apache.ant.common.service.FileService; import org.apache.ant.common.util.ExecutionException; -import org.apache.ant.common.util.PropertyUtils; import org.apache.ant.common.util.MessageLevel; -import org.apache.tools.ant.taskdefs.ExecTask; -import org.apache.tools.ant.taskdefs.Java; +import org.apache.ant.common.util.PropertyUtils; import org.apache.tools.ant.types.FilterSet; import org.apache.tools.ant.types.FilterSetCollection; import org.apache.tools.ant.util.FileUtils; @@ -112,6 +113,11 @@ public class Project { /** The java version detected that Ant is running on */ private static String javaVersion; + /** Collection of Ant1 type definitions */ + private Hashtable dataClassDefinitions = new Hashtable(); + /** Collection of Ant1 task definitions */ + private Hashtable taskClassDefinitions = new Hashtable(); + /** The project description */ private String description; @@ -356,6 +362,53 @@ public class Project { return getClass().getClassLoader(); } + /** + * get a copy of the property hashtable + * + * @return the hashtable containing all properties, user included + */ + public Hashtable getProperties() { + Map properties = dataService.getAllProperties(); + Hashtable result = new Hashtable(); + for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) { + String name = (String)i.next(); + Object value = properties.get(name); + if (value instanceof String) { + result.put(name, value); + } + } + + return result; + } + + /** + * get a copy of the property hashtable + * + * @return the hashtable containing all properties, user included + */ + public Hashtable getUserProperties() { + return getProperties(); + } + + /** + * Get all references in the project + * + * @return the hashtable containing all references + */ + public Hashtable getReferences() { + Map properties = dataService.getAllProperties(); + Hashtable result = new Hashtable(); + for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) { + String name = (String)i.next(); + Object value = properties.get(name); + if (!(value instanceof String)) { + result.put(name, value); + } + } + + return result; + } + /** * Add a reference to an object. NOte that in Ant2 objects and * properties occupy the same namespace. @@ -524,6 +577,66 @@ public class Project { this.context = context; fileService = (FileService)context.getCoreService(FileService.class); dataService = (DataService)context.getCoreService(DataService.class); + + String defs = "/org/apache/tools/ant/taskdefs/defaults.properties"; + + try { + Properties props = new Properties(); + InputStream in = this.getClass().getResourceAsStream(defs); + if (in == null) { + throw new BuildException("Can't load default task list"); + } + props.load(in); + in.close(); + + Enumeration enum = props.propertyNames(); + while (enum.hasMoreElements()) { + String key = (String)enum.nextElement(); + String value = props.getProperty(key); + try { + Class taskClass = Class.forName(value); + taskClassDefinitions.put(key, taskClass); + } catch (NoClassDefFoundError ncdfe) { + log("Could not load a dependent class (" + + ncdfe.getMessage() + ") for task " + key, MSG_DEBUG); + } catch (ClassNotFoundException cnfe) { + log("Could not load class (" + value + + ") for task " + key, MSG_DEBUG); + } + } + } 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); + if (in == null) { + throw new BuildException("Can't load default datatype list"); + } + props.load(in); + in.close(); + + Enumeration enum = props.propertyNames(); + while (enum.hasMoreElements()) { + String key = (String)enum.nextElement(); + String value = props.getProperty(key); + try { + Class dataClass = Class.forName(value); + dataClassDefinitions.put(key, dataClass); + } catch (NoClassDefFoundError ncdfe) { + log("Could not load a dependent class (" + + ncdfe.getMessage() + ") for type " + key, MSG_DEBUG); + } catch (ClassNotFoundException cnfe) { + log("Could not load class (" + value + + ") for type " + key, MSG_DEBUG); + } + } + } catch (IOException ioe) { + throw new BuildException("Can't load default datatype list"); + } } /** @@ -600,70 +713,26 @@ public class Project { * Create a Task. This faced hard codes a few well known tasks at this * time * - * @param taskName the name of the task to be created. + * @param taskType the name of the task to be created. * @return the created task instance */ - public Task createTask(String taskName) { + public Task createTask(String taskType) { // we piggy back the task onto the current context Task task = null; - if (taskName.equals("java")) { - task = new Java(); - } else if (taskName.equals("exec")) { - task = new ExecTask(); - } else { + Class c = (Class) taskClassDefinitions.get(taskType); + + if (c == null) { return null; } + try { + task = (Task)c.newInstance(); task.setProject(this); task.init(context); return task; - } catch (ExecutionException e) { + } catch (Throwable e) { throw new BuildException(e); } } - - /** - * get a copy of the property hashtable - * @return the hashtable containing all properties, user included - */ - public Hashtable getProperties() { - Map properties = dataService.getAllProperties(); - Hashtable result = new Hashtable(); - for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) { - String name = (String)i.next(); - Object value = properties.get(name); - if (value instanceof String) { - result.put(name, value); - } - } - - return result; - } - - /** - * get a copy of the property hashtable - * @return the hashtable containing all properties, user included - */ - public Hashtable getUserProperties() { - return getProperties(); - } - - /** - * Get all references in the project - * @return the hashtable containing all references - */ - public Hashtable getReferences() { - Map properties = dataService.getAllProperties(); - Hashtable result = new Hashtable(); - for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) { - String name = (String)i.next(); - Object value = properties.get(name); - if (!(value instanceof String)) { - result.put(name, value); - } - } - - return result; - } } diff --git a/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Task.java b/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Task.java index 398020aed..70ceace6c 100644 --- a/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Task.java +++ b/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Task.java @@ -83,9 +83,11 @@ public abstract class Task extends ProjectComponent public void init(AntContext context) throws ExecutionException { super.init(context); - BuildElement buildElement = (BuildElement)context.getModelElement(); - taskType = buildElement.getType(); - taskName = taskType; + if (context.getModelElement() instanceof BuildElement) { + BuildElement buildElement = (BuildElement)context.getModelElement(); + taskType = buildElement.getType(); + taskName = taskType; + } } /** diff --git a/proposal/mutant/src/java/cli/org/apache/ant/cli/Commandline.java b/proposal/mutant/src/java/cli/org/apache/ant/cli/Commandline.java index 965397309..c54daee59 100755 --- a/proposal/mutant/src/java/cli/org/apache/ant/cli/Commandline.java +++ b/proposal/mutant/src/java/cli/org/apache/ant/cli/Commandline.java @@ -260,7 +260,6 @@ public class Commandline { */ private void process(String[] args, InitConfig initConfig) { this.initConfig = initConfig; - System.out.println("Ant Home is " + initConfig.getAntHome()); try { parseArguments(args); diff --git a/proposal/mutant/src/java/start/org/apache/ant/start/Main.java b/proposal/mutant/src/java/start/org/apache/ant/start/Main.java index 511d44504..4ddf6bbc5 100755 --- a/proposal/mutant/src/java/start/org/apache/ant/start/Main.java +++ b/proposal/mutant/src/java/start/org/apache/ant/start/Main.java @@ -196,7 +196,6 @@ public class Main { InitConfig config = new InitConfig(); URL libraryURL = getLibraryURL(); - System.out.println("Library URL is " + libraryURL); config.setLibraryURL(libraryURL); URL antHome = getAntHome();