1 8 9 import java.io.*; 10 import java.util.*; 11 import java.util.zip.*; 12 import org.apache.tools.ant.*; 13 import org.apache.tools.ant.taskdefs.*; 14 import org.apache.tools.ant.types.*; 15 16 28 29 public class ClassAvailableTask extends Task { 30 31 34 private Hashtable zipFiles = new Hashtable(); 35 36 private String property; 37 private String classname; 38 private Path classpath; 39 private String value = "true"; 40 41 public void setClasspath(Path classpath) { 42 createClasspath().append(classpath); 43 } 44 45 public Path createClasspath() { 46 if (this.classpath == null) { 47 this.classpath = new Path(this.project); 48 } 49 return this.classpath.createPath(); 50 } 51 52 public void setClasspathRef(Reference r) { 53 createClasspath().setRefid(r); 54 } 55 56 public void setProperty(String property) { 57 this.property = property; 58 } 59 60 public void setValue(String value) { 61 this.value = value; 62 } 63 64 public void setClassname(String classname) { 65 if (!"".equals(classname)) { 66 this.classname = classname; 67 } 68 } 69 70 public void execute() throws BuildException { 71 if (property == null) { 72 throw new BuildException("property attribute is required", location); 73 } 74 75 if (eval()) { 76 this.project.setProperty(property, value); 77 } 78 } 79 80 public boolean eval() throws BuildException { 81 if (classname == null) { 82 throw new BuildException("At least one of (classname|file|resource) is required", location); 83 } 84 85 if (classpath != null) { 86 classpath.setProject(project); 87 classpath = classpath.concatSystemClasspath("ignore"); 88 } 89 90 if (!findClassInComponents(classname)) { 91 log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE); 92 return false; 93 } 94 95 return true; 96 } 97 98 108 private boolean contains(File file, String resourceName) { 109 try { 110 if (!file.exists()) { 111 return false; 112 } 113 114 if (file.isDirectory()) { 115 File resource = new File(file, resourceName); 116 117 if (resource.exists()) { 118 return true; 119 } 120 } 121 else { 122 ZipFile zipFile = (ZipFile)zipFiles.get(file); 124 if (zipFile == null) { 125 zipFile = new ZipFile(file); 126 zipFiles.put(file, zipFile); 127 } 128 ZipEntry entry = zipFile.getEntry(resourceName); 129 if (entry != null) { 130 return true; 131 } 132 } 133 } 134 catch (Exception e) { 135 log("Ignoring Exception " + e.getClass().getName() + ": " + e.getMessage() + 136 " reading resource " + resourceName + " from " + file, Project.MSG_VERBOSE); 137 } 138 139 return false; 140 } 141 142 145 private boolean findClassInComponents(String name) { 146 final String classname = name.replace('.', '/') + ".class"; 149 final String [] list = classpath.list(); 150 boolean found = false; 151 int i = 0; 152 while (i < list.length && found == false) { 153 final File pathComponent = (File)project.resolveFile(list[i]); 154 found = this.contains(pathComponent, classname); 155 i++; 156 } 157 return found; 158 } 159 } 160 | Popular Tags |