1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.rmi.Remote ; 24 import java.util.Vector ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.taskdefs.rmic.RmicAdapter; 29 import org.apache.tools.ant.taskdefs.rmic.RmicAdapterFactory; 30 import org.apache.tools.ant.types.FilterSetCollection; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.types.Reference; 33 import org.apache.tools.ant.util.FileNameMapper; 34 import org.apache.tools.ant.util.FileUtils; 35 import org.apache.tools.ant.util.SourceFileScanner; 36 import org.apache.tools.ant.util.facade.FacadeTaskHelper; 37 38 80 81 public class Rmic extends MatchingTask { 82 83 84 public static final String ERROR_RMIC_FAILED 85 = "Rmic failed; see the compiler error output for details."; 86 87 private File baseDir; 88 private String classname; 89 private File sourceBase; 90 private String stubVersion; 91 private Path compileClasspath; 92 private Path extDirs; 93 private boolean verify = false; 94 private boolean filtering = false; 95 96 private boolean iiop = false; 97 private String iiopOpts; 98 private boolean idl = false; 99 private String idlOpts; 100 private boolean debug = false; 101 private boolean includeAntRuntime = true; 102 private boolean includeJavaRuntime = false; 103 104 private Vector compileList = new Vector (); 105 106 private ClassLoader loader = null; 107 108 private FacadeTaskHelper facade; 109 110 public static final String ERROR_UNABLE_TO_VERIFY_CLASS = "Unable to verify class "; 111 112 public static final String ERROR_NOT_FOUND = ". It could not be found."; 113 114 public static final String ERROR_NOT_DEFINED = ". It is not defined."; 115 116 public static final String ERROR_LOADING_CAUSED_EXCEPTION = ". Loading caused Exception: "; 117 118 public static final String ERROR_NO_BASE_EXISTS = "base does not exist: "; 119 120 public static final String ERROR_NOT_A_DIR = "base is not a directory:"; 121 122 public static final String ERROR_BASE_NOT_SET = "base attribute must be set!"; 123 124 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 125 126 129 public Rmic() { 130 facade = new FacadeTaskHelper(RmicAdapterFactory.DEFAULT_COMPILER); 131 } 132 133 137 public void setBase(File base) { 138 this.baseDir = base; 139 } 140 141 145 146 public File getBase() { 147 return this.baseDir; 148 } 149 150 155 public void setClassname(String classname) { 156 this.classname = classname; 157 } 158 159 163 public String getClassname() { 164 return classname; 165 } 166 167 171 public void setSourceBase(File sourceBase) { 172 this.sourceBase = sourceBase; 173 } 174 175 179 public File getSourceBase() { 180 return sourceBase; 181 } 182 183 188 public void setStubVersion(String stubVersion) { 189 this.stubVersion = stubVersion; 190 } 191 192 196 public String getStubVersion() { 197 return stubVersion; 198 } 199 200 204 public void setFiltering(boolean filter) { 205 this.filtering = filter; 206 } 207 208 212 public boolean getFiltering() { 213 return filtering; 214 } 215 216 221 public void setDebug(boolean debug) { 222 this.debug = debug; 223 } 224 225 229 public boolean getDebug() { 230 return debug; 231 } 232 233 237 public void setClasspath(Path classpath) { 238 if (compileClasspath == null) { 239 compileClasspath = classpath; 240 } else { 241 compileClasspath.append(classpath); 242 } 243 } 244 245 249 public Path createClasspath() { 250 if (compileClasspath == null) { 251 compileClasspath = new Path(getProject()); 252 } 253 return compileClasspath.createPath(); 254 } 255 256 261 public void setClasspathRef(Reference pathRef) { 262 createClasspath().setRefid(pathRef); 263 } 264 265 269 public Path getClasspath() { 270 return compileClasspath; 271 } 272 273 280 281 public void setVerify(boolean verify) { 282 this.verify = verify; 283 } 284 285 289 public boolean getVerify() { 290 return verify; 291 } 292 293 299 public void setIiop(boolean iiop) { 300 this.iiop = iiop; 301 } 302 303 307 public boolean getIiop() { 308 return iiop; 309 } 310 311 315 public void setIiopopts(String iiopOpts) { 316 this.iiopOpts = iiopOpts; 317 } 318 319 323 public String getIiopopts() { 324 return iiopOpts; 325 } 326 327 333 public void setIdl(boolean idl) { 334 this.idl = idl; 335 } 336 337 341 public boolean getIdl() { 342 return idl; 343 } 344 345 349 public void setIdlopts(String idlOpts) { 350 this.idlOpts = idlOpts; 351 } 352 353 357 public String getIdlopts() { 358 return idlOpts; 359 } 360 361 365 public Vector getFileList() { 366 return compileList; 367 } 368 369 375 public void setIncludeantruntime(boolean include) { 376 includeAntRuntime = include; 377 } 378 379 384 public boolean getIncludeantruntime() { 385 return includeAntRuntime; 386 } 387 388 395 public void setIncludejavaruntime(boolean include) { 396 includeJavaRuntime = include; 397 } 398 399 404 public boolean getIncludejavaruntime() { 405 return includeJavaRuntime; 406 } 407 408 413 public void setExtdirs(Path extDirs) { 414 if (this.extDirs == null) { 415 this.extDirs = extDirs; 416 } else { 417 this.extDirs.append(extDirs); 418 } 419 } 420 421 425 public Path createExtdirs() { 426 if (extDirs == null) { 427 extDirs = new Path(getProject()); 428 } 429 return extDirs.createPath(); 430 } 431 432 437 public Path getExtdirs() { 438 return extDirs; 439 } 440 441 444 public Vector getCompileList() { 445 return compileList; 446 } 447 448 455 public void setCompiler(String compiler) { 456 if (compiler.length() > 0) { 457 facade.setImplementation(compiler); 458 } 459 } 460 461 466 public String getCompiler() { 467 facade.setMagicValue(getProject().getProperty("build.rmic")); 468 return facade.getImplementation(); 469 } 470 471 476 public ImplementationSpecificArgument createCompilerArg() { 477 ImplementationSpecificArgument arg = 478 new ImplementationSpecificArgument(); 479 facade.addImplementationArgument(arg); 480 return arg; 481 } 482 483 488 public String [] getCurrentCompilerArgs() { 489 getCompiler(); 490 return facade.getArgs(); 491 } 492 493 499 public void execute() throws BuildException { 500 if (baseDir == null) { 501 throw new BuildException(ERROR_BASE_NOT_SET, getLocation()); 502 } 503 if (!baseDir.exists()) { 504 throw new BuildException(ERROR_NO_BASE_EXISTS + baseDir, getLocation()); 505 } 506 if (!baseDir.isDirectory()) { 507 throw new BuildException(ERROR_NOT_A_DIR + baseDir, getLocation()); 508 } 509 if (verify) { 510 log("Verify has been turned on.", Project.MSG_VERBOSE); 511 } 512 513 RmicAdapter adapter = RmicAdapterFactory.getRmic(getCompiler(), this); 514 515 adapter.setRmic(this); 517 518 Path classpath = adapter.getClasspath(); 519 loader = getProject().createClassLoader(classpath); 520 521 try { 522 if (classname == null) { 525 DirectoryScanner ds = this.getDirectoryScanner(baseDir); 526 String [] files = ds.getIncludedFiles(); 527 scanDir(baseDir, files, adapter.getMapper()); 528 } else { 529 scanDir(baseDir, 531 new String [] {classname.replace('.', 532 File.separatorChar) 533 + ".class"}, 534 adapter.getMapper()); 535 } 536 537 int fileCount = compileList.size(); 538 if (fileCount > 0) { 539 log("RMI Compiling " + fileCount 540 + " class" + (fileCount > 1 ? "es" : "") + " to " + baseDir, 541 Project.MSG_INFO); 542 543 if (!adapter.execute()) { 545 throw new BuildException(ERROR_RMIC_FAILED, getLocation()); 546 } 547 } 548 549 554 if (null != sourceBase && !baseDir.equals(sourceBase) 555 && fileCount > 0) { 556 if (idl) { 557 log("Cannot determine sourcefiles in idl mode, ", 558 Project.MSG_WARN); 559 log("sourcebase attribute will be ignored.", 560 Project.MSG_WARN); 561 } else { 562 for (int j = 0; j < fileCount; j++) { 563 moveGeneratedFile(baseDir, sourceBase, 564 (String ) compileList.elementAt(j), 565 adapter); 566 } 567 } 568 } 569 } finally { 570 compileList.removeAllElements(); 571 } 572 } 573 574 580 private void moveGeneratedFile (File baseDir, File sourceBaseFile, 581 String classname, 582 RmicAdapter adapter) 583 throws BuildException { 584 585 String classFileName = 586 classname.replace('.', File.separatorChar) + ".class"; 587 String [] generatedFiles = 588 adapter.getMapper().mapFileName(classFileName); 589 590 for (int i = 0; i < generatedFiles.length; i++) { 591 final String generatedFile = generatedFiles[i]; 592 if (!generatedFile.endsWith(".class")) { 593 continue; 596 } 597 598 final int pos = generatedFile.length() - ".class".length(); 599 String sourceFileName = 600 generatedFile.substring(0, pos) + ".java"; 601 602 File oldFile = new File (baseDir, sourceFileName); 603 if (!oldFile.exists()) { 604 continue; 606 } 607 608 File newFile = new File (sourceBaseFile, sourceFileName); 609 try { 610 if (filtering) { 611 FILE_UTILS.copyFile(oldFile, newFile, 612 new FilterSetCollection(getProject() 613 .getGlobalFilterSet())); 614 } else { 615 FILE_UTILS.copyFile(oldFile, newFile); 616 } 617 oldFile.delete(); 618 } catch (IOException ioe) { 619 String msg = "Failed to copy " + oldFile + " to " 620 + newFile + " due to " + ioe.getMessage(); 621 throw new BuildException(msg, ioe, getLocation()); 622 } 623 } 624 } 625 626 633 protected void scanDir(File baseDir, String [] files, 634 FileNameMapper mapper) { 635 636 String [] newFiles = files; 637 if (idl) { 638 log("will leave uptodate test to rmic implementation in idl mode.", 639 Project.MSG_VERBOSE); 640 } else if (iiop 641 && iiopOpts != null && iiopOpts.indexOf("-always") > -1) { 642 log("no uptodate test as -always option has been specified", 643 Project.MSG_VERBOSE); 644 } else { 645 SourceFileScanner sfs = new SourceFileScanner(this); 646 newFiles = sfs.restrict(files, baseDir, baseDir, mapper); 647 } 648 649 for (int i = 0; i < newFiles.length; i++) { 650 String name = newFiles[i].replace(File.separatorChar, '.'); 651 name = name.substring(0, name.lastIndexOf(".class")); 652 compileList.addElement(name); 653 } 654 } 655 656 661 public boolean isValidRmiRemote(String classname) { 662 try { 663 Class testClass = loader.loadClass(classname); 664 if (testClass.isInterface() && !iiop && !idl) { 666 return false; 667 } 668 return isValidRmiRemote(testClass); 669 } catch (ClassNotFoundException e) { 670 log(ERROR_UNABLE_TO_VERIFY_CLASS + classname 671 + ERROR_NOT_FOUND, Project.MSG_WARN); 672 } catch (NoClassDefFoundError e) { 673 log(ERROR_UNABLE_TO_VERIFY_CLASS + classname 674 + ERROR_NOT_DEFINED, Project.MSG_WARN); 675 } catch (Throwable t) { 676 log(ERROR_UNABLE_TO_VERIFY_CLASS + classname 677 + ERROR_LOADING_CAUSED_EXCEPTION 678 + t.getMessage(), Project.MSG_WARN); 679 } 680 return false; 682 } 683 684 691 public Class getRemoteInterface(Class testClass) { 692 if (Remote .class.isAssignableFrom(testClass)) { 693 Class [] interfaces = testClass.getInterfaces(); 694 if (interfaces != null) { 695 for (int i = 0; i < interfaces.length; i++) { 696 if (Remote .class.isAssignableFrom(interfaces[i])) { 697 return interfaces[i]; 698 } 699 } 700 } 701 } 702 return null; 703 } 704 705 709 private boolean isValidRmiRemote (Class testClass) { 710 return getRemoteInterface(testClass) != null; 711 } 712 713 717 public ClassLoader getLoader() { 718 return loader; 719 } 720 721 726 public class ImplementationSpecificArgument extends 727 org.apache.tools.ant.util.facade.ImplementationSpecificArgument { 728 729 737 public void setCompiler(String impl) { 738 super.setImplementation(impl); 739 } 740 } 741 742 } 743 744 | Popular Tags |