diff --git a/WHATSNEW b/WHATSNEW index ae3b1c71f..8076df1f6 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -39,6 +39,11 @@ Other changes: finishing threads might fail. Bugzilla Report 62148 + * has a new attribute runtime which can be used to set + properties with values taken as snapshots from the + availableProcessors, freeMemory, maxMemory and totalMemory methods + of the Java Runtime class. + Changes from Ant 1.10.2 TO Ant 1.10.3 ===================================== diff --git a/manual/Tasks/property.html b/manual/Tasks/property.html index ed2c7846e..199bf786e 100644 --- a/manual/Tasks/property.html +++ b/manual/Tasks/property.html @@ -49,6 +49,13 @@ they are most definitely not variables.

  • By setting the environment attribute with a prefix to use. Properties will be defined for every environment variable by prefixing the supplied name and a period to the name of the variable.
  • +
  • By setting the runtime attribute with a prefix to use. + Properties prefix.availableProcessors, + prefix.freeMemory, prefix.totalMemory + and prefix.maxMemory will be defined with values that + correspond to the corresponding methods of + the Runtime + class.
  • Although combinations of these ways are possible, only one should be used at a time. Problems might occur with the order in which properties are set, for instance.

    @@ -119,6 +126,22 @@ to Sun DTD, w are not; e.g. Windows 2000 or later system path variable is set to an Ant property named env.Path rather than env.PATH. + + runtime + the prefix to use when retrieving Runtime properties. Thus if + you specify runtime=myrt you will be able to + access runtime values corresponding to methods in + the Runtime + class via property names myrt.availableProcessors, + myrt.maxMemory, myrt.totalMemory or + myrt.freeMemory. Note that if you supply a property name with a final + . it will not be doubled; i.e. runtime=myrt. will still + allow access of prpperties through myrt.maxMemory.
    + Note also that the property values are snapshots taken at the point in time + when the property has been executed. + Since Ant 1.10.4 + + classpath the classpath to use when looking up a resource. diff --git a/src/etc/testcases/taskdefs/property.xml b/src/etc/testcases/taskdefs/property.xml index 87cda8b01..2e0c8353e 100644 --- a/src/etc/testcases/taskdefs/property.xml +++ b/src/etc/testcases/taskdefs/property.xml @@ -89,4 +89,8 @@ + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Property.java b/src/main/org/apache/tools/ant/taskdefs/Property.java index 4240bd52c..1cfd26861 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Property.java +++ b/src/main/org/apache/tools/ant/taskdefs/Property.java @@ -58,6 +58,12 @@ import org.apache.tools.ant.util.FileUtils; *
  • By setting the environment attribute with a prefix to use. * Properties will be defined for every environment variable by * prefixing the supplied name and a period to the name of the variable.
  • + *
  • By setting the runtime attribute with a prefix to use. + * Properties prefix.availableProcessors, + * prefix.freeMemory, prefix.totalMemory + * and prefix.maxMemory will be defined with values + * that correspond to the corresponding methods of the {@link + * Runtime} class.
  • * *

    Although combinations of these ways are possible, only one should be used * at a time. Problems might occur with the order in which properties are set, for @@ -85,6 +91,7 @@ public class Property extends Task { protected String env; protected Reference ref; protected String prefix; + private String runtime; private Project fallback; private Object untypedValue; private boolean valueAttributeUsed = false; @@ -385,6 +392,42 @@ public class Property extends Task { return env; } + /** + * Prefix to use when retrieving Runtime properties. + * + *

    Properties prefix.availableProcessors, + * prefix.freeMemory, prefix.totalMemory + * and prefix.maxMemory will be defined with values + * that correspond to the corresponding methods of the {@link + * Runtime} class.

    + * + *

    Note that if you supply a prefix name with a final + * "." it will not be doubled. ie + * runtime="myrt." will still allow access of property + * through "myrt.availableProcessors" and + * "myrt.freeMemory".

    + * + *

    The property values are snapshots taken at the point in time + * when the property has been executed.

    + * + * @param prefix prefix + * + * @ant.attribute group="noname" + * @since Ant 1.10.4 + */ + public void setRuntime(String prefix) { + this.runtime = prefix; + } + + /** + * Get the runtime attribute. + * @return the runtime attribute + * @since Ant 1.10.4 + */ + public String getRuntime() { + return runtime; + } + /** * The classpath to use when looking up a resource. * @param classpath to add to any existing classpath @@ -450,7 +493,7 @@ public class Property extends Task { /** * set the property in the project to the value. - * if the task was give a file, resource or env attribute + * if the task was give a file, resource, env or runtime attribute * here is where it is loaded * @throws BuildException on error */ @@ -470,7 +513,7 @@ public class Property extends Task { if (url == null && file == null && resource == null && env == null) { throw new BuildException( - "You must specify url, file, resource or environment when not using the name attribute", + "You must specify url, file, resource, environment or runtime when not using the name attribute", getLocation()); } } @@ -515,6 +558,10 @@ public class Property extends Task { loadEnvironment(env); } + if (runtime != null) { + loadRuntime(runtime); + } + if (name != null && ref != null) { try { addProperty(name, @@ -650,6 +697,25 @@ public class Property extends Task { addProperties(props); } + /** + * load the runtime values + * @param prefix prefix to place before them + * @since 1.10.4 + */ + protected void loadRuntime(String prefix) { + Properties props = new Properties(); + if (!prefix.endsWith(".")) { + prefix += "."; + } + log("Loading Runtime properties " + prefix, Project.MSG_VERBOSE); + Runtime r = Runtime.getRuntime(); + props.put(prefix + "availableProcessors", String.valueOf(r.availableProcessors())); + props.put(prefix + "freeMemory", String.valueOf(r.freeMemory())); + props.put(prefix + "maxMemory", String.valueOf(r.maxMemory())); + props.put(prefix + "totalMemory", String.valueOf(r.totalMemory())); + addProperties(props); + } + /** * iterate through a set of properties, * resolve them then assign them diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java index 080b63f62..4115339e1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java @@ -20,6 +20,7 @@ package org.apache.tools.ant.taskdefs; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeNoException; @@ -134,4 +135,16 @@ public class PropertyTest { assertEquals("TWO", buildRule.getProject().getProperty("xml.two")); } + @Test + public void testRuntime() { + // should get no output at all + buildRule.executeTarget("testRuntime"); + assertEquals(Runtime.getRuntime().availableProcessors(), + Integer.parseInt(buildRule.getProject().getProperty("testruntime.availableProcessors"))); + assertEquals(Runtime.getRuntime().maxMemory(), + Long.parseLong(buildRule.getProject().getProperty("testruntime.maxMemory"))); + assertNotNull(buildRule.getProject().getProperty("testruntime.freeMemory")); + assertNotNull(buildRule.getProject().getProperty("testruntime.totalMemory")); + } + }