From 3c26912e56e06ac64c392444cb19e3587f1bbb11 Mon Sep 17 00:00:00 2001 From: Stefano Mazzocchi Date: Fri, 11 Feb 2000 01:31:24 +0000 Subject: [PATCH] Added "available" task that sets a property in case a file|class|resource is present in the system Also, added an init() method to Tasks and cleaned up Property task git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267598 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/Available.java | 123 ++++++++++++++++ .../apache/tools/ant/taskdefs/Property.java | 139 ++++++++---------- .../org/apache/tools/ant/taskdefs/Tstamp.java | 13 +- .../tools/ant/taskdefs/defaults.properties | 5 +- 4 files changed, 195 insertions(+), 85 deletions(-) create mode 100644 src/main/org/apache/tools/ant/taskdefs/Available.java diff --git a/src/main/org/apache/tools/ant/taskdefs/Available.java b/src/main/org/apache/tools/ant/taskdefs/Available.java new file mode 100644 index 000000000..3a7c01ace --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/Available.java @@ -0,0 +1,123 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant.taskdefs; + +import org.apache.tools.ant.*; +import java.io.*; +import java.util.*; + +/** + * Will set the given property if the requested resource is available at runtime. + * + * @author Stefano Mazzocchi stefano@apache.org + */ + +public class Available extends Task { + + private String property; + private String classname; + private String file; + private String resource; + + public void setProperty(String property) { + this.property = property; + } + + public void setClass(String classname) { + this.classname = classname; + } + + public void setFile(String filename) { + this.file = file; + } + + public void setResource(String resource) { + this.resource = resource; + } + + public void init() throws BuildException { + if ((classname != null) && !checkClass(classname)) return; + if ((file != null) && !checkFile(file)) return; + if ((resource != null) && !checkResource(resource)) return; + + this.project.setProperty(property, "true"); + } + + private boolean checkFile(String file) { + try { + File f = new File(file); + return f.exists(); + } catch (Exception e) { + return false; + } + } + + private boolean checkResource(String resource) { + try { + return (ClassLoader.getSystemResource(resource) != null); + } catch (Exception e) { + return false; + } + } + + private boolean checkClass(String classname) { + try { + Class.forName(classname); + return true; + } catch (Throwable t) { + return false; + } + } +} diff --git a/src/main/org/apache/tools/ant/taskdefs/Property.java b/src/main/org/apache/tools/ant/taskdefs/Property.java index 134addb55..0c2cbd9d9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Property.java +++ b/src/main/org/apache/tools/ant/taskdefs/Property.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 1999 The Apache Software Foundation. All rights + * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -9,7 +9,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -17,15 +17,15 @@ * distribution. * * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by the + * any, must include the following acknowlegement: + * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived - * from this software without prior written permission. For written + * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" @@ -57,100 +57,85 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; import java.io.*; import java.util.*; + /** * Will set a Project property. Used to be a hack in ProjectHelper * * @author costin@dnt.ro */ public class Property extends Task { + private String name; private String value; private String file; private String resource; - /** - * Needs to be set at XML-reading time, - * no runtime action. - * - * @exception BuildException Thrown in unrecoverable error. - */ - public void execute() throws BuildException { + public void setName(String name) { + this.name = name; + } + + public void setValue(String value) { + this.value = value; } - // XXX ugly - needs to be fixed - /** - * Called after each setter, will set the property at read-time - */ - private void initTimeSetProperty() { - try { - if((name!=null) && (value!=null) ) { - String value1= ProjectHelper.replaceProperties(value, project.getProperties()); - project.setProperty( name,value1); - } - - if( file!=null) - loadFile( file ); - - if( resource!=null) - loadResource( resource ); - } catch (Exception ex) { - ex.printStackTrace(); - } + public void setFile(String file) { + this.file = file; } - public void setName( String name) { - this.name=name; - initTimeSetProperty(); + public void setResource(String resource) { + this.resource = resource; } - public void setValue(String v) { - value=v; - initTimeSetProperty(); + public void init() throws BuildException { + try { + if ((name != null) && (value != null)) { + String v = ProjectHelper.replaceProperties(value, project.getProperties()); + project.setProperty(name, v); + } + + if (file != null) loadFile(file); + + if (resource != null) loadResource(resource); + + } catch (Exception e) { + throw new BuildException(e); + } } - public void setFile(String v) { - file=v; - initTimeSetProperty(); + private void loadFile (String name) { + Properties props = new Properties(); + project.log("Loading " + name, project.MSG_VERBOSE); + try { + if (new File(name).exists()) { + props.load(new FileInputStream(name)); + addProperties(props); + } + } catch(Exception ex) { + ex.printStackTrace(); + } } - public void setResource(String v) { - resource=v; - initTimeSetProperty(); + private void loadResource( String name ) { + Properties props = new Properties(); + project.log("Resource Loading " + name, project.MSG_VERBOSE); + try { + InputStream is = this.getClass().getResourceAsStream(name); + if (is != null) { + props.load(is); + addProperties(props); + } + } catch (Exception ex) { + ex.printStackTrace(); + } } - - private void loadFile( String name ) { - Properties props = new Properties(); - project.log("Loading " + name, project.MSG_VERBOSE); - try { - if( new File(name).exists() ) - props.load(new FileInputStream( name )); - } catch(Exception ex) { - ex.printStackTrace(); - } - addProperties( props ); - } - - private void loadResource( String name ) { - Properties props = new Properties(); - project.log("Resource Loading " + name, - project.MSG_VERBOSE); - try { - InputStream is=this.getClass().getResourceAsStream(name); - if(is!=null) - props.load(is); - } catch (Exception ex) { - ex.printStackTrace(); - } - addProperties( props ); - } - private void addProperties( Properties props) { - Enumeration e=props.keys(); - while( e.hasMoreElements() ) { - String name=(String)e.nextElement(); - String value=(String)props.getProperty(name); - String value1= ProjectHelper.replaceProperties(value, project.getProperties()); - project.setProperty( name, value1); - } + private void addProperties(Properties props) { + Enumeration e = props.keys(); + while (e.hasMoreElements()) { + String name = (String) e.nextElement(); + String value = (String) props.getProperty(name); + String v = ProjectHelper.replaceProperties(value, project.getProperties()); + project.setProperty(name, v); + } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java index ffb0026ca..03c178ec2 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tstamp.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tstamp.java @@ -67,21 +67,20 @@ import java.text.*; */ public class Tstamp extends Task { - public void execute() throws BuildException { - + public void init() throws BuildException { try { Date d = new Date(); - + SimpleDateFormat dstamp = new SimpleDateFormat ("yyyymmdd"); project.setProperty("DSTAMP", dstamp.format(d)); - + SimpleDateFormat tstamp = new SimpleDateFormat ("hhmm"); project.setProperty("TSTAMP", tstamp.format(d)); - + SimpleDateFormat today = new SimpleDateFormat ("MMMM d yyyy", Locale.US); project.setProperty("TODAY", today.format(d)); - } catch (Exception ex) { - ex.printStackTrace(); + } catch (Exception e) { + throw new BuildException(e); } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties index fe8e0e6af..f668642fd 100644 --- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties +++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties @@ -1,3 +1,4 @@ +# standard ant tasks mkdir=org.apache.tools.ant.taskdefs.Mkdir javac=org.apache.tools.ant.taskdefs.Javac chmod=org.apache.tools.ant.taskdefs.Chmod @@ -23,5 +24,7 @@ taskdef=org.apache.tools.ant.taskdefs.Taskdef ant=org.apache.tools.ant.taskdefs.Ant exec=org.apache.tools.ant.taskdefs.Exec tar=org.apache.tools.ant.taskdefs.Tar -# remove the task below once everyone has migrated +available=org.apache.tools.ant.taskdefs.Available + +# deprecated ant tasks (kept for back compatibility) javadoc2=org.apache.tools.ant.taskdefs.Javadoc