Browse Source

Updated this task so that you can have <classpath> inside that tells the Available where to look for classes and resources. It is completely back compatible so should cause any harm to anybody.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268000 13f79535-47bb-0310-9956-ffa450edef68
master
Stefano Mazzocchi 25 years ago
parent
commit
bd02671bd0
1 changed files with 39 additions and 5 deletions
  1. +39
    -5
      src/main/org/apache/tools/ant/taskdefs/Available.java

+ 39
- 5
src/main/org/apache/tools/ant/taskdefs/Available.java View File

@@ -54,9 +54,10 @@


package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import org.apache.tools.ant.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;


/** /**
* Will set the given property if the requested resource is available at runtime. * Will set the given property if the requested resource is available at runtime.
@@ -70,8 +71,29 @@ public class Available extends Task {
private String classname; private String classname;
private File file; private File file;
private String resource; private String resource;
private Path classpath;
private AntClassLoader loader;
private String value = "true"; private String value = "true";


public void setClasspath(Path classpath) {
if (this.classpath == null) {
this.classpath = classpath;
} else {
this.classpath.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 setProperty(String property) { public void setProperty(String property) {
this.property = property; this.property = property;
} }
@@ -93,6 +115,10 @@ public class Available extends Task {
} }


public void execute() throws BuildException { public void execute() throws BuildException {
if (classpath != null) {
this.loader = new AntClassLoader(project, classpath, false);
}

if ((classname != null) && !checkClass(classname)) return; if ((classname != null) && !checkClass(classname)) return;
if ((file != null) && !checkFile(file)) return; if ((file != null) && !checkFile(file)) return;
if ((resource != null) && !checkResource(resource)) return; if ((resource != null) && !checkResource(resource)) return;
@@ -105,15 +131,23 @@ public class Available extends Task {
} }


private boolean checkResource(String resource) { private boolean checkResource(String resource) {
return (ClassLoader.getSystemResource(resource) != null);
if (loader != null) {
return (loader.getResourceAsStream(resource) != null);
} else {
return (this.getClass().getResourceAsStream(resource) != null);
}
} }


private boolean checkClass(String classname) { private boolean checkClass(String classname) {
try { try {
Class.forName(classname);
if (loader != null) {
loader.loadClass(classname);
} else {
this.getClass().getClassLoader().loadClass(classname);
}
return true; return true;
} catch (Throwable t) {
log(t.toString(), Project.MSG_VERBOSE);
} catch (ClassNotFoundException e) {
log(e.toString(), Project.MSG_VERBOSE);
return false; return false;
} }
} }


Loading…
Cancel
Save