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.optional.jsp.compilers;
  18. import java.io.File;
  19. import org.apache.tools.ant.AntClassLoader;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Project;
  22. import org.apache.tools.ant.taskdefs.Java;
  23. import org.apache.tools.ant.taskdefs.optional.jsp.JspC;
  24. import org.apache.tools.ant.taskdefs.optional.jsp.JspMangler;
  25. import org.apache.tools.ant.types.CommandlineJava;
  26. import org.apache.tools.ant.types.Path;
  27. /**
  28. * The implementation of the jasper compiler.
  29. * This is a cut-and-paste of the original Jspc task.
  30. *
  31. * @since ant1.5
  32. */
  33. public class JasperC extends DefaultJspCompilerAdapter {
  34. /**
  35. * what produces java classes from .jsp files
  36. */
  37. JspMangler mangler;
  38. public JasperC(JspMangler mangler) {
  39. this.mangler = mangler;
  40. }
  41. /**
  42. * our execute method
  43. */
  44. public boolean execute()
  45. throws BuildException {
  46. getJspc().log("Using jasper compiler", Project.MSG_VERBOSE);
  47. CommandlineJava cmd = setupJasperCommand();
  48. try {
  49. // Create an instance of the compiler, redirecting output to
  50. // the project log
  51. Java java = (Java) (getProject().createTask("java"));
  52. Path p = getClasspath();
  53. if (getJspc().getClasspath() != null) {
  54. getProject().log("using user supplied classpath: " + p,
  55. Project.MSG_DEBUG);
  56. } else {
  57. getProject().log("using system classpath: " + p,
  58. Project.MSG_DEBUG);
  59. }
  60. java.setClasspath(p);
  61. java.setDir(getProject().getBaseDir());
  62. java.setClassname("org.apache.jasper.JspC");
  63. //this is really irritating; we need a way to set stuff
  64. String args[] = cmd.getJavaCommand().getArguments();
  65. for (int i = 0; i < args.length; i++) {
  66. java.createArg().setValue(args[i]);
  67. }
  68. java.setFailonerror(getJspc().getFailonerror());
  69. //we are forking here to be sure that if JspC calls
  70. //System.exit() it doesn't halt the build
  71. java.setFork(true);
  72. java.setTaskName("jasperc");
  73. java.execute();
  74. return true;
  75. } catch (Exception ex) {
  76. if (ex instanceof BuildException) {
  77. throw (BuildException) ex;
  78. } else {
  79. throw new BuildException("Error running jsp compiler: ",
  80. ex, getJspc().getLocation());
  81. }
  82. } finally {
  83. getJspc().deleteEmptyJavaFiles();
  84. }
  85. }
  86. /**
  87. * build up a command line
  88. * @return a command line for jasper
  89. */
  90. private CommandlineJava setupJasperCommand() {
  91. CommandlineJava cmd = new CommandlineJava();
  92. JspC jspc = getJspc();
  93. addArg(cmd, "-d", jspc.getDestdir());
  94. addArg(cmd, "-p", jspc.getPackage());
  95. if (!isTomcat5x()) {
  96. addArg(cmd, "-v" + jspc.getVerbose());
  97. } else {
  98. getProject().log("this task doesn't support Tomcat 5.x properly, "
  99. + "please use the Tomcat provided jspc task "
  100. + "instead");
  101. }
  102. addArg(cmd, "-uriroot", jspc.getUriroot());
  103. addArg(cmd, "-uribase", jspc.getUribase());
  104. addArg(cmd, "-ieplugin", jspc.getIeplugin());
  105. addArg(cmd, "-webinc", jspc.getWebinc());
  106. addArg(cmd, "-webxml", jspc.getWebxml());
  107. addArg(cmd, "-die9");
  108. if (jspc.isMapped()) {
  109. addArg(cmd, "-mapped");
  110. }
  111. if (jspc.getWebApp() != null) {
  112. File dir = jspc.getWebApp().getDirectory();
  113. addArg(cmd, "-webapp", dir);
  114. }
  115. logAndAddFilesToCompile(getJspc(), getJspc().getCompileList(), cmd);
  116. return cmd;
  117. }
  118. /**
  119. * @return an instance of the mangler this compiler uses
  120. */
  121. public JspMangler createMangler() {
  122. return mangler;
  123. }
  124. /**
  125. * @since Ant 1.6.2
  126. */
  127. private Path getClasspath() {
  128. Path p = getJspc().getClasspath();
  129. if (p == null) {
  130. p = new Path(getProject());
  131. return p.concatSystemClasspath("only");
  132. } else {
  133. return p.concatSystemClasspath("ignore");
  134. }
  135. }
  136. /**
  137. * @since Ant 1.6.2
  138. */
  139. private boolean isTomcat5x() {
  140. AntClassLoader l = null;
  141. try {
  142. l = getProject().createClassLoader(getClasspath());
  143. l.loadClass("org.apache.jasper.tagplugins.jstl.If");
  144. return true;
  145. } catch (ClassNotFoundException e) {
  146. return false;
  147. } finally {
  148. if (l != null) {
  149. l.cleanup();
  150. }
  151. }
  152. }
  153. }