1 17 18 package org.apache.jasper; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Set ; 28 29 import javax.servlet.ServletContext ; 30 import javax.servlet.jsp.tagext.TagInfo ; 31 32 import org.apache.jasper.compiler.Compiler; 33 import org.apache.jasper.compiler.JspRuntimeContext; 34 import org.apache.jasper.compiler.JspUtil; 35 import org.apache.jasper.compiler.Localizer; 36 import org.apache.jasper.compiler.ServletWriter; 37 import org.apache.jasper.servlet.JasperLoader; 38 import org.apache.jasper.servlet.JspServletWrapper; 39 40 54 public class JspCompilationContext { 55 56 protected org.apache.commons.logging.Log log = 57 org.apache.commons.logging.LogFactory.getLog(JspCompilationContext.class); 58 59 protected Map <String , URL > tagFileJarUrls; 60 protected boolean isPackagedTagFile; 61 62 protected String className; 63 protected String jspUri; 64 protected boolean isErrPage; 65 protected String basePackageName; 66 protected String derivedPackageName; 67 protected String servletJavaFileName; 68 protected String javaPath; 69 protected String classFileName; 70 protected String contentType; 71 protected ServletWriter writer; 72 protected Options options; 73 protected JspServletWrapper jsw; 74 protected Compiler jspCompiler; 75 protected String classPath; 76 77 protected String baseURI; 78 protected String outputDir; 79 protected ServletContext context; 80 protected URLClassLoader loader; 81 82 protected JspRuntimeContext rctxt; 83 84 protected int removed = 0; 85 86 protected URLClassLoader jspLoader; 87 protected URL baseUrl; 88 protected Class servletClass; 89 90 protected boolean isTagFile; 91 protected boolean protoTypeMode; 92 protected TagInfo tagInfo; 93 protected URL tagFileJarUrl; 94 95 public JspCompilationContext(String jspUri, 97 boolean isErrPage, 98 Options options, 99 ServletContext context, 100 JspServletWrapper jsw, 101 JspRuntimeContext rctxt) { 102 103 this.jspUri = canonicalURI(jspUri); 104 this.isErrPage = isErrPage; 105 this.options = options; 106 this.jsw = jsw; 107 this.context = context; 108 109 this.baseURI = jspUri.substring(0, jspUri.lastIndexOf('/') + 1); 110 if (baseURI == null) { 112 baseURI = "/"; 113 } else if (baseURI.charAt(0) != '/') { 114 baseURI = "/" + baseURI; 117 } 118 if (baseURI.charAt(baseURI.length() - 1) != '/') { 119 baseURI += '/'; 120 } 121 122 this.rctxt = rctxt; 123 this.tagFileJarUrls = new HashMap <String , URL >(); 124 this.basePackageName = Constants.JSP_PACKAGE_NAME; 125 } 126 127 public JspCompilationContext(String tagfile, 128 TagInfo tagInfo, 129 Options options, 130 ServletContext context, 131 JspServletWrapper jsw, 132 JspRuntimeContext rctxt, 133 URL tagFileJarUrl) { 134 this(tagfile, false, options, context, jsw, rctxt); 135 this.isTagFile = true; 136 this.tagInfo = tagInfo; 137 this.tagFileJarUrl = tagFileJarUrl; 138 if (tagFileJarUrl != null) { 139 isPackagedTagFile = true; 140 } 141 } 142 143 144 145 146 147 150 public String getClassPath() { 151 if( classPath != null ) 152 return classPath; 153 return rctxt.getClassPath(); 154 } 155 156 159 public void setClassPath(String classPath) { 160 this.classPath = classPath; 161 } 162 163 167 public ClassLoader getClassLoader() { 168 if( loader != null ) 169 return loader; 170 return rctxt.getParentClassLoader(); 171 } 172 173 public void setClassLoader(URLClassLoader loader) { 174 this.loader = loader; 175 } 176 177 public ClassLoader getJspLoader() { 178 if( jspLoader == null ) { 179 jspLoader = new JasperLoader 180 (new URL [] {baseUrl}, 181 getClassLoader(), 182 rctxt.getPermissionCollection(), 183 rctxt.getCodeSource()); 184 } 185 return jspLoader; 186 } 187 188 189 190 195 public String getOutputDir() { 196 if (outputDir == null) { 197 createOutputDir(); 198 } 199 200 return outputDir; 201 } 202 203 208 public Compiler createCompiler() throws JasperException { 209 if (jspCompiler != null ) { 210 return jspCompiler; 211 } 212 jspCompiler = null; 213 if (options.getCompilerClassName() != null) { 214 jspCompiler = createCompiler(options.getCompilerClassName()); 215 } else { 216 if (options.getCompiler() == null) { 217 jspCompiler = createCompiler("org.apache.jasper.compiler.JDTCompiler"); 218 if (jspCompiler == null) { 219 jspCompiler = createCompiler("org.apache.jasper.compiler.AntCompiler"); 220 } 221 } else { 222 jspCompiler = createCompiler("org.apache.jasper.compiler.AntCompiler"); 223 if (jspCompiler == null) { 224 jspCompiler = createCompiler("org.apache.jasper.compiler.JDTCompiler"); 225 } 226 } 227 } 228 if (jspCompiler == null) { 229 throw new IllegalStateException (Localizer.getMessage("jsp.error.compiler")); 230 } 231 jspCompiler.init(this, jsw); 232 return jspCompiler; 233 } 234 235 protected Compiler createCompiler(String className) { 236 Compiler compiler = null; 237 try { 238 compiler = (Compiler ) Class.forName(className).newInstance(); 239 } catch (InstantiationException e) { 240 log.warn(Localizer.getMessage("jsp.error.compiler"), e); 241 } catch (IllegalAccessException e) { 242 log.warn(Localizer.getMessage("jsp.error.compiler"), e); 243 } catch (NoClassDefFoundError e) { 244 if (log.isDebugEnabled()) { 245 log.debug(Localizer.getMessage("jsp.error.compiler"), e); 246 } 247 } catch (ClassNotFoundException e) { 248 if (log.isDebugEnabled()) { 249 log.debug(Localizer.getMessage("jsp.error.compiler"), e); 250 } 251 } 252 return compiler; 253 } 254 255 public Compiler getCompiler() { 256 return jspCompiler; 257 } 258 259 260 261 265 public String resolveRelativeUri(String uri) { 266 if (uri.startsWith("/") || uri.startsWith(File.separator)) { 269 return uri; 270 } else { 271 return baseURI + uri; 272 } 273 } 274 275 281 public java.io.InputStream getResourceAsStream(String res) { 282 return context.getResourceAsStream(canonicalURI(res)); 283 } 284 285 286 public URL getResource(String res) throws MalformedURLException { 287 return context.getResource(canonicalURI(res)); 288 } 289 290 public Set getResourcePaths(String path) { 291 return context.getResourcePaths(canonicalURI(path)); 292 } 293 294 298 public String getRealPath(String path) { 299 if (context != null) { 300 return context.getRealPath(path); 301 } 302 return path; 303 } 304 305 313 public URL getTagFileJarUrl(String tagFile) { 314 return this.tagFileJarUrls.get(tagFile); 315 } 316 317 public void setTagFileJarUrl(String tagFile, URL tagFileURL) { 318 this.tagFileJarUrls.put(tagFile, tagFileURL); 319 } 320 321 327 public URL getTagFileJarUrl() { 328 return this.tagFileJarUrl; 329 } 330 331 332 333 337 public String getServletClassName() { 338 339 if (className != null) { 340 return className; 341 } 342 343 if (isTagFile) { 344 className = tagInfo.getTagClassName(); 345 int lastIndex = className.lastIndexOf('.'); 346 if (lastIndex != -1) { 347 className = className.substring(lastIndex + 1); 348 } 349 } else { 350 int iSep = jspUri.lastIndexOf('/') + 1; 351 className = JspUtil.makeJavaIdentifier(jspUri.substring(iSep)); 352 } 353 return className; 354 } 355 356 public void setServletClassName(String className) { 357 this.className = className; 358 } 359 360 364 public String getJspFile() { 365 return jspUri; 366 } 367 368 372 public boolean isErrorPage() { 373 return isErrPage; 374 } 375 376 public void setErrorPage(boolean isErrPage) { 377 this.isErrPage = isErrPage; 378 } 379 380 public boolean isTagFile() { 381 return isTagFile; 382 } 383 384 public TagInfo getTagInfo() { 385 return tagInfo; 386 } 387 388 public void setTagInfo(TagInfo tagi) { 389 tagInfo = tagi; 390 } 391 392 397 public boolean isPrototypeMode() { 398 return protoTypeMode; 399 } 400 401 public void setPrototypeMode(boolean pm) { 402 protoTypeMode = pm; 403 } 404 405 410 public String getServletPackageName() { 411 if (isTagFile()) { 412 String className = tagInfo.getTagClassName(); 413 int lastIndex = className.lastIndexOf('.'); 414 String pkgName = ""; 415 if (lastIndex != -1) { 416 pkgName = className.substring(0, lastIndex); 417 } 418 return pkgName; 419 } else { 420 String dPackageName = getDerivedPackageName(); 421 if (dPackageName.length() == 0) { 422 return basePackageName; 423 } 424 return basePackageName + '.' + getDerivedPackageName(); 425 } 426 } 427 428 protected String getDerivedPackageName() { 429 if (derivedPackageName == null) { 430 int iSep = jspUri.lastIndexOf('/'); 431 derivedPackageName = (iSep > 0) ? 432 JspUtil.makeJavaPackage(jspUri.substring(1,iSep)) : ""; 433 } 434 return derivedPackageName; 435 } 436 437 440 public void setServletPackageName(String servletPackageName) { 441 this.basePackageName = servletPackageName; 442 } 443 444 448 public String getServletJavaFileName() { 449 if (servletJavaFileName == null) { 450 servletJavaFileName = getOutputDir() + getServletClassName() + ".java"; 451 } 452 return servletJavaFileName; 453 } 454 455 458 public Options getOptions() { 459 return options; 460 } 461 462 public ServletContext getServletContext() { 463 return context; 464 } 465 466 public JspRuntimeContext getRuntimeContext() { 467 return rctxt; 468 } 469 470 473 public String getJavaPath() { 474 475 if (javaPath != null) { 476 return javaPath; 477 } 478 479 if (isTagFile()) { 480 String tagName = tagInfo.getTagClassName(); 481 javaPath = tagName.replace('.', '/') + ".java"; 482 } else { 483 javaPath = getServletPackageName().replace('.', '/') + '/' + 484 getServletClassName() + ".java"; 485 } 486 return javaPath; 487 } 488 489 public String getClassFileName() { 490 if (classFileName == null) { 491 classFileName = getOutputDir() + getServletClassName() + ".class"; 492 } 493 return classFileName; 494 } 495 496 501 public String getContentType() { 502 return contentType; 503 } 504 505 public void setContentType(String contentType) { 506 this.contentType = contentType; 507 } 508 509 512 public ServletWriter getWriter() { 513 return writer; 514 } 515 516 public void setWriter(ServletWriter writer) { 517 this.writer = writer; 518 } 519 520 529 public String [] getTldLocation(String uri) throws JasperException { 530 String [] location = 531 getOptions().getTldLocationsCache().getLocation(uri); 532 return location; 533 } 534 535 538 public boolean keepGenerated() { 539 return getOptions().getKeepGenerated(); 540 } 541 542 544 public void incrementRemoved() { 545 if (removed == 0 && rctxt != null) { 546 rctxt.removeWrapper(jspUri); 547 } 548 removed++; 549 } 550 551 public boolean isRemoved() { 552 if (removed > 1 ) { 553 return true; 554 } 555 return false; 556 } 557 558 560 public void compile() throws JasperException, FileNotFoundException { 561 createCompiler(); 562 if (isPackagedTagFile || jspCompiler.isOutDated()) { 563 try { 564 jspCompiler.removeGeneratedFiles(); 565 jspLoader = null; 566 jspCompiler.compile(); 567 jsw.setReload(true); 568 jsw.setCompilationException(null); 569 } catch (JasperException ex) { 570 jsw.setCompilationException(ex); 572 throw ex; 573 } catch (Exception ex) { 574 JasperException je = new JasperException( 575 Localizer.getMessage("jsp.error.unable.compile"), 576 ex); 577 jsw.setCompilationException(je); 579 throw je; 580 } 581 } 582 } 583 584 586 public Class load() 587 throws JasperException, FileNotFoundException 588 { 589 try { 590 getJspLoader(); 591 592 String name; 593 if (isTagFile()) { 594 name = tagInfo.getTagClassName(); 595 } else { 596 name = getServletPackageName() + "." + getServletClassName(); 597 } 598 servletClass = jspLoader.loadClass(name); 599 } catch (ClassNotFoundException cex) { 600 throw new JasperException(Localizer.getMessage("jsp.error.unable.load"), 601 cex); 602 } catch (Exception ex) { 603 throw new JasperException(Localizer.getMessage("jsp.error.unable.compile"), 604 ex); 605 } 606 removed = 0; 607 return servletClass; 608 } 609 610 612 static Object outputDirLock = new Object (); 613 614 public void checkOutputDir() { 615 if (outputDir != null) { 616 if (!(new File (outputDir)).exists()) { 617 makeOutputDir(); 618 } 619 } else { 620 createOutputDir(); 621 } 622 } 623 624 protected boolean makeOutputDir() { 625 synchronized(outputDirLock) { 626 File outDirFile = new File (outputDir); 627 return (outDirFile.exists() || outDirFile.mkdirs()); 628 } 629 } 630 631 protected void createOutputDir() { 632 String path = null; 633 if (isTagFile()) { 634 String tagName = tagInfo.getTagClassName(); 635 path = tagName.replace('.', '/'); 636 path = path.substring(0, path.lastIndexOf('/')); 637 } else { 638 path = getServletPackageName().replace('.', '/'); 639 } 640 641 try { 643 baseUrl = options.getScratchDir().toURL(); 644 String outUrlString = baseUrl.toString() + '/' + path; 645 URL outUrl = new URL (outUrlString); 646 outputDir = outUrl.getFile() + File.separator; 647 if (!makeOutputDir()) { 648 throw new IllegalStateException (Localizer.getMessage("jsp.error.outputfolder")); 649 } 650 } catch (MalformedURLException e) { 651 throw new IllegalStateException (Localizer.getMessage("jsp.error.outputfolder"), e); 652 } 653 } 654 655 protected static final boolean isPathSeparator(char c) { 656 return (c == '/' || c == '\\'); 657 } 658 659 protected static final String canonicalURI(String s) { 660 if (s == null) return null; 661 StringBuffer result = new StringBuffer (); 662 final int len = s.length(); 663 int pos = 0; 664 while (pos < len) { 665 char c = s.charAt(pos); 666 if ( isPathSeparator(c) ) { 667 671 while (pos+1 < len && isPathSeparator(s.charAt(pos+1))) { 672 ++pos; 673 } 674 675 if (pos+1 < len && s.charAt(pos+1) == '.') { 676 679 if (pos+2 >= len) break; 680 681 switch (s.charAt(pos+2)) { 682 686 case '/': 687 case '\\': 688 pos += 2; 689 continue; 690 691 695 case '.': 696 if (pos+3 < len && isPathSeparator(s.charAt(pos+3))) { 698 pos += 3; 699 int separatorPos = result.length()-1; 700 while (separatorPos >= 0 && 701 ! isPathSeparator(result 702 .charAt(separatorPos))) { 703 --separatorPos; 704 } 705 if (separatorPos >= 0) 706 result.setLength(separatorPos); 707 continue; 708 } 709 } 710 } 711 } 712 result.append(c); 713 ++pos; 714 } 715 return result.toString(); 716 } 717 } 718 719 | Popular Tags |