Return-Path: Delivered-To: apmail-jakarta-ant-dev-archive@apache.org Received: (qmail 51301 invoked from network); 9 Apr 2002 20:32:16 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 9 Apr 2002 20:32:16 -0000 Received: (qmail 27568 invoked by uid 97); 9 Apr 2002 20:32:18 -0000 Delivered-To: qmlist-jakarta-archive-ant-dev@jakarta.apache.org Received: (qmail 27530 invoked by uid 97); 9 Apr 2002 20:32:17 -0000 Mailing-List: contact ant-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Ant Developers List" Reply-To: "Ant Developers List" Delivered-To: mailing list ant-dev@jakarta.apache.org Received: (qmail 27519 invoked by uid 97); 9 Apr 2002 20:32:17 -0000 Date: 9 Apr 2002 20:32:10 -0000 Message-ID: <20020409203210.94504.qmail@icarus.apache.org> From: stevel@apache.org To: jakarta-ant-cvs@apache.org Subject: cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional EchoProperties.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N stevel 02/04/09 13:32:10 Modified: src/main/org/apache/tools/ant/taskdefs/optional EchoProperties.java Log: Patch from Matt to use reflection to grab the java1.2 property save routine if available. As an aside, this code could be factored out and put into a utility method to save property objects safely on java1.2 and 1.2+ Revision Changes Path 1.5 +80 -4 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java Index: EchoProperties.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/EchoProperties.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- EchoProperties.java 25 Feb 2002 15:08:23 -0000 1.4 +++ EchoProperties.java 9 Apr 2002 20:32:10 -0000 1.5 @@ -118,13 +118,16 @@ private boolean failonerror = true; /** - * prefix string controls which properties to save + * Prefix string controls which properties to save. */ private String prefix = null; /** - * set a file to use for the output + * Set a file to store the property output. If this is never specified, + * then the output will be sent to the Ant log. + * + *@param destfile file to store the property output */ public void setDestfile(File destfile) { this.destfile = destfile; @@ -132,6 +135,8 @@ /** + * Sets the failure mode for the task. + * *@param failonerror true if IO exceptions are reported as build * exceptions, or false if IO exceptions are ignored. */ @@ -141,6 +146,16 @@ /** + * If the prefix is set, then only properties which start with this + * prefix string will be recorded. If this is never set, or it is set + * to an empty string or null, then all properties will be + * recorded.

+ * + * For example, if the property is set as: + *

<echoproperties  prefix="ant." />
+ * then the property "ant.home" will be recorded, but "ant-example" + * will not. + * *@param prefix The new prefix value */ public void setPrefix(String prefix) { @@ -181,6 +196,8 @@ /** * Send the key/value pairs in the hashtable to the given output stream. + * Only those properties matching the prefix constraint will be + * sent to the output stream. * The output stream will be closed when this method returns. * *@param allProps propfile to save @@ -188,7 +205,7 @@ *@exception IOException trouble */ protected void saveProperties(Hashtable allProps, OutputStream os) - throws IOException { + throws IOException, BuildException { Properties props = new Properties(); Enumeration enum = allProps.keys(); while (enum.hasMoreElements()) { @@ -199,10 +216,69 @@ } } try { - props.save(os, "Ant properties"); + jdkSaveProperties( props, os, "Ant properties" ); } finally { os.close(); } + } + + + /** + * JDK 1.2 allows for the safer method + * Properties.store( OutputStream, String ), which throws an + * IOException on an output error. This method attempts to + * use the JDK 1.2 method first, and if that does not exist, then the + * JDK 1.0 compatible method + * Properties.save( OutputStream, String ) is used instead. + * + *@param props the properties to record + *@param os record the properties to this output stream + *@param header prepend this header to the property output + *@exception IOException on an I/O error during a write. Only thrown + * for JDK 1.2+. + */ + protected void jdkSaveProperties( Properties props, OutputStream os, + String header ) + throws IOException { + try { + java.lang.reflect.Method m = props.getClass().getMethod( + "store", new Class[] { OutputStream.class, String.class } ); + m.invoke( props, new Object[] { os, header } ); + } catch (java.lang.reflect.InvocationTargetException ite) { + Throwable t = ite.getTargetException(); + if (t instanceof IOException) { + throw (IOException)t; + } + if (t instanceof RuntimeException) { + throw (RuntimeException)t; + } + + // not an expected exception. Resort to JDK 1.0 to execute + // this method + jdk10SaveProperties( props, os, header ); + } catch (ThreadDeath td) { + // don't trap thread death errors. + throw td; + } catch (Throwable ex) { + // this 'store' method is not available, so resort to the JDK 1.0 + // compatible method. + jdk10SaveProperties( props, os, header ); + } + } + + + /** + * Save the properties to the output stream using the JDK 1.0 compatible + * method. This won't throw an IOException on an output error. + * + *@param props the properties to record + *@param os record the properties to this output stream + *@param header prepend this header to the property output + */ + protected void jdk10SaveProperties( Properties props, OutputStream os, + String header ) + { + props.save( os, header ); } } -- To unsubscribe, e-mail: For additional commands, e-mail: