used to override an existing"
+ " property."
+ StringUtils.LINE_SEP
+ " Build file should not reuse the same property"
+ " name for different values.");
}
getProject().setProperty(property, value);
}
} finally {
isTask = false;
}
}
/**
* Evaluate the availability of a resource.
*
* @return boolean is the resource is available.
* @exception BuildException if the condition is not configured correctly
*/
public boolean eval() throws BuildException {
if (classname == null && file == null && resource == null) {
throw new BuildException("At least one of (classname|file|"
+ "resource) is required", getLocation());
}
if (type != null) {
if (file == null) {
throw new BuildException("The type attribute is only valid "
+ "when specifying the file "
+ "attribute.", getLocation());
}
}
if (classpath != null) {
classpath.setProject(getProject());
this.loader = getProject().createClassLoader(classpath);
}
String appendix = "";
if (isTask) {
appendix = " to set property " + property;
} else {
setTaskName("available");
}
if ((classname != null) && !checkClass(classname)) {
log("Unable to load class " + classname + appendix,
Project.MSG_VERBOSE);
return false;
}
if ((file != null) && !checkFile()) {
if (type != null) {
log("Unable to find " + type + " " + file + appendix,
Project.MSG_VERBOSE);
} else {
log("Unable to find " + file + appendix, Project.MSG_VERBOSE);
}
return false;
}
if ((resource != null) && !checkResource(resource)) {
log("Unable to load resource " + resource + appendix,
Project.MSG_VERBOSE);
return false;
}
if (loader != null) {
loader.cleanup();
loader = null;
}
if (!isTask) {
setTaskName(null);
}
return true;
}
/**
* Search for file/directory either either relative to project's
* basedir or in the path given as filepath.
*
* 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
*
*/
private boolean checkFile() {
if (filepath == null) {
return checkFile(getProject().resolveFile(file), file);
} else {
String[] paths = filepath.list();
for (int i = 0; i < paths.length; ++i) {
log("Searching " + paths[i], Project.MSG_DEBUG);
File path = new File(paths[i]);
// ** full-pathname specified == path in list
// ** simple name specified == path in list
if (path.exists() && file.equals(paths[i])) {
if (type == null) {
log("Found: " + path, Project.MSG_VERBOSE);
return true;
} else if (type.isDir()
&& path.isDirectory()) {
log("Found directory: " + path, Project.MSG_VERBOSE);
return true;
} else if (type.isFile()
&& path.isFile()) {
log("Found file: " + path, Project.MSG_VERBOSE);
return true;
}
// not the requested type
return false;
}
FileUtils fileUtils = FileUtils.newFileUtils();
File parent = fileUtils.getParentFile(path);
// ** full-pathname specified == parent dir of path in list
if (parent != null && parent.exists()
&& file.equals(parent.getAbsolutePath())) {
if (type == null) {
log("Found: " + parent, Project.MSG_VERBOSE);
return true;
} else if (type.isDir()) {
log("Found directory: " + parent, Project.MSG_VERBOSE);
return true;
}
// not the requested type
return false;
}
// ** simple name specified == path in list + name
if (path.exists() && path.isDirectory()) {
if (checkFile(new File(path, file),
file + " in " + path)) {
return true;
}
}
// ** simple name specified == parent dir + name
if (parent != null && parent.exists()) {
if (checkFile(new File(parent, file),
file + " in " + parent)) {
return true;
}
}
// ** simple name specified == parent of parent dir + name
if (parent != null) {
File grandParent = fileUtils.getParentFile(parent);
if (grandParent != null && grandParent.exists()) {
if (checkFile(new File(grandParent, file),
file + " in " + grandParent)) {
return true;
}
}
}
}
}
return false;
}
/**
* Check if a given file exists and matches the required type.
*/
private boolean checkFile(File f, String text) {
if (type != null) {
if (type.isDir()) {
if (f.isDirectory()) {
log("Found directory: " + text, Project.MSG_VERBOSE);
}
return f.isDirectory();
} else if (type.isFile()) {
if (f.isFile()) {
log("Found file: " + text, Project.MSG_VERBOSE);
}
return f.isFile();
}
}
if (f.exists()) {
log("Found: " + text, Project.MSG_VERBOSE);
}
return f.exists();
}
/**
* Check if a given resource can be loaded.
*/
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);
}
}
}
/**
* Check if a given class can be loaded.
*/
private boolean checkClass(String classname) {
try {
Class requiredClass = null;
if (ignoreSystemclasses) {
loader = getProject().createClassLoader(classpath);
loader.setParentFirst(false);
loader.addJavaLibraries();
if (loader != null) {
try {
requiredClass = loader.findClass(classname);
} catch (SecurityException se) {
// class found but restricted name; this is
// actually the case we're looking for in JDK 1.3+,
// so catch the exception and return
return true;
}
} else {
return false;
}
} else if (loader != null) {
requiredClass = 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) {
requiredClass = Class.forName(classname, true, l);
} else {
requiredClass = Class.forName(classname);
}
}
return true;
} catch (ClassNotFoundException e) {
log("class \"" + classname + "\" was not found",
Project.MSG_DEBUG);
return false;
} catch (NoClassDefFoundError e) {
log("Could not load dependent class \"" + e.getMessage()
+ "\" for class \"" + classname + "\"",
Project.MSG_DEBUG);
return false;
}
}
/**
* EnumeratedAttribute covering the file types to be checked for, either
* file or dir.
*/
public static class FileDir extends EnumeratedAttribute {
private static final String[] VALUES = {"file", "dir"};
/**
* @see EnumeratedAttribute#getValues
*/
public String[] getValues() {
return VALUES;
}
/**
* Indicate if the value specifies a directory.
*
* @return true if the value specifies a directory.
*/
public boolean isDir() {
return "dir".equalsIgnoreCase(getValue());
}
/**
* Indicate if the value specifies a file.
*
* @return true if the value specifies a file.
*/
public boolean isFile() {
return "file".equalsIgnoreCase(getValue());
}
}
}