001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.jsp.compilers;
020:
021: import java.io.File;
022: import org.apache.tools.ant.AntClassLoader;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.taskdefs.Java;
026: import org.apache.tools.ant.taskdefs.optional.jsp.JspC;
027: import org.apache.tools.ant.taskdefs.optional.jsp.JspMangler;
028: import org.apache.tools.ant.types.CommandlineJava;
029: import org.apache.tools.ant.types.Path;
030:
031: /**
032: * The implementation of the jasper compiler.
033: * This is a cut-and-paste of the original Jspc task.
034: *
035: * @since ant1.5
036: */
037: public class JasperC extends DefaultJspCompilerAdapter {
038:
039: // CheckStyle:VisibilityModifier OFF - bc
040:
041: /**
042: * what produces java classes from .jsp files
043: */
044: JspMangler mangler;
045:
046: // CheckStyle:VisibilityModifier ON
047:
048: /**
049: * Constructor for JasperC.
050: * @param mangler a filename converter
051: */
052: public JasperC(JspMangler mangler) {
053: this .mangler = mangler;
054: }
055:
056: /**
057: * Our execute method.
058: * @return true if successful
059: * @throws BuildException on error
060: */
061: public boolean execute() throws BuildException {
062: getJspc().log("Using jasper compiler", Project.MSG_VERBOSE);
063: CommandlineJava cmd = setupJasperCommand();
064:
065: try {
066: // Create an instance of the compiler, redirecting output to
067: // the project log
068: Java java = new Java(owner);
069: Path p = getClasspath();
070: if (getJspc().getClasspath() != null) {
071: getProject().log("using user supplied classpath: " + p,
072: Project.MSG_DEBUG);
073: } else {
074: getProject().log("using system classpath: " + p,
075: Project.MSG_DEBUG);
076: }
077: java.setClasspath(p);
078: java.setDir(getProject().getBaseDir());
079: java.setClassname("org.apache.jasper.JspC");
080: //this is really irritating; we need a way to set stuff
081: String[] args = cmd.getJavaCommand().getArguments();
082: for (int i = 0; i < args.length; i++) {
083: java.createArg().setValue(args[i]);
084: }
085: java.setFailonerror(getJspc().getFailonerror());
086: //we are forking here to be sure that if JspC calls
087: //System.exit() it doesn't halt the build
088: java.setFork(true);
089: java.setTaskName("jasperc");
090: java.execute();
091: return true;
092: } catch (Exception ex) {
093: if (ex instanceof BuildException) {
094: throw (BuildException) ex;
095: } else {
096: throw new BuildException(
097: "Error running jsp compiler: ", ex, getJspc()
098: .getLocation());
099: }
100: } finally {
101: getJspc().deleteEmptyJavaFiles();
102: }
103: }
104:
105: /**
106: * build up a command line
107: * @return a command line for jasper
108: */
109: private CommandlineJava setupJasperCommand() {
110: CommandlineJava cmd = new CommandlineJava();
111: JspC jspc = getJspc();
112: addArg(cmd, "-d", jspc.getDestdir());
113: addArg(cmd, "-p", jspc.getPackage());
114:
115: if (!isTomcat5x()) {
116: addArg(cmd, "-v" + jspc.getVerbose());
117: } else {
118: getProject()
119: .log(
120: "this task doesn't support Tomcat 5.x properly, "
121: + "please use the Tomcat provided jspc task "
122: + "instead");
123: }
124:
125: addArg(cmd, "-uriroot", jspc.getUriroot());
126: addArg(cmd, "-uribase", jspc.getUribase());
127: addArg(cmd, "-ieplugin", jspc.getIeplugin());
128: addArg(cmd, "-webinc", jspc.getWebinc());
129: addArg(cmd, "-webxml", jspc.getWebxml());
130: addArg(cmd, "-die9");
131:
132: if (jspc.isMapped()) {
133: addArg(cmd, "-mapped");
134: }
135: if (jspc.getWebApp() != null) {
136: File dir = jspc.getWebApp().getDirectory();
137: addArg(cmd, "-webapp", dir);
138: }
139: logAndAddFilesToCompile(getJspc(), getJspc().getCompileList(),
140: cmd);
141: return cmd;
142: }
143:
144: /**
145: * @return an instance of the mangler this compiler uses
146: */
147:
148: public JspMangler createMangler() {
149: return mangler;
150: }
151:
152: /**
153: * @since Ant 1.6.2
154: */
155: private Path getClasspath() {
156: Path p = getJspc().getClasspath();
157: if (p == null) {
158: p = new Path(getProject());
159: return p.concatSystemClasspath("only");
160: } else {
161: return p.concatSystemClasspath("ignore");
162: }
163: }
164:
165: /**
166: * @since Ant 1.6.2
167: */
168: private boolean isTomcat5x() {
169: AntClassLoader l = null;
170: try {
171: l = getProject().createClassLoader(getClasspath());
172: l.loadClass("org.apache.jasper.tagplugins.jstl.If");
173: return true;
174: } catch (ClassNotFoundException e) {
175: return false;
176: } finally {
177: if (l != null) {
178: l.cleanup();
179: }
180: }
181: }
182: }
|