/* * 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.tools.ant; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import org.apache.tools.ant.property.LocalProperties; /** * Class to implement a target object with required parameters. * *
If you are creating Targets programmatically, make sure you set * the Location to a useful value. In particular all targets should * have different location values.
*/ public class Target implements TaskContainer { /** Name of this target. */ private String name; /** The "if" condition to test on execution. */ private String ifCondition = ""; /** The "unless" condition to test on execution. */ private String unlessCondition = ""; /** List of targets this target is dependent on. */ private List/*null.
*/
public void setProject(Project project) {
this.project = project;
}
/**
* Returns the project this target belongs to.
*
* @return The project this target belongs to, or null if
* the project has not been set yet.
*/
public Project getProject() {
return project;
}
/**
* Sets the location of this target's definition.
*
* @param location Location
* @since 1.6.2
*/
public void setLocation(Location location) {
this.location = location;
}
/**
* Get the location of this target's definition.
*
* @return Location
* @since 1.6.2
*/
public Location getLocation() {
return location;
}
/**
* Sets the list of targets this target is dependent on.
* The targets themselves are not resolved at this time.
*
* @param depS A comma-separated list of targets this target
* depends on. Must not be null.
*/
public void setDepends(String depS) {
for (Iterator iter = parseDepends(depS, getName(), "depends").iterator();
iter.hasNext(); ) {
addDependency((String) iter.next());
}
}
public static Listnull.
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the name of this target.
*
* @return the name of this target, or null if the
* name has not been set yet.
*/
public String getName() {
return name;
}
/**
* Adds a task to this target.
*
* @param task The task to be added. Must not be null.
*/
public void addTask(Task task) {
children.add(task);
}
/**
* Adds the wrapper for a data type element to this target.
*
* @param r The wrapper for the data type element to be added.
* Must not be null.
*/
public void addDataType(RuntimeConfigurable r) {
children.add(r);
}
/**
* Returns the current set of tasks to be executed by this target.
*
* @return an array of the tasks currently within this target
*/
public Task[] getTasks() {
List tasks = new ArrayList(children.size());
Iterator it = children.iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Task) {
tasks.add(o);
}
}
return (Task[]) tasks.toArray(new Task[tasks.size()]);
}
/**
* Adds a dependency to this target.
*
* @param dependency The name of a target this target is dependent on.
* Must not be null.
*/
public void addDependency(String dependency) {
if (dependencies == null) {
dependencies = new ArrayList(2);
}
dependencies.add(dependency);
}
/**
* Returns an enumeration of the dependencies of this target.
*
* @return an enumeration of the dependencies of this target (enumeration of String)
*/
public Enumeration getDependencies() {
return Collections
.enumeration(dependencies == null ? Collections.EMPTY_LIST : dependencies);
}
/**
* Does this target depend on the named target?
* @param other the other named target.
* @return true if the target does depend on the named target
* @since Ant 1.6
*/
public boolean dependsOn(String other) {
Project p = getProject();
Hashtable t = p == null ? null : p.getTargets();
return p != null && p.topoSort(getName(), t, false).contains(t.get(other));
}
/**
* Sets the "if" condition to test on execution. This is the
* name of a property to test for existence - if the property
* is not set, the task will not execute. The property goes
* through property substitution once before testing, so if
* property foo has value bar, setting
* the "if" condition to ${foo}_x will mean that the
* task will only execute if property bar_x is set.
*
* @param property The property condition to test on execution.
* May be null, in which case
* no "if" test is performed.
*/
public void setIf(String property) {
ifCondition = property == null ? "" : property;
}
/**
* Returns the "if" property condition of this target.
*
* @return the "if" property condition or null if no
* "if" condition had been defined.
* @since 1.6.2
*/
public String getIf() {
return "".equals(ifCondition) ? null : ifCondition;
}
/**
* Sets the "unless" condition to test on execution. This is the
* name of a property to test for existence - if the property
* is set, the task will not execute. The property goes
* through property substitution once before testing, so if
* property foo has value bar, setting
* the "unless" condition to ${foo}_x will mean that the
* task will only execute if property bar_x isn't set.
*
* @param property The property condition to test on execution.
* May be null, in which case
* no "unless" test is performed.
*/
public void setUnless(String property) {
unlessCondition = property == null ? "" : property;
}
/**
* Returns the "unless" property condition of this target.
*
* @return the "unless" property condition or null
* if no "unless" condition had been defined.
* @since 1.6.2
*/
public String getUnless() {
return "".equals(unlessCondition) ? null : unlessCondition;
}
/**
* Sets the description of this target.
*
* @param description The description for this target.
* May be null, indicating that no
* description is available.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Returns the description of this target.
*
* @return the description of this target, or null if no
* description is available.
*/
public String getDescription() {
return description;
}
/**
* Returns the name of this target.
*
* @return the name of this target, or null if the
* name has not been set yet.
*/
public String toString() {
return name;
}
/**
* Executes the target if the "if" and "unless" conditions are
* satisfied. Dependency checking should be done before calling this
* method, as it does no checking of its own. If either the "if"
* or "unless" test prevents this target from being executed, a verbose
* message is logged giving the reason. It is recommended that clients
* of this class call performTasks rather than this method so that
* appropriate build events are fired.
*
* @exception BuildException if any of the tasks fail or if a data type
* configuration fails.
*
* @see #performTasks()
* @see #setIf(String)
* @see #setUnless(String)
*/
public void execute() throws BuildException {
if (!testIfAllows()) {
project.log(this, "Skipped because property '" + project.replaceProperties(ifCondition)
+ "' not set.", Project.MSG_VERBOSE);
return;
}
if (!testUnlessAllows()) {
project.log(this, "Skipped because property '"
+ project.replaceProperties(unlessCondition) + "' set.", Project.MSG_VERBOSE);
return;
}
LocalProperties localProperties = LocalProperties.get(getProject());
localProperties.enterScope();
try {
// use index-based approach to avoid ConcurrentModificationExceptions;
// also account for growing target children
// do not optimize this loop by replacing children.size() by a variable
// as children can be added dynamically as in RhinoScriptTest where a target is adding work for itself
for (int i = 0; i < children.size(); i++) {
Object o = children.get(i);
if (o instanceof Task) {
Task task = (Task) o;
task.perform();
} else {
((RuntimeConfigurable) o).maybeConfigure(project);
}
}
} finally {
localProperties.exitScope();
}
}
/**
* Performs the tasks within this target (if the conditions are met),
* firing target started/target finished messages around a call to
* execute.
*
* @see #execute()
*/
public final void performTasks() {
RuntimeException thrown = null;
project.fireTargetStarted(this);
try {
execute();
} catch (RuntimeException exc) {
thrown = exc;
throw exc;
} finally {
project.fireTargetFinished(this, thrown);
}
}
/**
* Replaces all occurrences of the given task in the list
* of children with the replacement data type wrapper.
*
* @param el The task to replace.
* Must not be null.
* @param o The data type wrapper to replace el with.
*/
void replaceChild(Task el, RuntimeConfigurable o) {
int index;
while ((index = children.indexOf(el)) >= 0) {
children.set(index, o);
}
}
/**
* Replaces all occurrences of the given task in the list
* of children with the replacement task.
*
* @param el The task to replace.
* Must not be null.
* @param o The task to replace el with.
*/
void replaceChild(Task el, Task o) {
int index;
while ((index = children.indexOf(el)) >= 0) {
children.set(index, o);
}
}
/**
* Tests whether or not the "if" condition allows the execution of this target.
*
* @return whether or not the "if" condition is satisfied. If no
* condition (or an empty condition) has been set,
* true is returned.
*
* @see #setIf(String)
*/
private boolean testIfAllows() {
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject());
Object o = propertyHelper.parseProperties(ifCondition);
return propertyHelper.testIfCondition(o);
}
/**
* Tests whether or not the "unless" condition allows the execution of this target.
*
* @return whether or not the "unless" condition is satisfied. If no
* condition (or an empty condition) has been set,
* true is returned.
*
* @see #setUnless(String)
*/
private boolean testUnlessAllows() {
PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(getProject());
Object o = propertyHelper.parseProperties(unlessCondition);
return propertyHelper.testUnlessCondition(o);
}
}