KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > AntCompiler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.compiler;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.apache.jasper.JasperException;
26 import org.apache.jasper.util.SystemLogHandler;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.DefaultLogger;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.taskdefs.Javac;
31 import org.apache.tools.ant.types.Path;
32 import org.apache.tools.ant.types.PatternSet;
33
34 /**
35  * Main JSP compiler class. This class uses Ant for compiling.
36  *
37  * @author Anil K. Vijendran
38  * @author Mandar Raje
39  * @author Pierre Delisle
40  * @author Kin-man Chung
41  * @author Remy Maucherat
42  * @author Mark Roth
43  */

44 public class AntCompiler extends Compiler JavaDoc {
45
46     protected static Object JavaDoc javacLock = new Object JavaDoc();
47
48     static {
49         System.setErr(new SystemLogHandler(System.err));
50     }
51
52     // ----------------------------------------------------- Instance Variables
53

54     protected Project project = null;
55     protected JasperAntLogger logger;
56
57     // ------------------------------------------------------------ Constructor
58

59     // Lazy eval - if we don't need to compile we probably don't need the project
60
protected Project getProject() {
61         
62         if (project != null)
63             return project;
64         
65         // Initializing project
66
project = new Project();
67         logger = new JasperAntLogger();
68         logger.setOutputPrintStream(System.out);
69         logger.setErrorPrintStream(System.err);
70         logger.setMessageOutputLevel(Project.MSG_INFO);
71         project.addBuildListener( logger);
72         if (System.getProperty("catalina.home") != null) {
73             project.setBasedir( System.getProperty("catalina.home"));
74         }
75         
76         if( options.getCompiler() != null ) {
77             if( log.isDebugEnabled() )
78                 log.debug("Compiler " + options.getCompiler() );
79             project.setProperty("build.compiler", options.getCompiler() );
80         }
81         project.init();
82         return project;
83     }
84     
85     public class JasperAntLogger extends DefaultLogger {
86         
87         protected StringBuffer JavaDoc reportBuf = new StringBuffer JavaDoc();
88         
89         protected void printMessage(final String JavaDoc message,
90                 final PrintStream JavaDoc stream,
91                 final int priority) {
92         }
93         
94         protected void log(String JavaDoc message) {
95             reportBuf.append(message);
96             reportBuf.append(System.getProperty("line.separator"));
97         }
98         
99         protected String JavaDoc getReport() {
100             String JavaDoc report = reportBuf.toString();
101             reportBuf.setLength(0);
102             return report;
103         }
104     }
105     
106     // --------------------------------------------------------- Public Methods
107

108
109     /**
110      * Compile the servlet from .java file to .class file
111      */

112     protected void generateClass(String JavaDoc[] smap)
113         throws FileNotFoundException JavaDoc, JasperException, Exception JavaDoc {
114         
115         long t1 = 0;
116         if (log.isDebugEnabled()) {
117             t1 = System.currentTimeMillis();
118         }
119
120         String JavaDoc javaEncoding = ctxt.getOptions().getJavaEncoding();
121         String JavaDoc javaFileName = ctxt.getServletJavaFileName();
122         String JavaDoc classpath = ctxt.getClassPath();
123         
124         String JavaDoc sep = System.getProperty("path.separator");
125         
126         StringBuffer JavaDoc errorReport = new StringBuffer JavaDoc();
127         
128         StringBuffer JavaDoc info=new StringBuffer JavaDoc();
129         info.append("Compile: javaFileName=" + javaFileName + "\n" );
130         info.append(" classpath=" + classpath + "\n" );
131         
132         // Start capturing the System.err output for this thread
133
SystemLogHandler.setThread();
134         
135         // Initializing javac task
136
getProject();
137         Javac javac = (Javac) project.createTask("javac");
138         
139         // Initializing classpath
140
Path path = new Path(project);
141         path.setPath(System.getProperty("java.class.path"));
142         info.append(" cp=" + System.getProperty("java.class.path") + "\n");
143         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classpath, sep);
144         while (tokenizer.hasMoreElements()) {
145             String JavaDoc pathElement = tokenizer.nextToken();
146             File JavaDoc repository = new File JavaDoc(pathElement);
147             path.setLocation(repository);
148             info.append(" cp=" + repository + "\n");
149         }
150         
151         if( log.isDebugEnabled() )
152             log.debug( "Using classpath: " + System.getProperty("java.class.path") + sep
153                     + classpath);
154         
155         // Initializing sourcepath
156
Path srcPath = new Path(project);
157         srcPath.setLocation(options.getScratchDir());
158         
159         info.append(" work dir=" + options.getScratchDir() + "\n");
160         
161         // Initialize and set java extensions
162
String JavaDoc exts = System.getProperty("java.ext.dirs");
163         if (exts != null) {
164             Path extdirs = new Path(project);
165             extdirs.setPath(exts);
166             javac.setExtdirs(extdirs);
167             info.append(" extension dir=" + exts + "\n");
168         }
169
170         // Add endorsed directories if any are specified and we're forking
171
// See Bugzilla 31257
172
if(ctxt.getOptions().getFork()) {
173             String JavaDoc endorsed = System.getProperty("java.endorsed.dirs");
174             if(endorsed != null) {
175                 Javac.ImplementationSpecificArgument endorsedArg =
176                     javac.createCompilerArg();
177                 endorsedArg.setLine("-J-Djava.endorsed.dirs="+endorsed);
178                 info.append(" endorsed dir=" + endorsed + "\n");
179             } else {
180                 info.append(" no endorsed dirs specified\n");
181             }
182         }
183         
184         // Configure the compiler object
185
javac.setEncoding(javaEncoding);
186         javac.setClasspath(path);
187         javac.setDebug(ctxt.getOptions().getClassDebugInfo());
188         javac.setSrcdir(srcPath);
189         javac.setTempdir(options.getScratchDir());
190         javac.setOptimize(! ctxt.getOptions().getClassDebugInfo() );
191         javac.setFork(ctxt.getOptions().getFork());
192         info.append(" srcDir=" + srcPath + "\n" );
193         
194         // Set the Java compiler to use
195
if (options.getCompiler() != null) {
196             javac.setCompiler(options.getCompiler());
197             info.append(" compiler=" + options.getCompiler() + "\n");
198         }
199
200         if (options.getCompilerTargetVM() != null) {
201             javac.setTarget(options.getCompilerTargetVM());
202             info.append(" compilerTargetVM=" + options.getCompilerTargetVM() + "\n");
203         }
204
205         if (options.getCompilerSourceVM() != null) {
206             javac.setSource(options.getCompilerSourceVM());
207             info.append(" compilerSourceVM=" + options.getCompilerSourceVM() + "\n");
208         }
209         
210         // Build includes path
211
PatternSet.NameEntry includes = javac.createInclude();
212         
213         includes.setName(ctxt.getJavaPath());
214         info.append(" include="+ ctxt.getJavaPath() + "\n" );
215         
216         BuildException be = null;
217         
218         try {
219             if (ctxt.getOptions().getFork()) {
220                 javac.execute();
221             } else {
222                 synchronized(javacLock) {
223                     javac.execute();
224                 }
225             }
226         } catch (BuildException e) {
227             be = e;
228             log.error(Localizer.getMessage("jsp.error.javac"), e);
229             log.error(Localizer.getMessage("jsp.error.javac.env") + info.toString());
230         }
231         
232         errorReport.append(logger.getReport());
233
234         // Stop capturing the System.err output for this thread
235
String JavaDoc errorCapture = SystemLogHandler.unsetThread();
236         if (errorCapture != null) {
237             errorReport.append(System.getProperty("line.separator"));
238             errorReport.append(errorCapture);
239         }
240
241         if (!ctxt.keepGenerated()) {
242             File JavaDoc javaFile = new File JavaDoc(javaFileName);
243             javaFile.delete();
244         }
245         
246         if (be != null) {
247             String JavaDoc errorReportString = errorReport.toString();
248             log.error(Localizer.getMessage("jsp.error.compilation", javaFileName, errorReportString));
249             JavacErrorDetail[] javacErrors = ErrorDispatcher.parseJavacErrors(
250                     errorReportString, javaFileName, pageNodes);
251             if (javacErrors != null) {
252                 errDispatcher.javacError(javacErrors);
253             } else {
254                 errDispatcher.javacError(errorReportString, be);
255             }
256         }
257         
258         if( log.isDebugEnabled() ) {
259             long t2 = System.currentTimeMillis();
260             log.debug("Compiled " + ctxt.getServletJavaFileName() + " "
261                       + (t2-t1) + "ms");
262         }
263         
264         logger = null;
265         project = null;
266         
267         if (ctxt.isPrototypeMode()) {
268             return;
269         }
270         
271         // JSR45 Support
272
if (! options.isSmapSuppressed()) {
273             SmapUtil.installSmap(smap);
274         }
275     }
276
277     
278 }
279
Popular Tags