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