/* * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 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", "Ant", 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 java.io.File; import org.apache.tools.ant.Task; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.Condition; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; /** * Will set the given property if the requested resource is available at runtime. * * @author Stefano Mazzocchi stefano@apache.org */ public class Available extends Task implements Condition { private String property; private String classname; private String file; private Path filepath; private String resource; private String type; private Path classpath; private AntClassLoader loader; private String value = "true"; public void setClasspath(Path classpath) { createClasspath().append(classpath); } public Path createClasspath() { if (this.classpath == null) { this.classpath = new Path(project); } return this.classpath.createPath(); } public void setClasspathRef(Reference r) { createClasspath().setRefid(r); } public void setFilepath(Path filepath) { createFilepath().append(filepath); } public Path createFilepath() { if (this.filepath == null) { this.filepath = new Path(project); } return this.filepath.createPath(); } public void setProperty(String property) { this.property = property; } public void setValue(String value) { this.value = value; } public void setClassname(String classname) { if (!"".equals(classname)) { this.classname = classname; } } public void setFile(String file) { this.file = file; } public void setResource(String resource) { this.resource = resource; } public void setType(String type) { this.type = type; } public void execute() throws BuildException { if (property == null) { throw new BuildException("property attribute is required", location); } if (eval()) { this.project.setProperty(property, value); } } public boolean eval() throws BuildException { if (classname == null && file == null && resource == null) { throw new BuildException("At least one of (classname|file|resource) is required", location); } if (type != null){ if (file == null){ throw new BuildException("The type attribute is only valid when specifying the file attribute."); } if (!type.equalsIgnoreCase("file") && !type.equalsIgnoreCase("dir")){ throw new BuildException("Type must be one of either dir or file"); } } if (classpath != null) { classpath.setProject(project); this.loader = new AntClassLoader(project, classpath); } if ((classname != null) && !checkClass(classname)) { log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE); return false; } if ((file != null) && !checkFile()) { if (type != null) { log("Unable to find " + type + " " + file + " to set property " + property, Project.MSG_VERBOSE); } else { log("Unable to find " + file + " to set property " + property, Project.MSG_VERBOSE); } return false; } if ((resource != null) && !checkResource(resource)) { log("Unable to load resource " + resource + " to set property " + property, Project.MSG_VERBOSE); return false; } if (loader != null) { loader.cleanup(); } return true; } private boolean checkFile() { if (filepath == null) { return checkFile(file); } else { String[] paths = filepath.list(); for(int i = 0; i < paths.length; ++i) { log("Searching " + paths[i], Project.MSG_DEBUG); /* ** filepath can be a list of directory and/or ** file names (gen'd via ) ** ** look for: ** full-pathname specified == path in list ** full-pathname specified == parent dir of path in list ** simple name specified == path in list ** simple name specified == path in list + name ** simple name specified == parent dir + name ** simple name specified == parent of parent dir + name ** */ File path = new File(paths[i]); String dirname = path.getParent(); if (type != null) { if (type.equalsIgnoreCase("dir")) { if (path.isFile()) { // full-pathname specified if (dirname.equals(path.toString())) { log("Found directory: " + path, Project.MSG_VERBOSE); return true; // simple name specified } else if(new File(dirname, file).isDirectory()) { log("Found directory: " + dirname + File.separator + file, Project.MSG_VERBOSE); return true; } // full-pathname specified } else if (path.toString().equals(new File(file).toString()) && path.isDirectory()) { log("Found directory: " + path, Project.MSG_VERBOSE); return true; // simple name specified } else if (new File(path, file).isDirectory()) { log("Found directory: " + path + File.separator + file, Project.MSG_VERBOSE); return true; } /* end check for type dir */ } else { if (path.toString().equals(new File(file).toString()) && path.isFile()) { log("Found file: " + path, Project.MSG_VERBOSE); return true; } else if (new File(path, file).isFile()) { log("Found file: " + path + File.separator + file, Project.MSG_VERBOSE); return true; } else if (new File(dirname, file).isFile()) { log("Found file: " + dirname + File.separator + file, Project.MSG_VERBOSE); return true; } } /* end check for specified type */ } else { if (path.toString().equals(new File(file).toString())) { log("Found: " + path, Project.MSG_VERBOSE); return true; } else if (new File(path, file).exists()) { log("Found: " + path + File.separator + file, Project.MSG_VERBOSE); return true; } else if (new File(dirname, file).exists()) { log("Found: " + dirname + File.separator + file, Project.MSG_VERBOSE); return true; } else { File dir = new File(dirname); dirname = dir.getParent(); if (new File(dirname, file).exists()) { log("Found: " + dirname + File.separator + file, Project.MSG_VERBOSE); return true; } } } } } return false; } private boolean checkFile(String file) { File filename = new File(file); if (type != null) { if (type.equalsIgnoreCase("dir")) { if( filename.isDirectory()) { log("Found directory: " + file, Project.MSG_VERBOSE); } return filename.isDirectory(); } else if (type.equalsIgnoreCase("file")) { if( filename.isFile()) { log("Found file: " + file, Project.MSG_VERBOSE); } return filename.isFile(); } } if (filename.exists()) { log("Found: " + file, Project.MSG_VERBOSE); } return filename.exists(); } private boolean checkResource(String resource) { if (loader != null) { return (loader.getResourceAsStream(resource) != null); } else { ClassLoader cL = this.getClass().getClassLoader(); if (cL != null) { return (cL.getResourceAsStream(resource) != null); } else { return (ClassLoader.getSystemResourceAsStream(resource) != null); } } } private boolean checkClass(String classname) { try { if (loader != null) { loader.loadClass(classname); } else { ClassLoader l = this.getClass().getClassLoader(); // Can return null to represent the bootstrap class loader. // see API docs of Class.getClassLoader. if (l != null) { l.loadClass(classname); } else { Class.forName(classname); } } return true; } catch (ClassNotFoundException e) { return false; } catch (NoClassDefFoundError e) { return false; } } }