1 17 18 package org.apache.jasper.compiler; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.OutputStreamWriter ; 24 import java.io.PrintWriter ; 25 import java.io.UnsupportedEncodingException ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.jasper.JasperException; 32 import org.apache.jasper.JspCompilationContext; 33 import org.apache.jasper.Options; 34 import org.apache.jasper.servlet.JspServletWrapper; 35 36 46 public abstract class Compiler { 47 48 protected org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 49 .getLog(Compiler .class); 50 51 53 protected JspCompilationContext ctxt; 54 55 protected ErrorDispatcher errDispatcher; 56 57 protected PageInfo pageInfo; 58 59 protected JspServletWrapper jsw; 60 61 protected TagFileProcessor tfp; 62 63 protected Options options; 64 65 protected Node.Nodes pageNodes; 66 67 69 public void init(JspCompilationContext ctxt, JspServletWrapper jsw) { 70 this.jsw = jsw; 71 this.ctxt = ctxt; 72 this.options = ctxt.getOptions(); 73 } 74 75 77 84 public Node.Nodes getPageNodes() { 85 return this.pageNodes; 86 } 87 88 94 protected String [] generateJava() throws Exception { 95 96 String [] smapStr = null; 97 98 long t1, t2, t3, t4; 99 100 t1 = t2 = t3 = t4 = 0; 101 102 if (log.isDebugEnabled()) { 103 t1 = System.currentTimeMillis(); 104 } 105 106 pageInfo = new PageInfo(new BeanRepository(ctxt.getClassLoader(), 108 errDispatcher), ctxt.getJspFile()); 109 110 JspConfig jspConfig = options.getJspConfig(); 111 JspConfig.JspProperty jspProperty = jspConfig.findJspProperty(ctxt 112 .getJspFile()); 113 114 119 if (jspProperty.isELIgnored() != null) { 120 pageInfo.setELIgnored(JspUtil.booleanValue(jspProperty 121 .isELIgnored())); 122 } 123 if (jspProperty.isScriptingInvalid() != null) { 124 pageInfo.setScriptingInvalid(JspUtil.booleanValue(jspProperty 125 .isScriptingInvalid())); 126 } 127 if (jspProperty.getIncludePrelude() != null) { 128 pageInfo.setIncludePrelude(jspProperty.getIncludePrelude()); 129 } 130 if (jspProperty.getIncludeCoda() != null) { 131 pageInfo.setIncludeCoda(jspProperty.getIncludeCoda()); 132 } 133 if (jspProperty.isDeferedSyntaxAllowedAsLiteral() != null) { 134 pageInfo.setDeferredSyntaxAllowedAsLiteral(JspUtil.booleanValue(jspProperty 135 .isDeferedSyntaxAllowedAsLiteral())); 136 } 137 if (jspProperty.isTrimDirectiveWhitespaces() != null) { 138 pageInfo.setTrimDirectiveWhitespaces(JspUtil.booleanValue(jspProperty 139 .isTrimDirectiveWhitespaces())); 140 } 141 142 ctxt.checkOutputDir(); 143 String javaFileName = ctxt.getServletJavaFileName(); 144 ServletWriter writer = null; 145 146 try { 147 String javaEncoding = ctxt.getOptions().getJavaEncoding(); 149 OutputStreamWriter osw = null; 150 151 try { 152 osw = new OutputStreamWriter ( 153 new FileOutputStream (javaFileName), javaEncoding); 154 } catch (UnsupportedEncodingException ex) { 155 errDispatcher.jspError("jsp.error.needAlternateJavaEncoding", 156 javaEncoding); 157 } 158 159 writer = new ServletWriter(new PrintWriter (osw)); 160 ctxt.setWriter(writer); 161 162 JspUtil.resetTemporaryVariableName(); 164 165 ParserController parserCtl = new ParserController(ctxt, this); 167 pageNodes = parserCtl.parse(ctxt.getJspFile()); 168 169 if (ctxt.isPrototypeMode()) { 170 Generator.generate(writer, this, pageNodes); 172 writer.close(); 173 writer = null; 174 return null; 175 } 176 177 Validator.validate(this, pageNodes); 179 180 if (log.isDebugEnabled()) { 181 t2 = System.currentTimeMillis(); 182 } 183 184 Collector.collect(this, pageNodes); 186 187 tfp = new TagFileProcessor(); 190 tfp.loadTagFiles(this, pageNodes); 191 192 if (log.isDebugEnabled()) { 193 t3 = System.currentTimeMillis(); 194 } 195 196 ScriptingVariabler.set(pageNodes, errDispatcher); 198 199 TagPluginManager tagPluginManager = options.getTagPluginManager(); 201 tagPluginManager.apply(pageNodes, errDispatcher, pageInfo); 202 203 TextOptimizer.concatenate(this, pageNodes); 205 206 ELFunctionMapper.map(this, pageNodes); 208 209 Generator.generate(writer, this, pageNodes); 211 writer.close(); 212 writer = null; 213 214 ctxt.setWriter(null); 218 219 if (log.isDebugEnabled()) { 220 t4 = System.currentTimeMillis(); 221 log.debug("Generated " + javaFileName + " total=" + (t4 - t1) 222 + " generate=" + (t4 - t3) + " validate=" + (t2 - t1)); 223 } 224 225 } catch (Exception e) { 226 if (writer != null) { 227 try { 228 writer.close(); 229 writer = null; 230 } catch (Exception e1) { 231 } 233 } 234 new File (javaFileName).delete(); 236 throw e; 237 } finally { 238 if (writer != null) { 239 try { 240 writer.close(); 241 } catch (Exception e2) { 242 } 244 } 245 } 246 247 if (!options.isSmapSuppressed()) { 249 smapStr = SmapUtil.generateSmap(ctxt, pageNodes); 250 } 251 252 tfp.removeProtoTypeFiles(ctxt.getClassFileName()); 258 259 return smapStr; 260 } 261 262 265 protected abstract void generateClass(String [] smap) 266 throws FileNotFoundException , JasperException, Exception ; 267 268 271 public void compile() throws FileNotFoundException , JasperException, 272 Exception { 273 compile(true); 274 } 275 276 284 public void compile(boolean compileClass) throws FileNotFoundException , 285 JasperException, Exception { 286 compile(compileClass, false); 287 } 288 289 299 public void compile(boolean compileClass, boolean jspcMode) 300 throws FileNotFoundException , JasperException, Exception { 301 if (errDispatcher == null) { 302 this.errDispatcher = new ErrorDispatcher(jspcMode); 303 } 304 305 try { 306 String [] smap = generateJava(); 307 if (compileClass) { 308 generateClass(smap); 309 } 310 } finally { 311 if (tfp != null) { 312 tfp.removeProtoTypeFiles(null); 313 } 314 tfp = null; 319 errDispatcher = null; 320 pageInfo = null; 321 322 if (!this.options.getDevelopment()) { 327 pageNodes = null; 328 } 329 330 if (ctxt.getWriter() != null) { 331 ctxt.getWriter().close(); 332 ctxt.setWriter(null); 333 } 334 } 335 } 336 337 341 public boolean isOutDated() { 342 return isOutDated(true); 343 } 344 345 355 public boolean isOutDated(boolean checkClass) { 356 357 String jsp = ctxt.getJspFile(); 358 359 if (jsw != null 360 && (ctxt.getOptions().getModificationTestInterval() > 0)) { 361 362 if (jsw.getLastModificationTest() 363 + (ctxt.getOptions().getModificationTestInterval() * 1000) > System 364 .currentTimeMillis()) { 365 return false; 366 } else { 367 jsw.setLastModificationTest(System.currentTimeMillis()); 368 } 369 } 370 371 long jspRealLastModified = 0; 372 try { 373 URL jspUrl = ctxt.getResource(jsp); 374 if (jspUrl == null) { 375 ctxt.incrementRemoved(); 376 return false; 377 } 378 URLConnection uc = jspUrl.openConnection(); 379 jspRealLastModified = uc.getLastModified(); 380 uc.getInputStream().close(); 381 } catch (Exception e) { 382 return true; 383 } 384 385 long targetLastModified = 0; 386 File targetFile; 387 388 if (checkClass) { 389 targetFile = new File (ctxt.getClassFileName()); 390 } else { 391 targetFile = new File (ctxt.getServletJavaFileName()); 392 } 393 394 if (!targetFile.exists()) { 395 return true; 396 } 397 398 targetLastModified = targetFile.lastModified(); 399 if (checkClass && jsw != null) { 400 jsw.setServletClassLastModifiedTime(targetLastModified); 401 } 402 if (targetLastModified < jspRealLastModified) { 403 if (log.isDebugEnabled()) { 404 log.debug("Compiler: outdated: " + targetFile + " " 405 + targetLastModified); 406 } 407 return true; 408 } 409 410 if (jsw == null) { 413 return false; 414 } 415 416 List depends = jsw.getDependants(); 417 if (depends == null) { 418 return false; 419 } 420 421 Iterator it = depends.iterator(); 422 while (it.hasNext()) { 423 String include = (String ) it.next(); 424 try { 425 URL includeUrl = ctxt.getResource(include); 426 if (includeUrl == null) { 427 return true; 428 } 429 430 URLConnection includeUconn = includeUrl.openConnection(); 431 long includeLastModified = includeUconn.getLastModified(); 432 includeUconn.getInputStream().close(); 433 434 if (includeLastModified > targetLastModified) { 435 return true; 436 } 437 } catch (Exception e) { 438 return true; 439 } 440 } 441 442 return false; 443 444 } 445 446 449 public ErrorDispatcher getErrorDispatcher() { 450 return errDispatcher; 451 } 452 453 456 public PageInfo getPageInfo() { 457 return pageInfo; 458 } 459 460 public JspCompilationContext getCompilationContext() { 461 return ctxt; 462 } 463 464 467 public void removeGeneratedFiles() { 468 try { 469 String classFileName = ctxt.getClassFileName(); 470 if (classFileName != null) { 471 File classFile = new File (classFileName); 472 if (log.isDebugEnabled()) 473 log.debug("Deleting " + classFile); 474 classFile.delete(); 475 } 476 } catch (Exception e) { 477 } 479 try { 480 String javaFileName = ctxt.getServletJavaFileName(); 481 if (javaFileName != null) { 482 File javaFile = new File (javaFileName); 483 if (log.isDebugEnabled()) 484 log.debug("Deleting " + javaFile); 485 javaFile.delete(); 486 } 487 } catch (Exception e) { 488 } 490 } 491 492 public void removeGeneratedClassFiles() { 493 try { 494 String classFileName = ctxt.getClassFileName(); 495 if (classFileName != null) { 496 File classFile = new File (classFileName); 497 if (log.isDebugEnabled()) 498 log.debug("Deleting " + classFile); 499 classFile.delete(); 500 } 501 } catch (Exception e) { 502 } 504 } 505 } 506 | Popular Tags |