1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import org.apache.tools.ant.AntClassLoader; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.taskdefs.condition.Condition; 27 import org.apache.tools.ant.types.EnumeratedAttribute; 28 import org.apache.tools.ant.types.Path; 29 import org.apache.tools.ant.types.Reference; 30 import org.apache.tools.ant.util.FileUtils; 31 import org.apache.tools.ant.util.StringUtils; 32 33 41 public class Available extends Task implements Condition { 42 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 43 44 private String property; 45 private String classname; 46 private String filename; 47 private File file; 48 private Path filepath; 49 private String resource; 50 private FileDir type; 51 private Path classpath; 52 private AntClassLoader loader; 53 private String value = "true"; 54 private boolean isTask = false; 55 private boolean ignoreSystemclasses = false; 56 private boolean searchParents = false; 57 58 66 public void setSearchParents(boolean searchParents) { 67 this.searchParents = searchParents; 68 } 69 70 75 public void setClasspath(Path classpath) { 76 createClasspath().append(classpath); 77 } 78 79 84 public Path createClasspath() { 85 if (this.classpath == null) { 86 this.classpath = new Path(getProject()); 87 } 88 return this.classpath.createPath(); 89 } 90 91 97 public void setClasspathRef(Reference r) { 98 createClasspath().setRefid(r); 99 } 100 101 106 public void setFilepath(Path filepath) { 107 createFilepath().append(filepath); 108 } 109 110 116 public Path createFilepath() { 117 if (this.filepath == null) { 118 this.filepath = new Path(getProject()); 119 } 120 return this.filepath.createPath(); 121 } 122 123 129 public void setProperty(String property) { 130 this.property = property; 131 } 132 133 139 public void setValue(String value) { 140 this.value = value; 141 } 142 143 149 public void setClassname(String classname) { 150 if (!"".equals(classname)) { 151 this.classname = classname; 152 } 153 } 154 155 161 public void setFile(File file) { 162 this.file = file; 163 this.filename = FILE_UTILS.removeLeadingPath(getProject().getBaseDir(), file); 164 } 165 166 171 public void setResource(String resource) { 172 this.resource = resource; 173 } 174 175 183 public void setType(String type) { 184 log("DEPRECATED - The setType(String) method has been deprecated." 185 + " Use setType(Available.FileDir) instead.", 186 Project.MSG_WARN); 187 this.type = new FileDir(); 188 this.type.setValue(type); 189 } 190 191 198 public void setType(FileDir type) { 199 this.type = type; 200 } 201 202 208 public void setIgnoresystemclasses(boolean ignore) { 209 this.ignoreSystemclasses = ignore; 210 } 211 212 217 public void execute() throws BuildException { 218 if (property == null) { 219 throw new BuildException("property attribute is required", 220 getLocation()); 221 } 222 223 isTask = true; 224 try { 225 if (eval()) { 226 String oldvalue = getProject().getProperty(property); 227 if (null != oldvalue && !oldvalue.equals(value)) { 228 log("DEPRECATED - <available> used to override an existing" 229 + " property." 230 + StringUtils.LINE_SEP 231 + " Build file should not reuse the same property" 232 + " name for different values.", 233 Project.MSG_WARN); 234 } 235 getProject().setProperty(property, value); 238 } 239 } finally { 240 isTask = false; 241 } 242 } 243 244 250 public boolean eval() throws BuildException { 251 try { 252 if (classname == null && file == null && resource == null) { 253 throw new BuildException("At least one of (classname|file|" 254 + "resource) is required", getLocation()); 255 } 256 if (type != null) { 257 if (file == null) { 258 throw new BuildException("The type attribute is only valid " 259 + "when specifying the file " 260 + "attribute.", getLocation()); 261 } 262 } 263 if (classpath != null) { 264 classpath.setProject(getProject()); 265 this.loader = getProject().createClassLoader(classpath); 266 } 267 String appendix = ""; 268 if (isTask) { 269 appendix = " to set property " + property; 270 } else { 271 setTaskName("available"); 272 } 273 if ((classname != null) && !checkClass(classname)) { 274 log("Unable to load class " + classname + appendix, 275 Project.MSG_VERBOSE); 276 return false; 277 } 278 if ((file != null) && !checkFile()) { 279 StringBuffer buf = new StringBuffer ("Unable to find "); 280 if (type != null) { 281 buf.append(type).append(' '); 282 } 283 buf.append(filename).append(appendix); 284 log(buf.toString(), Project.MSG_VERBOSE); 285 return false; 286 } 287 if ((resource != null) && !checkResource(resource)) { 288 log("Unable to load resource " + resource + appendix, 289 Project.MSG_VERBOSE); 290 return false; 291 } 292 } finally { 293 if (loader != null) { 294 loader.cleanup(); 295 loader = null; 296 } 297 if (!isTask) { 298 setTaskName(null); 299 } 300 } 301 return true; 302 } 303 304 320 private boolean checkFile() { 321 if (filepath == null) { 322 return checkFile(file, filename); 323 } else { 324 String [] paths = filepath.list(); 325 for (int i = 0; i < paths.length; ++i) { 326 log("Searching " + paths[i], Project.MSG_DEBUG); 327 File path = new File (paths[i]); 328 329 if (path.exists() && filename.equals(paths[i])) { 332 if (type == null) { 333 log("Found: " + path, Project.MSG_VERBOSE); 334 return true; 335 } else if (type.isDir() 336 && path.isDirectory()) { 337 log("Found directory: " + path, Project.MSG_VERBOSE); 338 return true; 339 } else if (type.isFile() 340 && path.isFile()) { 341 log("Found file: " + path, Project.MSG_VERBOSE); 342 return true; 343 } 344 return false; 346 } 347 File parent = path.getParentFile(); 348 if (parent != null && parent.exists() 350 && filename.equals(parent.getAbsolutePath())) { 351 if (type == null) { 352 log("Found: " + parent, Project.MSG_VERBOSE); 353 return true; 354 } else if (type.isDir()) { 355 log("Found directory: " + parent, Project.MSG_VERBOSE); 356 return true; 357 } 358 return false; 360 } 361 if (path.exists() && path.isDirectory()) { 363 if (checkFile(new File (path, filename), 364 filename + " in " + path)) { 365 return true; 366 } 367 } 368 while (searchParents && parent != null && parent.exists()) { 370 if (checkFile(new File (parent, filename), 371 filename + " in " + parent)) { 372 return true; 373 } 374 parent = parent.getParentFile(); 375 } 376 } 377 } 378 return false; 379 } 380 381 384 private boolean checkFile(File f, String text) { 385 if (type != null) { 386 if (type.isDir()) { 387 if (f.isDirectory()) { 388 log("Found directory: " + text, Project.MSG_VERBOSE); 389 } 390 return f.isDirectory(); 391 } else if (type.isFile()) { 392 if (f.isFile()) { 393 log("Found file: " + text, Project.MSG_VERBOSE); 394 } 395 return f.isFile(); 396 } 397 } 398 if (f.exists()) { 399 log("Found: " + text, Project.MSG_VERBOSE); 400 } 401 return f.exists(); 402 } 403 404 407 private boolean checkResource(String resource) { 408 if (loader != null) { 409 return (loader.getResourceAsStream(resource) != null); 410 } else { 411 ClassLoader cL = this.getClass().getClassLoader(); 412 if (cL != null) { 413 return (cL.getResourceAsStream(resource) != null); 414 } else { 415 return 416 (ClassLoader.getSystemResourceAsStream(resource) != null); 417 } 418 } 419 } 420 421 424 private boolean checkClass(String classname) { 425 try { 426 if (ignoreSystemclasses) { 427 loader = getProject().createClassLoader(classpath); 428 loader.setParentFirst(false); 429 loader.addJavaLibraries(); 430 if (loader != null) { 431 try { 432 loader.findClass(classname); 433 } catch (SecurityException se) { 434 return true; 438 } 439 } else { 440 return false; 441 } 442 } else if (loader != null) { 443 loader.loadClass(classname); 444 } else { 445 ClassLoader l = this.getClass().getClassLoader(); 446 if (l != null) { 449 Class.forName(classname, true, l); 450 } else { 451 Class.forName(classname); 452 } 453 } 454 return true; 455 } catch (ClassNotFoundException e) { 456 log("class \"" + classname + "\" was not found", 457 Project.MSG_DEBUG); 458 return false; 459 } catch (NoClassDefFoundError e) { 460 log("Could not load dependent class \"" + e.getMessage() 461 + "\" for class \"" + classname + "\"", 462 Project.MSG_DEBUG); 463 return false; 464 } 465 } 466 467 471 public static class FileDir extends EnumeratedAttribute { 472 473 private static final String [] VALUES = {"file", "dir"}; 474 475 478 479 public String [] getValues() { 480 return VALUES; 481 } 482 483 488 public boolean isDir() { 489 return "dir".equalsIgnoreCase(getValue()); 490 } 491 492 497 public boolean isFile() { 498 return "file".equalsIgnoreCase(getValue()); 499 } 500 501 } 502 } 503 | Popular Tags |