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 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 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", "Tomcat", 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.*;
  56. import java.io.*;
  57. import java.util.StringTokenizer;
  58. /**
  59. * Task to compile RMI stubs and skeletons. This task can take the following
  60. * arguments:
  61. * <ul>
  62. * <li>base: The base directory for the compiled stubs and skeletons
  63. * <li>class: The name of the class to generate the stubs from
  64. * <li>stubVersion: The version of the stub prototol to use (1.1, 1.2, compat)
  65. * <li>sourceBase: The base directory for the generated stubs and skeletons
  66. * <li>classpath: Additional classpath, appended before the system classpath
  67. * </ul>
  68. * Of these arguments, the <b>base</b> and <b>class</b> are required.
  69. * <p>
  70. *
  71. * @author duncan@x180.com
  72. * @author ludovic.claude@websitewatchers.co.uk
  73. */
  74. public class Rmic extends Task {
  75. private String base;
  76. private String classname;
  77. private String sourceBase;
  78. private String stubVersion;
  79. private String compileClasspath;
  80. private boolean filtering = false;
  81. public void setBase(String base) {
  82. this.base = base;
  83. }
  84. public void setClass(String classname) {
  85. project.log("The class attribute is deprecated. " +
  86. "Please use the classname attribute.",
  87. Project.MSG_WARN);
  88. this.classname = classname;
  89. }
  90. public void setClassname(String classname) {
  91. this.classname = classname;
  92. }
  93. public void setSourceBase(String sourceBase) {
  94. this.sourceBase = sourceBase;
  95. }
  96. public void setStubVersion(String stubVersion) {
  97. this.stubVersion = stubVersion;
  98. }
  99. public void setFiltering(String filter) {
  100. filtering = Project.toBoolean(filter);
  101. }
  102. /**
  103. * Set the classpath to be used for this compilation.
  104. */
  105. public void setClasspath(String classpath) {
  106. compileClasspath = project.translatePath(classpath);
  107. }
  108. public void execute() throws BuildException {
  109. File baseFile = project.resolveFile(base);
  110. File sourceBaseFile = null;
  111. if (null != sourceBase)
  112. sourceBaseFile = project.resolveFile(sourceBase);
  113. String classpath = getCompileClasspath(baseFile);
  114. // XXX
  115. // need to provide an input stream that we read in from!
  116. sun.rmi.rmic.Main compiler = new sun.rmi.rmic.Main(System.out, "rmic");
  117. int argCount = 5;
  118. int i = 0;
  119. if (null != stubVersion) argCount++;
  120. if (null != sourceBase) argCount++;
  121. String[] args = new String[argCount];
  122. args[i++] = "-d";
  123. args[i++] = baseFile.getAbsolutePath();
  124. args[i++] = "-classpath";
  125. args[i++] = classpath;
  126. args[i++] = classname;
  127. if (null != stubVersion) {
  128. if ("1.1".equals(stubVersion))
  129. args[i++] = "-v1.1";
  130. else if ("1.2".equals(stubVersion))
  131. args[i++] = "-v1.2";
  132. else
  133. args[i++] = "-vcompat";
  134. }
  135. if (null != sourceBase) args[i++] = "-keepgenerated";
  136. compiler.compile(args);
  137. // Move the generated source file to the base directory
  138. if (null != sourceBase) {
  139. String stubFileName = classname.replace('.', '/') + "_Stub.java";
  140. File oldStubFile = new File(baseFile, stubFileName);
  141. File newStubFile = new File(sourceBaseFile, stubFileName);
  142. try {
  143. project.copyFile(oldStubFile, newStubFile, filtering);
  144. oldStubFile.delete();
  145. } catch (IOException ioe) {
  146. String msg = "Failed to copy " + oldStubFile + " to " +
  147. newStubFile + " due to " + ioe.getMessage();
  148. throw new BuildException(msg);
  149. }
  150. if (!"1.2".equals(stubVersion)) {
  151. String skelFileName = classname.replace('.', '/') + "_Skel.java";
  152. File oldSkelFile = new File(baseFile, skelFileName);
  153. File newSkelFile = new File(sourceBaseFile, skelFileName);
  154. try {
  155. project.copyFile(oldSkelFile, newSkelFile, filtering);
  156. oldSkelFile.delete();
  157. } catch (IOException ioe) {
  158. String msg = "Failed to copy " + oldSkelFile + " to " +
  159. newSkelFile + " due to " + ioe.getMessage();
  160. throw new BuildException(msg);
  161. }
  162. }
  163. }
  164. }
  165. /**
  166. * Builds the compilation classpath.
  167. */
  168. // XXX
  169. // we need a way to not use the current classpath.
  170. private String getCompileClasspath(File baseFile) {
  171. StringBuffer classpath = new StringBuffer();
  172. // add dest dir to classpath so that previously compiled and
  173. // untouched classes are on classpath
  174. classpath.append(baseFile.getAbsolutePath());
  175. // add our classpath to the mix
  176. if (compileClasspath != null) {
  177. addExistingToClasspath(classpath,compileClasspath);
  178. }
  179. // add the system classpath
  180. addExistingToClasspath(classpath,System.getProperty("java.class.path"));
  181. // in jdk 1.2, the system classes are not on the visible classpath.
  182. if (Project.getJavaVersion().startsWith("1.2")) {
  183. String bootcp = System.getProperty("sun.boot.class.path");
  184. if (bootcp != null) {
  185. addExistingToClasspath(classpath, bootcp);
  186. }
  187. }
  188. return classpath.toString();
  189. }
  190. /**
  191. * Takes a classpath-like string, and adds each element of
  192. * this string to a new classpath, if the components exist.
  193. * Components that don't exist, aren't added.
  194. * We do this, because jikes issues warnings for non-existant
  195. * files/dirs in his classpath, and these warnings are pretty
  196. * annoying.
  197. * @param target - target classpath
  198. * @param source - source classpath
  199. * to get file objects.
  200. */
  201. private void addExistingToClasspath(StringBuffer target,String source) {
  202. StringTokenizer tok = new StringTokenizer(source,
  203. System.getProperty("path.separator"), false);
  204. while (tok.hasMoreTokens()) {
  205. File f = project.resolveFile(tok.nextToken());
  206. if (f.exists()) {
  207. target.append(File.pathSeparator);
  208. target.append(f.getAbsolutePath());
  209. } else {
  210. project.log("Dropping from classpath: "+
  211. f.getAbsolutePath(),project.MSG_VERBOSE);
  212. }
  213. }
  214. }
  215. }