Return-Path: Mailing-List: contact ant-dev-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list ant-dev@jakarta.apache.org Received: (qmail 88856 invoked from network); 24 Jul 2000 03:44:12 -0000 Received: from smtp02.mrf.mail.rcn.net (207.172.4.61) by locus.apache.org with SMTP; 24 Jul 2000 03:44:12 -0000 Received: from r83aap006110.sbo.ma.cable.rcn.com ([209.6.196.75] helo=cow) by smtp02.mrf.mail.rcn.net with smtp (Exim 3.15 #2) id 13GZA4-000103-00 for ant-dev@jakarta.apache.org; Sun, 23 Jul 2000 23:44:12 -0400 From: "Tim Fennell" To: Subject: [PATCH] Available.java - to detect JDK versions Date: Sun, 23 Jul 2000 23:43:53 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700 X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N Here is the patch. It supports the syntax suggested: 1.2.2 - matches specified version 1.2+ - matches 1.2 or higher 1.2- - matches 1.2 or lower Note both the +/- version are *inclusive*, so check for 1.99- if you want pre 1.2 etc! -t ---------------------------Begin Patch---------------------------- Index: Available.java =================================================================== RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Available.jav a,v retrieving revision 1.6 diff -u -r1.6 Available.java --- Available.java 2000/07/06 16:48:13 1.6 +++ Available.java 2000/07/24 03:43:23 @@ -62,6 +62,7 @@ * Will set the given property if the requested resource is available at runtime. * * @author Stefano Mazzocchi stefano@apache.org + * @author Tim Fennell tfennell@sapient.com */ public class Available extends Task { @@ -70,6 +71,7 @@ private String classname; private String file; private String resource; + private String jdk; public void setProperty(String property) { this.property = property; @@ -94,10 +96,16 @@ this.resource = resource; } + /** Setter method to set the value of the JDK to check for. */ + public void setJdk(String jdk) { + this.jdk = jdk; + } + public void init() throws BuildException { if ((classname != null) && !checkClass(classname)) return; if ((file != null) && !checkFile(file)) return; if ((resource != null) && !checkResource(resource)) return; + if ((jdk != null) && !checkJdk(jdk)) return; this.project.setProperty(property, "true"); } @@ -125,4 +133,75 @@ return false; } } + + /** + * Checks to see if the current JDK version (the one this method is called + * within) matches a criteria. Either that it is 'equal', 'equal or higher' + * or 'equal of lower'. + * @param jdk version of the JDK to check for. Terminate with a '+' to check + * for a version or higher. Terminate with a '-' to check for a + * version or lower. + * @return boolean true if the current JDK matches the criteria. + */ + private boolean checkJdk(String jdk) { + double checkVersion = 0.0; + double currentVersion = 0.0; + int returnCheck = 0; + int returnValue = 0; + + // Determine if we are looking for an exact match, greater than or less + // than... And convert the jdk string to a double. + if (jdk.endsWith("-")) { + returnCheck = -1; + checkVersion = stringToDouble(jdk.substring(0, jdk.length() -1)); + } + else if (jdk.endsWith("+")) { + returnCheck = +1; + checkVersion = stringToDouble(jdk.substring(0, jdk.length() -1)); + } + else { + returnCheck = 0; + checkVersion = stringToDouble(jdk); + } + + // Get the current JDK version as a double + currentVersion = stringToDouble(System.getProperty("java.version")); + + // Check the current version against the checkVersion + if (checkVersion == currentVersion) + returnValue = 0; + else if (currentVersion < checkVersion) + returnValue = -1; + else if (currentVersion > checkVersion) + returnValue = +1; + + // If the versions are an exact match, return true. Otherwise check to + // see if the returnValue is equal to the checkValue. + if (returnValue == 0) return true; + else return (returnValue == returnCheck); + + } + + /** + * Utility method to convert strings with multiple decimal places into + * doubles that can be safely compared with one another for numerical + * equality. + * @param inString the String to convert to a decimal. + * @return double the value of the string as a double. + */ + public static double stringToDouble(String inString) { + double returnValue = 0.0; + int divisor = 1; + String digits = null; + StringTokenizer tokenizer = new StringTokenizer(inString, "."); + + while (tokenizer.hasMoreTokens()) { + digits = tokenizer.nextToken(); + returnValue += (Double.parseDouble(digits) / (divisor * digits.length()) ); + divisor *= 10; + } + + return returnValue; + } + }