1 17 18 package org.apache.jasper.compiler; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.PrintStream ; 23 import java.util.StringTokenizer ; 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 44 public class AntCompiler extends Compiler { 45 46 protected static Object javacLock = new Object (); 47 48 static { 49 System.setErr(new SystemLogHandler(System.err)); 50 } 51 52 54 protected Project project = null; 55 protected JasperAntLogger logger; 56 57 59 protected Project getProject() { 61 62 if (project != null) 63 return project; 64 65 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 reportBuf = new StringBuffer (); 88 89 protected void printMessage(final String message, 90 final PrintStream stream, 91 final int priority) { 92 } 93 94 protected void log(String message) { 95 reportBuf.append(message); 96 reportBuf.append(System.getProperty("line.separator")); 97 } 98 99 protected String getReport() { 100 String report = reportBuf.toString(); 101 reportBuf.setLength(0); 102 return report; 103 } 104 } 105 106 108 109 112 protected void generateClass(String [] smap) 113 throws FileNotFoundException , JasperException, Exception { 114 115 long t1 = 0; 116 if (log.isDebugEnabled()) { 117 t1 = System.currentTimeMillis(); 118 } 119 120 String javaEncoding = ctxt.getOptions().getJavaEncoding(); 121 String javaFileName = ctxt.getServletJavaFileName(); 122 String classpath = ctxt.getClassPath(); 123 124 String sep = System.getProperty("path.separator"); 125 126 StringBuffer errorReport = new StringBuffer (); 127 128 StringBuffer info=new StringBuffer (); 129 info.append("Compile: javaFileName=" + javaFileName + "\n" ); 130 info.append(" classpath=" + classpath + "\n" ); 131 132 SystemLogHandler.setThread(); 134 135 getProject(); 137 Javac javac = (Javac) project.createTask("javac"); 138 139 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 tokenizer = new StringTokenizer (classpath, sep); 144 while (tokenizer.hasMoreElements()) { 145 String pathElement = tokenizer.nextToken(); 146 File repository = new File (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 Path srcPath = new Path(project); 157 srcPath.setLocation(options.getScratchDir()); 158 159 info.append(" work dir=" + options.getScratchDir() + "\n"); 160 161 String 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 if(ctxt.getOptions().getFork()) { 173 String 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 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 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 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 String 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 javaFile = new File (javaFileName); 243 javaFile.delete(); 244 } 245 246 if (be != null) { 247 String 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 if (! options.isSmapSuppressed()) { 273 SmapUtil.installSmap(smap); 274 } 275 } 276 277 278 } 279 | Popular Tags |