Repository: ant
Updated Branches:
  refs/heads/master 22a05c5ba -> 6a9d70120


add properties created from Runtime's methods

inspired by 
https://stackoverflow.com/questions/1170337/how-can-i-get-the-number-of-availableprocessors-in-ant


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/5e99fc2c
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/5e99fc2c
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/5e99fc2c

Branch: refs/heads/master
Commit: 5e99fc2c1ad0930b8f46ead6fdb5fc63b430827f
Parents: f72406d
Author: Stefan Bodewig <bode...@apache.org>
Authored: Thu Apr 26 13:56:09 2018 +0200
Committer: Stefan Bodewig <bode...@apache.org>
Committed: Thu Apr 26 13:56:09 2018 +0200

----------------------------------------------------------------------
 WHATSNEW                                        |  5 ++
 manual/Tasks/property.html                      | 23 ++++++
 src/etc/testcases/taskdefs/property.xml         |  4 ++
 .../org/apache/tools/ant/taskdefs/Property.java | 74 ++++++++++++++++++--
 .../apache/tools/ant/taskdefs/PropertyTest.java | 13 ++++
 5 files changed, 115 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/5e99fc2c/WHATSNEW
----------------------------------------------------------------------
diff --git a/WHATSNEW b/WHATSNEW
index 3041bc5..a87bd81 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -36,6 +36,11 @@ Other changes:
    finishing threads might fail.
    Bugzilla Report 62148
 
+ * <property> 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.9.10 TO Ant 1.9.11
 =====================================
 

http://git-wip-us.apache.org/repos/asf/ant/blob/5e99fc2c/manual/Tasks/property.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/property.html b/manual/Tasks/property.html
index 7dc90a7..e8e57bb 100644
--- a/manual/Tasks/property.html
+++ b/manual/Tasks/property.html
@@ -49,6 +49,13 @@ resource) in the project.  Properties are case sensitive.</p>
   <li>By setting the <i>environment</i> 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.</li>
+  <li>By setting the <i>runtime</i> attribute with a prefix to use.
+    Properties <code>prefix.availableProcessors</code>,
+    <code>prefix.freeMemory</code>, <code>prefix.totalMemory</code>
+    and <code>prefix.maxMemory</code> will be defined with values that
+    correspond to the corresponding methods of
+    the <a 
href="https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html";>Runtime</a>
+    class.</li>
 </ul>
 <p>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
@@ -127,6 +134,22 @@ to end with <tt>.xml</tt>.</p>
     rather than "env.PATH".</td>
   </tr>
   <tr>
+    <td valign="top">runtime</td>
+    <td valign="top">the prefix to use when retrieving Runtime properties. Thus
+    if you specify runtime=&quot;myrt&quot; you will be able to access
+    runtime values corresponding to methods in
+    the <a 
href="https://docs.oracle.com/javase/10/docs/api/java/lang/Runtime.html";>Runtime</a>
+    class via property names &quot;myrt.availableProcessors&quot;,
+    &quot;myrt.maxMemory&quot;, &quot;myrt.totalMemory&quot; or
+    &quot;myrt.freeMemory&quot;. Note that if you supply a property name with 
a final
+    &quot;.&quot; it will not be doubled; i.e. runtime=&quot;myrt.&quot; will 
still
+    allow access of prpperties through &quot;myrt.maxMemory&quot;.<br>
+    Note also that the property values are snapshots taken at the point in time
+    when the <code>property</code> has been executed.
+    <em>Since Ant 1.9.12</em>
+    </td>
+  </tr>
+  <tr>
     <td valign="top">classpath</td>
     <td valign="top">the classpath to use when looking up a resource.</td>
     <td align="center" valign="top">No</td>

http://git-wip-us.apache.org/repos/asf/ant/blob/5e99fc2c/src/etc/testcases/taskdefs/property.xml
----------------------------------------------------------------------
diff --git a/src/etc/testcases/taskdefs/property.xml 
b/src/etc/testcases/taskdefs/property.xml
index 87cda8b..2e0c835 100644
--- a/src/etc/testcases/taskdefs/property.xml
+++ b/src/etc/testcases/taskdefs/property.xml
@@ -89,4 +89,8 @@
   </target>    
   <target name="testXmlProperty" depends="testXmlProperty.internal"/>  
 
+  <target name="testRuntime">
+    <property runtime="testruntime"/>
+  </target>
+
 </project>

http://git-wip-us.apache.org/repos/asf/ant/blob/5e99fc2c/src/main/org/apache/tools/ant/taskdefs/Property.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/Property.java 
b/src/main/org/apache/tools/ant/taskdefs/Property.java
index 64a6376..a6b51a2 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Property.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Property.java
@@ -59,6 +59,12 @@ import org.apache.tools.ant.util.FileUtils;
  *   <li>By setting the <i>environment</i> 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.</li>
+ *   <li>By setting the <i>runtime</i> attribute with a prefix to use.
+ *     Properties <code>prefix.availableProcessors</code>,
+ *     <code>prefix.freeMemory</code>, <code>prefix.totalMemory</code>
+ *     and <code>prefix.maxMemory</code> will be defined with values
+ *     that correspond to the corresponding methods of the {@link
+ *     Runtime} class.</li>
  * </ul>
  * <p>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
@@ -86,6 +92,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;
@@ -388,6 +395,42 @@ public class Property extends Task {
     }
 
     /**
+     * Prefix to use when retrieving Runtime properties.
+     *
+     * <p>Properties <code>prefix.availableProcessors</code>,
+     * <code>prefix.freeMemory</code>, <code>prefix.totalMemory</code>
+     * and <code>prefix.maxMemory</code> will be defined with values
+     * that correspond to the corresponding methods of the {@link
+     * Runtime} class.</p>
+     *
+     * <p>Note that if you supply a prefix name with a final
+     * &quot;.&quot; it will not be doubled. ie
+     * runtime=&quot;myrt.&quot; will still allow access of property
+     * through &quot;myrt.availableProcessors&quot; and
+     * &quot;myrt.freeMemory&quot;.</p>
+     *
+     * <p>The property values are snapshots taken at the point in time
+     * when the <code>property</code> has been executed.</p>
+     *
+     * @param prefix prefix
+     *
+     * @ant.attribute group="noname"
+     * @since Ant 1.9.12
+     */
+    public void setRuntime(String prefix) {
+        this.runtime = prefix;
+    }
+
+    /**
+     * Get the runtime attribute.
+     * @return the runtime attribute
+     * @since Ant 1.9.12
+     */
+    public String getRuntime() {
+        return runtime;
+    }
+
+    /**
      * The classpath to use when looking up a resource.
      * @param classpath to add to any existing classpath
      */
@@ -452,7 +495,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
      */
@@ -469,9 +512,9 @@ public class Property extends Task {
                                          getLocation());
             }
         } else {
-            if (url == null && file == null && resource == null && env == 
null) {
-                throw new BuildException("You must specify url, file, resource 
or "
-                                         + "environment when not using the "
+            if (url == null && file == null && resource == null && env == null 
&& runtime == null) {
+                throw new BuildException("You must specify url, file, 
resource, "
+                                         + "environment or runtime when not 
using the "
                                          + "name attribute", getLocation());
             }
         }
@@ -513,6 +556,10 @@ public class Property extends Task {
             loadEnvironment(env);
         }
 
+        if (runtime != null) {
+            loadRuntime(runtime);
+        }
+
         if ((name != null) && (ref != null)) {
             try {
                 addProperty(name,
@@ -660,6 +707,25 @@ public class Property extends Task {
     }
 
     /**
+     * load the runtime values
+     * @param prefix prefix to place before them
+     * @since 1.9.12
+     */
+    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
      * @param props the properties to iterate over

http://git-wip-us.apache.org/repos/asf/ant/blob/5e99fc2c/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java
----------------------------------------------------------------------
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 7cead66..adb7843 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.apache.tools.ant.AntAssert.assertContains;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -136,4 +137,16 @@ public class PropertyTest {
 
     }
 
+    @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"));
+    }
+
 }

Reply via email to