1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.rmic;
  18. import java.io.File;
  19. import java.util.Random;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.taskdefs.Rmic;
  23. import org.apache.tools.ant.types.Commandline;
  24. import org.apache.tools.ant.types.Path;
  25. import org.apache.tools.ant.util.FileNameMapper;
  26. import org.apache.tools.ant.util.JavaEnvUtils;
  27. /**
  28. * This is the default implementation for the RmicAdapter interface.
  29. * Currently, this is a cut-and-paste of the original rmic task and
  30. * DefaultCopmpilerAdapter.
  31. *
  32. * @since Ant 1.4
  33. */
  34. public abstract class DefaultRmicAdapter implements RmicAdapter {
  35. private Rmic attributes;
  36. private FileNameMapper mapper;
  37. private static final Random rand = new Random();
  38. public DefaultRmicAdapter() {
  39. }
  40. public void setRmic(Rmic attributes) {
  41. this.attributes = attributes;
  42. mapper = new RmicFileNameMapper();
  43. }
  44. public Rmic getRmic() {
  45. return attributes;
  46. }
  47. protected String getStubClassSuffix() {
  48. return "_Stub";
  49. }
  50. protected String getSkelClassSuffix() {
  51. return "_Skel";
  52. }
  53. protected String getTieClassSuffix() {
  54. return "_Tie";
  55. }
  56. /**
  57. * This implementation returns a mapper that may return up to two
  58. * file names.
  59. *
  60. * <ul>
  61. * <li>for JRMP it will return *_getStubClassSuffix (and
  62. * *_getSkelClassSuffix if JDK 1.1 is used)</li>
  63. *
  64. * <li>for IDL it will return a random name, causing <rmic> to
  65. * always recompile.</li>
  66. *
  67. * <li>for IIOP it will return _*_getStubClassSuffix for
  68. * interfaces and _*_getStubClassSuffix for non-interfaces (and
  69. * determine the interface and create _*_Stub from that).</li>
  70. * </ul>
  71. */
  72. public FileNameMapper getMapper() {
  73. return mapper;
  74. }
  75. /**
  76. * The CLASSPATH this rmic process will use.
  77. */
  78. public Path getClasspath() {
  79. return getCompileClasspath();
  80. }
  81. /**
  82. * Builds the compilation classpath.
  83. */
  84. protected Path getCompileClasspath() {
  85. Path classpath = new Path(attributes.getProject());
  86. // add dest dir to classpath so that previously compiled and
  87. // untouched classes are on classpath
  88. classpath.setLocation(attributes.getBase());
  89. // Combine the build classpath with the system classpath, in an
  90. // order determined by the value of build.sysclasspath
  91. Path cp = attributes.getClasspath();
  92. if (cp == null) {
  93. cp = new Path(attributes.getProject());
  94. }
  95. if (attributes.getIncludeantruntime()) {
  96. classpath.addExisting(cp.concatSystemClasspath("last"));
  97. } else {
  98. classpath.addExisting(cp.concatSystemClasspath("ignore"));
  99. }
  100. if (attributes.getIncludejavaruntime()) {
  101. classpath.addJavaRuntime();
  102. }
  103. return classpath;
  104. }
  105. /**
  106. * setup rmic argument for rmic.
  107. */
  108. protected Commandline setupRmicCommand() {
  109. return setupRmicCommand(null);
  110. }
  111. /**
  112. * setup rmic argument for rmic.
  113. *
  114. * @param options additional parameters needed by a specific
  115. * implementation.
  116. */
  117. protected Commandline setupRmicCommand(String[] options) {
  118. Commandline cmd = new Commandline();
  119. if (options != null) {
  120. for (int i = 0; i < options.length; i++) {
  121. cmd.createArgument().setValue(options[i]);
  122. }
  123. }
  124. Path classpath = getCompileClasspath();
  125. cmd.createArgument().setValue("-d");
  126. cmd.createArgument().setFile(attributes.getBase());
  127. if (attributes.getExtdirs() != null) {
  128. if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
  129. /*
  130. * XXX - This doesn't mix very well with build.systemclasspath,
  131. */
  132. classpath.addExtdirs(attributes.getExtdirs());
  133. } else {
  134. cmd.createArgument().setValue("-extdirs");
  135. cmd.createArgument().setPath(attributes.getExtdirs());
  136. }
  137. }
  138. cmd.createArgument().setValue("-classpath");
  139. cmd.createArgument().setPath(classpath);
  140. String stubVersion = attributes.getStubVersion();
  141. if (null != stubVersion) {
  142. if ("1.1".equals(stubVersion)) {
  143. cmd.createArgument().setValue("-v1.1");
  144. } else if ("1.2".equals(stubVersion)) {
  145. cmd.createArgument().setValue("-v1.2");
  146. } else {
  147. cmd.createArgument().setValue("-vcompat");
  148. }
  149. }
  150. if (null != attributes.getSourceBase()) {
  151. cmd.createArgument().setValue("-keepgenerated");
  152. }
  153. if (attributes.getIiop()) {
  154. attributes.log("IIOP has been turned on.", Project.MSG_INFO);
  155. cmd.createArgument().setValue("-iiop");
  156. if (attributes.getIiopopts() != null) {
  157. attributes.log("IIOP Options: " + attributes.getIiopopts(),
  158. Project.MSG_INFO);
  159. cmd.createArgument().setValue(attributes.getIiopopts());
  160. }
  161. }
  162. if (attributes.getIdl()) {
  163. cmd.createArgument().setValue("-idl");
  164. attributes.log("IDL has been turned on.", Project.MSG_INFO);
  165. if (attributes.getIdlopts() != null) {
  166. cmd.createArgument().setValue(attributes.getIdlopts());
  167. attributes.log("IDL Options: " + attributes.getIdlopts(),
  168. Project.MSG_INFO);
  169. }
  170. }
  171. if (attributes.getDebug()) {
  172. cmd.createArgument().setValue("-g");
  173. }
  174. cmd.addArguments(attributes.getCurrentCompilerArgs());
  175. logAndAddFilesToCompile(cmd);
  176. return cmd;
  177. }
  178. /**
  179. * Logs the compilation parameters, adds the files to compile and logs the
  180. * &qout;niceSourceList"
  181. */
  182. protected void logAndAddFilesToCompile(Commandline cmd) {
  183. Vector compileList = attributes.getCompileList();
  184. attributes.log("Compilation " + cmd.describeArguments(),
  185. Project.MSG_VERBOSE);
  186. StringBuffer niceSourceList = new StringBuffer("File");
  187. if (compileList.size() != 1) {
  188. niceSourceList.append("s");
  189. }
  190. niceSourceList.append(" to be compiled:");
  191. for (int i = 0; i < compileList.size(); i++) {
  192. String arg = (String) compileList.elementAt(i);
  193. cmd.createArgument().setValue(arg);
  194. niceSourceList.append(" " + arg);
  195. }
  196. attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
  197. }
  198. /**
  199. * Mapper that may return up to two file names.
  200. *
  201. * <ul>
  202. * <li>for JRMP it will return *_getStubClassSuffix (and
  203. * *_getSkelClassSuffix if JDK 1.1 is used)</li>
  204. *
  205. * <li>for IDL it will return a random name, causing <rmic> to
  206. * always recompile.</li>
  207. *
  208. * <li>for IIOP it will return _*_getStubClassSuffix for
  209. * interfaces and _*_getStubClassSuffix for non-interfaces (and
  210. * determine the interface and create _*_Stub from that).</li>
  211. * </ul>
  212. */
  213. private class RmicFileNameMapper implements FileNameMapper {
  214. RmicFileNameMapper() {
  215. }
  216. /**
  217. * Empty implementation.
  218. */
  219. public void setFrom(String s) {
  220. }
  221. /**
  222. * Empty implementation.
  223. */
  224. public void setTo(String s) {
  225. }
  226. public String[] mapFileName(String name) {
  227. if (name == null
  228. || !name.endsWith(".class")
  229. || name.endsWith(getStubClassSuffix() + ".class")
  230. || name.endsWith(getSkelClassSuffix() + ".class")
  231. || name.endsWith(getTieClassSuffix() + ".class")) {
  232. // Not a .class file or the one we'd generate
  233. return null;
  234. }
  235. // we know that name.endsWith(".class")
  236. String base = name.substring(0, name.length() - 6);
  237. String classname = base.replace(File.separatorChar, '.');
  238. if (attributes.getVerify()
  239. && !attributes.isValidRmiRemote(classname)) {
  240. return null;
  241. }
  242. /*
  243. * fallback in case we have trouble loading the class or
  244. * don't know how to handle it (there is no easy way to
  245. * know what IDL mode would generate.
  246. *
  247. * This is supposed to make Ant always recompile the
  248. * class, as a file of that name should not exist.
  249. */
  250. String[] target = new String[] {name + ".tmp." + rand.nextLong()};
  251. if (!attributes.getIiop() && !attributes.getIdl()) {
  252. // JRMP with simple naming convention
  253. if ("1.2".equals(attributes.getStubVersion())) {
  254. target = new String[] {
  255. base + getStubClassSuffix() + ".class"
  256. };
  257. } else {
  258. target = new String[] {
  259. base + getStubClassSuffix() + ".class",
  260. base + getSkelClassSuffix() + ".class",
  261. };
  262. }
  263. } else if (!attributes.getIdl()) {
  264. int lastSlash = base.lastIndexOf(File.separatorChar);
  265. String dirname = "";
  266. /*
  267. * I know, this is not necessary, but I prefer it explicit (SB)
  268. */
  269. int index = -1;
  270. if (lastSlash == -1) {
  271. // no package
  272. index = 0;
  273. } else {
  274. index = lastSlash + 1;
  275. dirname = base.substring(0, index);
  276. }
  277. String filename = base.substring(index);
  278. try {
  279. Class c = attributes.getLoader().loadClass(classname);
  280. if (c.isInterface()) {
  281. // only stub, no tie
  282. target = new String[] {
  283. dirname + "_" + filename + getStubClassSuffix()
  284. + ".class"
  285. };
  286. } else {
  287. /*
  288. * stub is derived from implementation,
  289. * tie from interface name.
  290. */
  291. Class interf = attributes.getRemoteInterface(c);
  292. String iName = interf.getName();
  293. String iDir = "";
  294. int iIndex = -1;
  295. int lastDot = iName.lastIndexOf(".");
  296. if (lastDot == -1) {
  297. // no package
  298. iIndex = 0;
  299. } else {
  300. iIndex = lastDot + 1;
  301. iDir = iName.substring(0, iIndex);
  302. iDir = iDir.replace('.', File.separatorChar);
  303. }
  304. target = new String[] {
  305. dirname + "_" + filename + getTieClassSuffix()
  306. + ".class",
  307. iDir + "_" + iName.substring(iIndex)
  308. + getStubClassSuffix() + ".class"
  309. };
  310. }
  311. } catch (ClassNotFoundException e) {
  312. attributes.log("Unable to verify class " + classname
  313. + ". It could not be found.",
  314. Project.MSG_WARN);
  315. } catch (NoClassDefFoundError e) {
  316. attributes.log("Unable to verify class " + classname
  317. + ". It is not defined.", Project.MSG_WARN);
  318. } catch (Throwable t) {
  319. attributes.log("Unable to verify class " + classname
  320. + ". Loading caused Exception: "
  321. + t.getMessage(), Project.MSG_WARN);
  322. }
  323. }
  324. return target;
  325. }
  326. }
  327. }