1. /*
  2. * Copyright 2001-2002,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.IOException;
  19. import java.io.OutputStream;
  20. import java.lang.reflect.Constructor;
  21. import java.lang.reflect.Method;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.Project;
  24. import org.apache.tools.ant.taskdefs.LogOutputStream;
  25. import org.apache.tools.ant.types.Commandline;
  26. /**
  27. * The implementation of the rmic for SUN's JDK.
  28. *
  29. * @since Ant 1.4
  30. */
  31. public class SunRmic extends DefaultRmicAdapter {
  32. public boolean execute() throws BuildException {
  33. getRmic().log("Using SUN rmic compiler", Project.MSG_VERBOSE);
  34. Commandline cmd = setupRmicCommand();
  35. // Create an instance of the rmic, redirecting output to
  36. // the project log
  37. LogOutputStream logstr = new LogOutputStream(getRmic(),
  38. Project.MSG_WARN);
  39. try {
  40. Class c = Class.forName("sun.rmi.rmic.Main");
  41. Constructor cons
  42. = c.getConstructor(new Class[] {OutputStream.class, String.class});
  43. Object rmic = cons.newInstance(new Object[] {logstr, "rmic"});
  44. Method doRmic = c.getMethod("compile",
  45. new Class [] {String[].class});
  46. Boolean ok =
  47. (Boolean) doRmic.invoke(rmic,
  48. (new Object[] {cmd.getArguments()}));
  49. return ok.booleanValue();
  50. } catch (ClassNotFoundException ex) {
  51. throw new BuildException("Cannot use SUN rmic, as it is not "
  52. + "available. A common solution is to "
  53. + "set the environment variable "
  54. + "JAVA_HOME or CLASSPATH.",
  55. getRmic().getLocation());
  56. } catch (Exception ex) {
  57. if (ex instanceof BuildException) {
  58. throw (BuildException) ex;
  59. } else {
  60. throw new BuildException("Error starting SUN rmic: ",
  61. ex, getRmic().getLocation());
  62. }
  63. } finally {
  64. try {
  65. logstr.close();
  66. } catch (IOException e) {
  67. throw new BuildException(e);
  68. }
  69. }
  70. }
  71. }