You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rmic.java 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.AntClassLoader;
  56. import org.apache.tools.ant.BuildException;
  57. import org.apache.tools.ant.DirectoryScanner;
  58. import org.apache.tools.ant.Project;
  59. import org.apache.tools.ant.taskdefs.rmic.RmicAdapter;
  60. import org.apache.tools.ant.taskdefs.rmic.RmicAdapterFactory;
  61. import org.apache.tools.ant.types.FilterSetCollection;
  62. import org.apache.tools.ant.types.Path;
  63. import org.apache.tools.ant.types.Reference;
  64. import org.apache.tools.ant.util.FileNameMapper;
  65. import org.apache.tools.ant.util.FileUtils;
  66. import org.apache.tools.ant.util.SourceFileScanner;
  67. import org.apache.tools.ant.util.facade.FacadeTaskHelper;
  68. import java.io.File;
  69. import java.io.IOException;
  70. import java.rmi.Remote;
  71. import java.util.Vector;
  72. /**
  73. * Task to compile RMI stubs and skeletons. This task can take the following
  74. * arguments:
  75. * <ul>
  76. * <li>base: The base directory for the compiled stubs and skeletons
  77. * <li>class: The name of the class to generate the stubs from
  78. * <li>stubVersion: The version of the stub prototol to use (1.1, 1.2, compat)
  79. * <li>sourceBase: The base directory for the generated stubs and skeletons
  80. * <li>classpath: Additional classpath, appended before the system classpath
  81. * <li>iiop: Generate IIOP compatable output
  82. * <li>iiopopts: Include IIOP options
  83. * <li>idl: Generate IDL output
  84. * <li>idlopts: Include IDL options
  85. * <li>includeantruntime
  86. * <li>includejavaruntime
  87. * <li>extdirs
  88. * </ul>
  89. * Of these arguments, <b>base</b> is required.
  90. * <p>
  91. * If classname is specified then only that classname will be compiled. If it
  92. * is absent, then <b>base</b> is traversed for classes according to patterns.
  93. * <p>
  94. *
  95. * @author duncan@x180.com
  96. * @author ludovic.claude@websitewatchers.co.uk
  97. * @author David Maclean <a href="mailto:david@cm.co.za">david@cm.co.za</a>
  98. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  99. * @author Takashi Okamoto tokamoto@rd.nttdata.co.jp
  100. *
  101. * @since Ant 1.1
  102. *
  103. * @ant.task category="java"
  104. */
  105. public class Rmic extends MatchingTask {
  106. private final static String FAIL_MSG
  107. = "Rmic failed; see the compiler error output for details.";
  108. private File baseDir;
  109. private String classname;
  110. private File sourceBase;
  111. private String stubVersion;
  112. private Path compileClasspath;
  113. private Path extdirs;
  114. private boolean verify = false;
  115. private boolean filtering = false;
  116. private boolean iiop = false;
  117. private String iiopopts;
  118. private boolean idl = false;
  119. private String idlopts;
  120. private boolean debug = false;
  121. private boolean includeAntRuntime = true;
  122. private boolean includeJavaRuntime = false;
  123. private Vector compileList = new Vector();
  124. private ClassLoader loader = null;
  125. private FileUtils fileUtils = FileUtils.newFileUtils();
  126. private FacadeTaskHelper facade;
  127. public Rmic() {
  128. try {
  129. Class.forName("kaffe.rmi.rmic.RMIC");
  130. facade = new FacadeTaskHelper("kaffe");
  131. } catch (ClassNotFoundException cnfe) {
  132. facade = new FacadeTaskHelper("sun");
  133. }
  134. }
  135. /** Sets the base directory to output generated class. */
  136. public void setBase(File base) {
  137. this.baseDir = base;
  138. }
  139. /** Gets the base directory to output generated class. */
  140. public File getBase() {
  141. return this.baseDir;
  142. }
  143. /** Sets the class name to compile. */
  144. public void setClassname(String classname) {
  145. this.classname = classname;
  146. }
  147. /** Gets the class name to compile. */
  148. public String getClassname() {
  149. return classname;
  150. }
  151. /** Sets the source dirs to find the source java files. */
  152. public void setSourceBase(File sourceBase) {
  153. this.sourceBase = sourceBase;
  154. }
  155. /** Gets the source dirs to find the source java files. */
  156. public File getSourceBase() {
  157. return sourceBase;
  158. }
  159. /** Sets the stub version. */
  160. public void setStubVersion(String stubVersion) {
  161. this.stubVersion = stubVersion;
  162. }
  163. public String getStubVersion() {
  164. return stubVersion;
  165. }
  166. public void setFiltering(boolean filter) {
  167. filtering = filter;
  168. }
  169. public boolean getFiltering() {
  170. return filtering;
  171. }
  172. /** Sets the debug flag. */
  173. public void setDebug(boolean debug) {
  174. this.debug = debug;
  175. }
  176. /** Gets the debug flag. */
  177. public boolean getDebug() {
  178. return debug;
  179. }
  180. /**
  181. * Set the classpath to be used for this compilation.
  182. */
  183. public void setClasspath(Path classpath) {
  184. if (compileClasspath == null) {
  185. compileClasspath = classpath;
  186. } else {
  187. compileClasspath.append(classpath);
  188. }
  189. }
  190. /**
  191. * Creates a nested classpath element.
  192. */
  193. public Path createClasspath() {
  194. if (compileClasspath == null) {
  195. compileClasspath = new Path(project);
  196. }
  197. return compileClasspath.createPath();
  198. }
  199. /**
  200. * Adds a reference to a CLASSPATH defined elsewhere.
  201. */
  202. public void setClasspathRef(Reference r) {
  203. createClasspath().setRefid(r);
  204. }
  205. /**
  206. * Gets the classpath.
  207. */
  208. public Path getClasspath() {
  209. return compileClasspath;
  210. }
  211. /**
  212. * Indicates that the classes found by the directory match should be
  213. * checked to see if they implement java.rmi.Remote.
  214. * This defaults to false if not set. */
  215. public void setVerify(boolean verify) {
  216. this.verify = verify;
  217. }
  218. /** Get verify flag. */
  219. public boolean getVerify() {
  220. return verify;
  221. }
  222. /**
  223. * Indicates that IIOP compatible stubs should
  224. * be generated. This defaults to false
  225. * if not set.
  226. */
  227. public void setIiop(boolean iiop) {
  228. this.iiop = iiop;
  229. }
  230. /** Gets iiop flags. */
  231. public boolean getIiop() {
  232. return iiop;
  233. }
  234. /**
  235. * pass additional arguments for iiop
  236. */
  237. public void setIiopopts(String iiopopts) {
  238. this.iiopopts = iiopopts;
  239. }
  240. /** Gets additional arguments for iiop. */
  241. public String getIiopopts() {
  242. return iiopopts;
  243. }
  244. /**
  245. * Indicates that IDL output should be
  246. * generated. This defaults to false
  247. * if not set.
  248. */
  249. public void setIdl(boolean idl) {
  250. this.idl = idl;
  251. }
  252. /* Gets IDL flags. */
  253. public boolean getIdl() {
  254. return idl;
  255. }
  256. /**
  257. * pass additional arguments for idl compile
  258. */
  259. public void setIdlopts(String idlopts) {
  260. this.idlopts = idlopts;
  261. }
  262. /**
  263. * Gets additional arguments for idl compile.
  264. */
  265. public String getIdlopts() {
  266. return idlopts;
  267. }
  268. /** Gets file list to compile. */
  269. public Vector getFileList() {
  270. return compileList;
  271. }
  272. /**
  273. * Include ant's own classpath in this task's classpath?
  274. */
  275. public void setIncludeantruntime(boolean include) {
  276. includeAntRuntime = include;
  277. }
  278. /**
  279. * Gets whether or not the ant classpath is to be included in the
  280. * task's classpath.
  281. */
  282. public boolean getIncludeantruntime() {
  283. return includeAntRuntime;
  284. }
  285. /**
  286. * Sets whether or not to include the java runtime libraries to this
  287. * task's classpath.
  288. */
  289. public void setIncludejavaruntime(boolean include) {
  290. includeJavaRuntime = include;
  291. }
  292. /**
  293. * Gets whether or not the java runtime should be included in this
  294. * task's classpath.
  295. */
  296. public boolean getIncludejavaruntime() {
  297. return includeJavaRuntime;
  298. }
  299. /**
  300. * Sets the extension directories that will be used during the
  301. * compilation.
  302. */
  303. public void setExtdirs(Path extdirs) {
  304. if (this.extdirs == null) {
  305. this.extdirs = extdirs;
  306. } else {
  307. this.extdirs.append(extdirs);
  308. }
  309. }
  310. /**
  311. * Maybe creates a nested extdirs element.
  312. */
  313. public Path createExtdirs() {
  314. if (extdirs == null) {
  315. extdirs = new Path(project);
  316. }
  317. return extdirs.createPath();
  318. }
  319. /**
  320. * Gets the extension directories that will be used during the
  321. * compilation.
  322. */
  323. public Path getExtdirs() {
  324. return extdirs;
  325. }
  326. public Vector getCompileList() {
  327. return compileList;
  328. }
  329. /**
  330. * @since Ant 1.5
  331. */
  332. public void setCompiler(String compiler) {
  333. facade.setImplementation(compiler);
  334. }
  335. /**
  336. * @since Ant 1.5
  337. */
  338. public String getCompiler() {
  339. facade.setMagicValue(getProject().getProperty("build.rmic"));
  340. return facade.getImplementation();
  341. }
  342. /**
  343. * Adds an implementation specific command line argument.
  344. * @since Ant 1.5
  345. */
  346. public ImplementationSpecificArgument createCompilerArg() {
  347. ImplementationSpecificArgument arg =
  348. new ImplementationSpecificArgument();
  349. facade.addImplementationArgument(arg);
  350. return arg;
  351. }
  352. /**
  353. * Get the additional implementation specific command line arguments.
  354. * @return array of command line arguments, guaranteed to be non-null.
  355. * @since Ant 1.5
  356. */
  357. public String[] getCurrentCompilerArgs() {
  358. getCompiler();
  359. return facade.getArgs();
  360. }
  361. public void execute() throws BuildException {
  362. if (baseDir == null) {
  363. throw new BuildException("base attribute must be set!", location);
  364. }
  365. if (!baseDir.exists()) {
  366. throw new BuildException("base does not exist!", location);
  367. }
  368. if (verify) {
  369. log("Verify has been turned on.", Project.MSG_INFO);
  370. }
  371. RmicAdapter adapter = RmicAdapterFactory.getRmic(getCompiler(), this);
  372. // now we need to populate the compiler adapter
  373. adapter.setRmic(this);
  374. Path classpath = adapter.getClasspath();
  375. loader = new AntClassLoader(project, classpath);
  376. try {
  377. // scan base dirs to build up compile lists only if a
  378. // specific classname is not given
  379. if (classname == null) {
  380. DirectoryScanner ds = this.getDirectoryScanner(baseDir);
  381. String[] files = ds.getIncludedFiles();
  382. scanDir(baseDir, files, adapter.getMapper());
  383. } else {
  384. // otherwise perform a timestamp comparison - at least
  385. scanDir(baseDir,
  386. new String[] {classname.replace('.',
  387. File.separatorChar)
  388. + ".class"},
  389. adapter.getMapper());
  390. }
  391. int fileCount = compileList.size();
  392. if (fileCount > 0) {
  393. log("RMI Compiling " + fileCount +
  394. " class" + (fileCount > 1 ? "es" : "") + " to " + baseDir,
  395. Project.MSG_INFO);
  396. // finally, lets execute the compiler!!
  397. if (!adapter.execute()) {
  398. throw new BuildException(FAIL_MSG, location);
  399. }
  400. }
  401. /*
  402. * Move the generated source file to the base directory. If
  403. * base directory and sourcebase are the same, the generated
  404. * sources are already in place.
  405. */
  406. if (null != sourceBase && !baseDir.equals(sourceBase)
  407. && fileCount > 0) {
  408. if (idl) {
  409. log("Cannot determine sourcefiles in idl mode, ",
  410. Project.MSG_WARN);
  411. log("sourcebase attribute will be ignored.",
  412. Project.MSG_WARN);
  413. } else {
  414. for (int j = 0; j < fileCount; j++) {
  415. moveGeneratedFile(baseDir, sourceBase,
  416. (String) compileList.elementAt(j),
  417. adapter);
  418. }
  419. }
  420. }
  421. } finally {
  422. compileList.removeAllElements();
  423. }
  424. }
  425. /**
  426. * Move the generated source file(s) to the base directory
  427. *
  428. * @exception org.apache.tools.ant.BuildException When error
  429. * copying/removing files.
  430. */
  431. private void moveGeneratedFile (File baseDir, File sourceBaseFile,
  432. String classname,
  433. RmicAdapter adapter)
  434. throws BuildException {
  435. String classFileName =
  436. classname.replace('.', File.separatorChar) + ".class";
  437. String[] generatedFiles =
  438. adapter.getMapper().mapFileName(classFileName);
  439. for (int i = 0; i < generatedFiles.length; i++) {
  440. if (!generatedFiles[i].endsWith(".class")) {
  441. // don't know how to handle that - a IDL file doesn't
  442. // have a corresponding Java source for example.
  443. continue;
  444. }
  445. String sourceFileName =
  446. generatedFiles[i].substring(0, classFileName.length() - 6)
  447. + ".java";
  448. File oldFile = new File(baseDir, sourceFileName);
  449. if (!oldFile.exists()) {
  450. // no source file generated, nothing to move
  451. continue;
  452. }
  453. File newFile = new File(sourceBaseFile, sourceFileName);
  454. try {
  455. if (filtering) {
  456. fileUtils.copyFile(oldFile, newFile,
  457. new FilterSetCollection(getProject()
  458. .getGlobalFilterSet()));
  459. } else {
  460. fileUtils.copyFile(oldFile, newFile);
  461. }
  462. oldFile.delete();
  463. } catch (IOException ioe) {
  464. String msg = "Failed to copy " + oldFile + " to " +
  465. newFile + " due to " + ioe.getMessage();
  466. throw new BuildException(msg, ioe, location);
  467. }
  468. }
  469. }
  470. /**
  471. * Scans the directory looking for class files to be compiled.
  472. * The result is returned in the class variable compileList.
  473. */
  474. protected void scanDir(File baseDir, String[] files,
  475. FileNameMapper mapper) {
  476. String[] newFiles = files;
  477. if (idl) {
  478. log("will leave uptodate test to rmic implementation in idl mode.",
  479. Project.MSG_VERBOSE);
  480. } else if (iiop
  481. && iiopopts != null && iiopopts.indexOf("-always") > -1) {
  482. log("no uptodate test as -always option has been specified",
  483. Project.MSG_VERBOSE);
  484. } else {
  485. SourceFileScanner sfs = new SourceFileScanner(this);
  486. newFiles = sfs.restrict(files, baseDir, baseDir, mapper);
  487. }
  488. for (int i = 0; i < newFiles.length; i++) {
  489. String classname = newFiles[i].replace(File.separatorChar, '.');
  490. classname = classname.substring(0, classname.lastIndexOf(".class"));
  491. compileList.addElement(classname);
  492. }
  493. }
  494. /**
  495. * Load named class and test whether it can be rmic'ed
  496. */
  497. public boolean isValidRmiRemote(String classname) {
  498. try {
  499. Class testClass = loader.loadClass(classname);
  500. // One cannot RMIC an interface for "classic" RMI (JRMP)
  501. if (testClass.isInterface() && !iiop && !idl) {
  502. return false;
  503. }
  504. return isValidRmiRemote(testClass);
  505. } catch (ClassNotFoundException e) {
  506. log("Unable to verify class " + classname +
  507. ". It could not be found.", Project.MSG_WARN);
  508. } catch (NoClassDefFoundError e) {
  509. log("Unable to verify class " + classname +
  510. ". It is not defined.", Project.MSG_WARN);
  511. } catch (Throwable t) {
  512. log("Unable to verify class " + classname +
  513. ". Loading caused Exception: " +
  514. t.getMessage(), Project.MSG_WARN);
  515. }
  516. // we only get here if an exception has been thrown
  517. return false;
  518. }
  519. /**
  520. * Returns the topmost interface that extends Remote for a given
  521. * class - if one exists.
  522. */
  523. public Class getRemoteInterface(Class testClass) {
  524. if (Remote.class.isAssignableFrom(testClass)) {
  525. Class [] interfaces = testClass.getInterfaces();
  526. if (interfaces != null) {
  527. for (int i = 0; i < interfaces.length; i++) {
  528. if (Remote.class.isAssignableFrom(interfaces[i])) {
  529. return interfaces[i];
  530. }
  531. }
  532. }
  533. }
  534. return null;
  535. }
  536. /**
  537. * Check to see if the class or (super)interfaces implement
  538. * java.rmi.Remote.
  539. */
  540. private boolean isValidRmiRemote (Class testClass) {
  541. return getRemoteInterface(testClass) != null;
  542. }
  543. /**
  544. * Classloader for the user-specified classpath.
  545. */
  546. public ClassLoader getLoader() {
  547. return loader;
  548. }
  549. /**
  550. * Adds an "compiler" attribute to Commandline$Attribute used to
  551. * filter command line attributes based on the current
  552. * implementation.
  553. */
  554. public class ImplementationSpecificArgument extends
  555. org.apache.tools.ant.util.facade.ImplementationSpecificArgument {
  556. public void setCompiler(String impl) {
  557. super.setImplementation(impl);
  558. }
  559. }
  560. }