1 18 19 package org.apache.tools.ant.taskdefs.optional.jdepend; 20 21 import java.io.File ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.lang.reflect.Constructor ; 26 import java.lang.reflect.Method ; 27 import java.util.Vector ; 28 import java.util.Enumeration ; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 import org.apache.tools.ant.taskdefs.Execute; 33 import org.apache.tools.ant.taskdefs.ExecuteWatchdog; 34 import org.apache.tools.ant.taskdefs.LogStreamHandler; 35 import org.apache.tools.ant.types.Commandline; 36 import org.apache.tools.ant.types.CommandlineJava; 37 import org.apache.tools.ant.types.EnumeratedAttribute; 38 import org.apache.tools.ant.types.Path; 39 import org.apache.tools.ant.types.PatternSet; 40 import org.apache.tools.ant.types.Reference; 41 import org.apache.tools.ant.util.FileUtils; 42 import org.apache.tools.ant.util.LoaderUtils; 43 44 54 public class JDependTask extends Task { 55 57 private Path sourcesPath; private Path classesPath; 61 private File outputFile; 63 private File dir; 64 private Path compileClasspath; 65 private boolean haltonerror = false; 66 private boolean fork = false; 67 private Long timeout = null; 68 69 private String jvm = null; 70 private String format = "text"; 71 private PatternSet defaultPatterns = new PatternSet(); 72 73 private static Constructor packageFilterC; 74 private static Method setFilter; 75 76 private boolean includeRuntime = false; 77 private Path runtimeClasses = null; 78 79 static { 80 try { 81 Class packageFilter = 82 Class.forName("jdepend.framework.PackageFilter"); 83 packageFilterC = 84 packageFilter.getConstructor(new Class [] {java.util.Collection .class}); 85 setFilter = 86 jdepend.textui.JDepend.class.getDeclaredMethod("setFilter", 87 new Class [] {packageFilter}); 88 } catch (Throwable t) { 89 if (setFilter == null) { 90 packageFilterC = null; 91 } 92 } 93 } 94 95 102 public void setIncluderuntime(boolean b) { 103 includeRuntime = b; 104 } 105 106 115 public void setTimeout(Long value) { 116 timeout = value; 117 } 118 119 122 public Long getTimeout() { 123 return timeout; 124 } 125 126 131 public void setOutputFile(File outputFile) { 132 this.outputFile = outputFile; 133 } 134 135 138 public File getOutputFile() { 139 return outputFile; 140 } 141 142 146 public void setHaltonerror(boolean haltonerror) { 147 this.haltonerror = haltonerror; 148 } 149 150 153 public boolean getHaltonerror() { 154 return haltonerror; 155 } 156 157 163 public void setFork(boolean value) { 164 fork = value; 165 } 166 167 170 public boolean getFork() { 171 return fork; 172 } 173 174 181 public void setJvm(String value) { 182 jvm = value; 183 184 } 185 186 191 public Path createSourcespath() { 192 if (sourcesPath == null) { 193 sourcesPath = new Path(getProject()); 194 } 195 return sourcesPath.createPath(); 196 } 197 198 203 public Path getSourcespath() { 204 return sourcesPath; 205 } 206 207 211 public Path createClassespath() { 212 if (classesPath == null) { 213 classesPath = new Path(getProject()); 214 } 215 return classesPath.createPath(); 216 } 217 218 222 public Path getClassespath() { 223 return classesPath; 224 } 225 226 231 public void setDir(File dir) { 232 this.dir = dir; 233 } 234 235 238 public File getDir() { 239 return dir; 240 } 241 242 246 public void setClasspath(Path classpath) { 247 if (compileClasspath == null) { 248 compileClasspath = classpath; 249 } else { 250 compileClasspath.append(classpath); 251 } 252 } 253 254 258 public Path getClasspath() { 259 return compileClasspath; 260 } 261 262 266 public Path createClasspath() { 267 if (compileClasspath == null) { 268 compileClasspath = new Path(getProject()); 269 } 270 return compileClasspath.createPath(); 271 } 272 273 280 public Commandline.Argument createJvmarg(CommandlineJava commandline) { 281 return commandline.createVmArgument(); 282 } 283 284 288 public void setClasspathRef(Reference r) { 289 createClasspath().setRefid(r); 290 } 291 292 296 public PatternSet.NameEntry createExclude() { 297 return defaultPatterns.createExclude(); 298 } 299 300 303 public PatternSet getExcludes() { 304 return defaultPatterns; 305 } 306 307 312 public void setFormat(FormatAttribute ea) { 313 format = ea.getValue(); 314 } 315 316 321 public static class FormatAttribute extends EnumeratedAttribute { 322 private String [] formats = new String []{"xml", "text"}; 323 324 327 public String [] getValues() { 328 return formats; 329 } 330 } 331 332 335 private static final int SUCCESS = 0; 336 339 private static final int ERRORS = 1; 340 341 351 private void addClasspathEntry(String resource) { 352 361 if (resource.startsWith("/")) { 362 resource = resource.substring(1); 363 } else { 364 resource = "org/apache/tools/ant/taskdefs/optional/jdepend/" 365 + resource; 366 } 367 368 File f = LoaderUtils.getResourceSource(getClass().getClassLoader(), 369 resource); 370 if (f != null) { 371 log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG); 372 runtimeClasses.createPath().setLocation(f); 373 } else { 374 log("Couldn\'t find " + resource, Project.MSG_DEBUG); 375 } 376 } 377 378 383 public void execute() throws BuildException { 384 385 CommandlineJava commandline = new CommandlineJava(); 386 387 if ("text".equals(format)) { 388 commandline.setClassname("jdepend.textui.JDepend"); 389 } else 390 if ("xml".equals(format)) { 391 commandline.setClassname("jdepend.xmlui.JDepend"); 392 } 393 394 if (jvm != null) { 395 commandline.setVm(jvm); 396 } 397 if (getSourcespath() == null && getClassespath() == null) { 398 throw new BuildException("Missing classespath required argument"); 399 } else if (getClassespath() == null) { 400 String msg = 401 "sourcespath is deprecated in JDepend >= 2.5 " 402 + "- please convert to classespath"; 403 log(msg); 404 } 405 406 int exitValue = JDependTask.ERRORS; 408 boolean wasKilled = false; 409 if (!getFork()) { 410 exitValue = executeInVM(commandline); 411 } else { 412 ExecuteWatchdog watchdog = createWatchdog(); 413 exitValue = executeAsForked(commandline, watchdog); 414 if (watchdog != null) { 416 wasKilled = watchdog.killedProcess(); 417 } 418 } 419 420 boolean errorOccurred = exitValue == JDependTask.ERRORS || wasKilled; 423 424 if (errorOccurred) { 425 String errorMessage = "JDepend FAILED" 426 + (wasKilled ? " - Timed out" : ""); 427 428 if (getHaltonerror()) { 429 throw new BuildException(errorMessage, getLocation()); 430 } else { 431 log(errorMessage, Project.MSG_ERR); 432 } 433 } 434 } 435 436 441 448 public int executeInVM(CommandlineJava commandline) throws BuildException { 449 jdepend.textui.JDepend jdepend; 450 451 if ("xml".equals(format)) { 452 jdepend = new jdepend.xmlui.JDepend(); 453 } else { 454 jdepend = new jdepend.textui.JDepend(); 455 } 456 457 FileWriter fw = null; 458 if (getOutputFile() != null) { 459 try { 460 fw = new FileWriter (getOutputFile().getPath()); 461 } catch (IOException e) { 462 String msg = "JDepend Failed when creating the output file: " 463 + e.getMessage(); 464 log(msg); 465 throw new BuildException(msg); 466 } 467 jdepend.setWriter(new PrintWriter (fw)); 468 log("Output to be stored in " + getOutputFile().getPath()); 469 } 470 471 472 try { 473 if (getClassespath() != null) { 474 String [] cP = getClassespath().list(); 478 for (int i = 0; i < cP.length; i++) { 479 File f = new File (cP[i]); 480 if (!f.exists()) { 483 String msg = "\"" 484 + f.getPath() 485 + "\" does not represent a valid" 486 + " file or directory. JDepend would fail."; 487 log(msg); 488 throw new BuildException(msg); 489 } 490 try { 491 jdepend.addDirectory(f.getPath()); 492 } catch (IOException e) { 493 String msg = 494 "JDepend Failed when adding a class directory: " 495 + e.getMessage(); 496 log(msg); 497 throw new BuildException(msg); 498 } 499 } 500 501 } else if (getSourcespath() != null) { 502 503 String [] sP = getSourcespath().list(); 506 for (int i = 0; i < sP.length; i++) { 507 File f = new File (sP[i]); 508 509 if (!f.exists() || !f.isDirectory()) { 512 String msg = "\"" 513 + f.getPath() 514 + "\" does not represent a valid" 515 + " directory. JDepend would fail."; 516 log(msg); 517 throw new BuildException(msg); 518 } 519 try { 520 jdepend.addDirectory(f.getPath()); 521 } catch (IOException e) { 522 String msg = 523 "JDepend Failed when adding a source directory: " 524 + e.getMessage(); 525 log(msg); 526 throw new BuildException(msg); 527 } 528 } 529 } 530 531 String [] patterns = defaultPatterns.getExcludePatterns(getProject()); 533 if (patterns != null && patterns.length > 0) { 534 if (setFilter != null) { 535 Vector v = new Vector (); 536 for (int i = 0; i < patterns.length; i++) { 537 v.addElement(patterns[i]); 538 } 539 try { 540 Object o = packageFilterC.newInstance(new Object [] {v}); 541 setFilter.invoke(jdepend, new Object [] {o}); 542 } catch (Throwable e) { 543 log("excludes will be ignored as JDepend doesn't like me: " 544 + e.getMessage(), Project.MSG_WARN); 545 } 546 } else { 547 log("Sorry, your version of JDepend doesn't support excludes", 548 Project.MSG_WARN); 549 } 550 } 551 552 jdepend.analyze(); 553 } finally { 554 FileUtils.close(fw); 555 } 556 return SUCCESS; 557 } 558 559 560 570 public int executeAsForked(CommandlineJava commandline, 572 ExecuteWatchdog watchdog) throws BuildException { 573 runtimeClasses = new Path(getProject()); 574 addClasspathEntry("/jdepend/textui/JDepend.class"); 575 576 createClasspath(); 578 579 if (getClasspath().toString().length() > 0) { 582 createJvmarg(commandline).setValue("-classpath"); 583 createJvmarg(commandline).setValue(getClasspath().toString()); 584 } 585 586 if (includeRuntime) { 587 Vector v = Execute.getProcEnvironment(); 588 Enumeration e = v.elements(); 589 while (e.hasMoreElements()) { 590 String s = (String ) e.nextElement(); 591 if (s.startsWith("CLASSPATH=")) { 592 commandline.createClasspath(getProject()).createPath() 593 .append(new Path(getProject(), 594 s.substring("CLASSPATH=".length() 595 ))); 596 } 597 } 598 log("Implicitly adding " + runtimeClasses + " to CLASSPATH", 599 Project.MSG_VERBOSE); 600 commandline.createClasspath(getProject()).createPath() 601 .append(runtimeClasses); 602 } 603 604 if (getOutputFile() != null) { 605 commandline.createArgument().setValue("-file"); 609 commandline.createArgument().setValue(outputFile.getPath()); 610 } 612 613 if (getSourcespath() != null) { 614 String [] sP = getSourcespath().list(); 616 for (int i = 0; i < sP.length; i++) { 617 File f = new File (sP[i]); 618 619 if (!f.exists() || !f.isDirectory()) { 622 throw new BuildException("\"" + f.getPath() 623 + "\" does not represent a valid" 624 + " directory. JDepend would" 625 + " fail."); 626 } 627 commandline.createArgument().setValue(f.getPath()); 628 } 629 } 630 631 if (getClassespath() != null) { 632 String [] cP = getClassespath().list(); 635 for (int i = 0; i < cP.length; i++) { 636 File f = new File (cP[i]); 637 if (!f.exists()) { 640 throw new BuildException("\"" + f.getPath() 641 + "\" does not represent a valid" 642 + " file or directory. JDepend would" 643 + " fail."); 644 } 645 commandline.createArgument().setValue(f.getPath()); 646 } 647 } 648 649 Execute execute = new Execute(new LogStreamHandler(this, 650 Project.MSG_INFO, Project.MSG_WARN), watchdog); 651 execute.setCommandline(commandline.getCommandline()); 652 if (getDir() != null) { 653 execute.setWorkingDirectory(getDir()); 654 execute.setAntRun(getProject()); 655 } 656 657 if (getOutputFile() != null) { 658 log("Output to be stored in " + getOutputFile().getPath()); 659 } 660 log(commandline.describeCommand(), Project.MSG_VERBOSE); 661 try { 662 return execute.execute(); 663 } catch (IOException e) { 664 throw new BuildException("Process fork failed.", e, getLocation()); 665 } 666 } 667 668 673 protected ExecuteWatchdog createWatchdog() throws BuildException { 674 if (getTimeout() == null) { 675 return null; 676 } 677 return new ExecuteWatchdog(getTimeout().longValue()); 678 } 679 } 680 | Popular Tags |