1 17 18 package org.apache.jasper; 19 20 import java.io.BufferedReader ; 21 import java.io.CharArrayWriter ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.FileReader ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.io.Writer ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.net.URLClassLoader ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.HashMap ; 39 import java.util.Stack ; 40 import java.util.StringTokenizer ; 41 import java.util.Vector ; 42 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.apache.jasper.compiler.Compiler; 46 import org.apache.jasper.compiler.JspConfig; 47 import org.apache.jasper.compiler.JspRuntimeContext; 48 import org.apache.jasper.compiler.Localizer; 49 import org.apache.jasper.compiler.TagPluginManager; 50 import org.apache.jasper.compiler.TldLocationsCache; 51 import org.apache.jasper.servlet.JspCServletContext; 52 53 import org.apache.tools.ant.AntClassLoader; 54 import org.apache.tools.ant.Project; 55 import org.apache.tools.ant.util.FileUtils; 56 57 92 public class JspC implements Options { 93 94 public static final String DEFAULT_IE_CLASS_ID = 95 "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"; 96 97 protected static Log log = LogFactory.getLog(JspC.class); 99 100 protected static final String SWITCH_VERBOSE = "-v"; 101 protected static final String SWITCH_HELP = "-help"; 102 protected static final String SWITCH_OUTPUT_DIR = "-d"; 103 protected static final String SWITCH_PACKAGE_NAME = "-p"; 104 protected static final String SWITCH_CACHE = "-cache"; 105 protected static final String SWITCH_CLASS_NAME = "-c"; 106 protected static final String SWITCH_FULL_STOP = "--"; 107 protected static final String SWITCH_COMPILE = "-compile"; 108 protected static final String SWITCH_SOURCE = "-source"; 109 protected static final String SWITCH_TARGET = "-target"; 110 protected static final String SWITCH_URI_BASE = "-uribase"; 111 protected static final String SWITCH_URI_ROOT = "-uriroot"; 112 protected static final String SWITCH_FILE_WEBAPP = "-webapp"; 113 protected static final String SWITCH_WEBAPP_INC = "-webinc"; 114 protected static final String SWITCH_WEBAPP_XML = "-webxml"; 115 protected static final String SWITCH_MAPPED = "-mapped"; 116 protected static final String SWITCH_XPOWERED_BY = "-xpoweredBy"; 117 protected static final String SWITCH_TRIM_SPACES = "-trimSpaces"; 118 protected static final String SWITCH_CLASSPATH = "-classpath"; 119 protected static final String SWITCH_DIE = "-die"; 120 protected static final String SWITCH_POOLING = "-poolingEnabled"; 121 protected static final String SWITCH_ENCODING = "-javaEncoding"; 122 protected static final String SWITCH_SMAP = "-smap"; 123 protected static final String SWITCH_DUMP_SMAP = "-dumpsmap"; 124 125 protected static final String SHOW_SUCCESS ="-s"; 126 protected static final String LIST_ERRORS = "-l"; 127 protected static final int INC_WEBXML = 10; 128 protected static final int ALL_WEBXML = 20; 129 protected static final int DEFAULT_DIE_LEVEL = 1; 130 protected static final int NO_DIE_LEVEL = 0; 131 132 protected static final String [] insertBefore = 133 { "</web-app>", "<servlet-mapping>", "<session-config>", 134 "<mime-mapping>", "<welcome-file-list>", "<error-page>", "<taglib>", 135 "<resource-env-ref>", "<resource-ref>", "<security-constraint>", 136 "<login-config>", "<security-role>", "<env-entry>", "<ejb-ref>", 137 "<ejb-local-ref>" }; 138 139 protected static int die; 140 protected String classPath = null; 141 protected URLClassLoader loader = null; 142 protected boolean trimSpaces = false; 143 protected boolean genStringAsCharArray = false; 144 protected boolean xpoweredBy; 145 protected boolean mappedFile = false; 146 protected boolean poolingEnabled = true; 147 protected File scratchDir; 148 protected String ieClassId = DEFAULT_IE_CLASS_ID; 149 protected String targetPackage; 150 protected String targetClassName; 151 protected String uriBase; 152 protected String uriRoot; 153 protected Project project; 154 protected int dieLevel; 155 protected boolean helpNeeded = false; 156 protected boolean compile = false; 157 protected boolean smapSuppressed = true; 158 protected boolean smapDumped = false; 159 protected boolean caching = true; 160 protected Map cache = new HashMap (); 161 162 protected String compiler = null; 163 164 protected String compilerTargetVM = "1.4"; 165 protected String compilerSourceVM = "1.4"; 166 167 protected boolean classDebugInfo = true; 168 169 173 protected boolean failOnError = true; 174 175 179 protected List extensions; 180 181 184 protected List pages = new Vector (); 185 186 190 protected boolean errorOnUseBeanInvalidClassAttribute = true; 191 192 196 protected String javaEncoding = "UTF-8"; 197 198 protected String webxmlFile; 200 protected int webxmlLevel; 201 protected boolean addWebXmlMappings = false; 202 203 protected Writer mapout; 204 protected CharArrayWriter servletout; 205 protected CharArrayWriter mappingout; 206 207 210 protected JspCServletContext context; 211 212 216 protected JspRuntimeContext rctxt; 217 218 221 protected TldLocationsCache tldLocationsCache = null; 222 223 protected JspConfig jspConfig = null; 224 protected TagPluginManager tagPluginManager = null; 225 226 protected boolean verbose = false; 227 protected boolean listErrors = false; 228 protected boolean showSuccess = false; 229 protected int argPos; 230 protected boolean fullstop = false; 231 protected String args[]; 232 233 public static void main(String arg[]) { 234 if (arg.length == 0) { 235 System.out.println(Localizer.getMessage("jspc.usage")); 236 } else { 237 try { 238 JspC jspc = new JspC(); 239 jspc.setArgs(arg); 240 if (jspc.helpNeeded) { 241 System.out.println(Localizer.getMessage("jspc.usage")); 242 } else { 243 jspc.execute(); 244 } 245 } catch (JasperException je) { 246 System.err.println(je); 247 if (die != NO_DIE_LEVEL) { 248 System.exit(die); 249 } 250 } 251 } 252 } 253 254 public void setArgs(String [] arg) throws JasperException { 255 args = arg; 256 String tok; 257 258 dieLevel = NO_DIE_LEVEL; 259 die = dieLevel; 260 261 while ((tok = nextArg()) != null) { 262 if (tok.equals(SWITCH_VERBOSE)) { 263 verbose = true; 264 showSuccess = true; 265 listErrors = true; 266 } else if (tok.equals(SWITCH_OUTPUT_DIR)) { 267 tok = nextArg(); 268 setOutputDir( tok ); 269 } else if (tok.equals(SWITCH_PACKAGE_NAME)) { 270 targetPackage = nextArg(); 271 } else if (tok.equals(SWITCH_COMPILE)) { 272 compile=true; 273 } else if (tok.equals(SWITCH_CLASS_NAME)) { 274 targetClassName = nextArg(); 275 } else if (tok.equals(SWITCH_URI_BASE)) { 276 uriBase=nextArg(); 277 } else if (tok.equals(SWITCH_URI_ROOT)) { 278 setUriroot( nextArg()); 279 } else if (tok.equals(SWITCH_FILE_WEBAPP)) { 280 setUriroot( nextArg()); 281 } else if ( tok.equals( SHOW_SUCCESS ) ) { 282 showSuccess = true; 283 } else if ( tok.equals( LIST_ERRORS ) ) { 284 listErrors = true; 285 } else if (tok.equals(SWITCH_WEBAPP_INC)) { 286 webxmlFile = nextArg(); 287 if (webxmlFile != null) { 288 webxmlLevel = INC_WEBXML; 289 } 290 } else if (tok.equals(SWITCH_WEBAPP_XML)) { 291 webxmlFile = nextArg(); 292 if (webxmlFile != null) { 293 webxmlLevel = ALL_WEBXML; 294 } 295 } else if (tok.equals(SWITCH_MAPPED)) { 296 mappedFile = true; 297 } else if (tok.equals(SWITCH_XPOWERED_BY)) { 298 xpoweredBy = true; 299 } else if (tok.equals(SWITCH_TRIM_SPACES)) { 300 setTrimSpaces(true); 301 } else if (tok.equals(SWITCH_CACHE)) { 302 tok = nextArg(); 303 if ("false".equals(tok)) { 304 caching = false; 305 } else { 306 caching = true; 307 } 308 } else if (tok.equals(SWITCH_CLASSPATH)) { 309 setClassPath(nextArg()); 310 } else if (tok.startsWith(SWITCH_DIE)) { 311 try { 312 dieLevel = Integer.parseInt( 313 tok.substring(SWITCH_DIE.length())); 314 } catch (NumberFormatException nfe) { 315 dieLevel = DEFAULT_DIE_LEVEL; 316 } 317 die = dieLevel; 318 } else if (tok.equals(SWITCH_HELP)) { 319 helpNeeded = true; 320 } else if (tok.equals(SWITCH_POOLING)) { 321 tok = nextArg(); 322 if ("false".equals(tok)) { 323 poolingEnabled = false; 324 } else { 325 poolingEnabled = true; 326 } 327 } else if (tok.equals(SWITCH_ENCODING)) { 328 setJavaEncoding(nextArg()); 329 } else if (tok.equals(SWITCH_SOURCE)) { 330 setCompilerSourceVM(nextArg()); 331 } else if (tok.equals(SWITCH_TARGET)) { 332 setCompilerTargetVM(nextArg()); 333 } else if (tok.equals(SWITCH_SMAP)) { 334 smapSuppressed = false; 335 } else if (tok.equals(SWITCH_DUMP_SMAP)) { 336 smapDumped = true; 337 } else { 338 if (tok.startsWith("-")) { 339 throw new JasperException("Unrecognized option: " + tok + 340 ". Use -help for help."); 341 } 342 if (!fullstop) { 343 argPos--; 344 } 345 break; 347 } 348 } 349 350 while( true ) { 352 String file = nextFile(); 353 if( file==null ) { 354 break; 355 } 356 pages.add( file ); 357 } 358 } 359 360 public boolean getKeepGenerated() { 361 return true; 363 } 364 365 public boolean getTrimSpaces() { 366 return trimSpaces; 367 } 368 369 public void setTrimSpaces(boolean ts) { 370 this.trimSpaces = ts; 371 } 372 373 public boolean isPoolingEnabled() { 374 return poolingEnabled; 375 } 376 377 public void setPoolingEnabled(boolean poolingEnabled) { 378 this.poolingEnabled = poolingEnabled; 379 } 380 381 public boolean isXpoweredBy() { 382 return xpoweredBy; 383 } 384 385 public void setXpoweredBy(boolean xpoweredBy) { 386 this.xpoweredBy = xpoweredBy; 387 } 388 389 public boolean getDisplaySourceFragment() { 390 return true; 391 } 392 393 public boolean getErrorOnUseBeanInvalidClassAttribute() { 394 return errorOnUseBeanInvalidClassAttribute; 395 } 396 397 public void setErrorOnUseBeanInvalidClassAttribute(boolean b) { 398 errorOnUseBeanInvalidClassAttribute = b; 399 } 400 401 public int getTagPoolSize() { 402 return Constants.MAX_POOL_SIZE; 403 } 404 405 408 public boolean getMappedFile() { 409 return mappedFile; 410 } 411 412 public Object getProtectionDomain() { 414 return null; 415 } 416 417 public boolean getSendErrorToClient() { 418 return true; 420 } 421 422 public void setClassDebugInfo( boolean b ) { 423 classDebugInfo=b; 424 } 425 426 public boolean getClassDebugInfo() { 427 return classDebugInfo; 429 } 430 431 434 public boolean isCaching() { 435 return caching; 436 } 437 438 441 public void setCaching(boolean caching) { 442 this.caching = caching; 443 } 444 445 448 public Map getCache() { 449 return cache; 450 } 451 452 455 public int getCheckInterval() { 456 return 0; 457 } 458 459 462 public int getModificationTestInterval() { 463 return 0; 464 } 465 466 469 public boolean getDevelopment() { 470 return false; 471 } 472 473 476 public boolean isSmapSuppressed() { 477 return smapSuppressed; 478 } 479 480 483 public void setSmapSuppressed(boolean smapSuppressed) { 484 this.smapSuppressed = smapSuppressed; 485 } 486 487 488 491 public boolean isSmapDumped() { 492 return smapDumped; 493 } 494 495 498 public void setSmapDumped(boolean smapDumped) { 499 this.smapDumped = smapDumped; 500 } 501 502 503 510 public void setGenStringAsCharArray(boolean genStringAsCharArray) { 511 this.genStringAsCharArray = genStringAsCharArray; 512 } 513 514 520 public boolean genStringAsCharArray() { 521 return genStringAsCharArray; 522 } 523 524 530 public void setIeClassId(String ieClassId) { 531 this.ieClassId = ieClassId; 532 } 533 534 540 public String getIeClassId() { 541 return ieClassId; 542 } 543 544 public File getScratchDir() { 545 return scratchDir; 546 } 547 548 public Class getJspCompilerPlugin() { 549 return null; 551 } 552 553 public String getJspCompilerPath() { 554 return null; 556 } 557 558 561 public String getCompiler() { 562 return compiler; 563 } 564 565 public void setCompiler(String c) { 566 compiler=c; 567 } 568 569 572 public String getCompilerClassName() { 573 return null; 574 } 575 576 579 public String getCompilerTargetVM() { 580 return compilerTargetVM; 581 } 582 583 public void setCompilerTargetVM(String vm) { 584 compilerTargetVM = vm; 585 } 586 587 590 public String getCompilerSourceVM() { 591 return compilerSourceVM; 592 } 593 594 597 public void setCompilerSourceVM(String vm) { 598 compilerSourceVM = vm; 599 } 600 601 public TldLocationsCache getTldLocationsCache() { 602 return tldLocationsCache; 603 } 604 605 611 public String getJavaEncoding() { 612 return javaEncoding; 613 } 614 615 621 public void setJavaEncoding(String encodingName) { 622 javaEncoding = encodingName; 623 } 624 625 public boolean getFork() { 626 return false; 627 } 628 629 public String getClassPath() { 630 if( classPath != null ) 631 return classPath; 632 return System.getProperty("java.class.path"); 633 } 634 635 public void setClassPath(String s) { 636 classPath=s; 637 } 638 639 645 public List getExtensions() { 646 return extensions; 647 } 648 649 655 protected void addExtension(final String extension) { 656 if(extension != null) { 657 if(extensions == null) { 658 extensions = new Vector (); 659 } 660 661 extensions.add(extension); 662 } 663 } 664 665 670 public void setProject(final Project theProject) { 671 project = theProject; 672 } 673 674 680 public Project getProject() { 681 return project; 682 } 683 684 688 public void setUriroot( String s ) { 689 if( s==null ) { 690 uriRoot = s; 691 return; 692 } 693 try { 694 uriRoot = resolveFile(s).getCanonicalPath(); 695 } catch( Exception ex ) { 696 uriRoot = s; 697 } 698 } 699 700 709 public void setJspFiles(final String jspFiles) { 710 if(jspFiles == null) { 711 return; 712 } 713 714 StringTokenizer tok = new StringTokenizer (jspFiles, ","); 715 while (tok.hasMoreTokens()) { 716 pages.add(tok.nextToken()); 717 } 718 } 719 720 725 public void setCompile( final boolean b ) { 726 compile = b; 727 } 728 729 736 public void setVerbose( final int level ) { 737 if (level > 0) { 738 verbose = true; 739 showSuccess = true; 740 listErrors = true; 741 } 742 } 743 744 public void setValidateXml( boolean b ) { 745 org.apache.jasper.xmlparser.ParserUtils.validating=b; 746 } 747 748 public void setListErrors( boolean b ) { 749 listErrors = b; 750 } 751 752 public void setOutputDir( String s ) { 753 if( s!= null ) { 754 scratchDir = resolveFile(s).getAbsoluteFile(); 755 } else { 756 scratchDir=null; 757 } 758 } 759 760 public void setPackage( String p ) { 761 targetPackage=p; 762 } 763 764 769 public void setClassName( String p ) { 770 targetClassName=p; 771 } 772 773 776 public void setWebXmlFragment( String s ) { 777 webxmlFile=resolveFile(s).getAbsolutePath(); 778 webxmlLevel=INC_WEBXML; 779 } 780 781 784 public void setWebXml( String s ) { 785 webxmlFile=resolveFile(s).getAbsolutePath(); 786 webxmlLevel=ALL_WEBXML; 787 } 788 789 public void setAddWebXmlMappings(boolean b) { 790 addWebXmlMappings = b; 791 } 792 793 796 public void setFailOnError(final boolean b) { 797 failOnError = b; 798 } 799 800 public boolean getFailOnError() { 801 return failOnError; 802 } 803 804 807 public JspConfig getJspConfig() { 808 return jspConfig; 809 } 810 811 public TagPluginManager getTagPluginManager() { 812 return tagPluginManager; 813 } 814 815 public void generateWebMapping( String file, JspCompilationContext clctxt ) 816 throws IOException 817 { 818 String className = clctxt.getServletClassName(); 819 String packageName = clctxt.getServletPackageName(); 820 821 String thisServletName; 822 if ("".equals(packageName)) { 823 thisServletName = className; 824 } else { 825 thisServletName = packageName + '.' + className; 826 } 827 828 if (servletout != null) { 829 servletout.write("\n <servlet>\n <servlet-name>"); 830 servletout.write(thisServletName); 831 servletout.write("</servlet-name>\n <servlet-class>"); 832 servletout.write(thisServletName); 833 servletout.write("</servlet-class>\n </servlet>\n"); 834 } 835 if (mappingout != null) { 836 mappingout.write("\n <servlet-mapping>\n <servlet-name>"); 837 mappingout.write(thisServletName); 838 mappingout.write("</servlet-name>\n <url-pattern>"); 839 mappingout.write(file.replace('\\', '/')); 840 mappingout.write("</url-pattern>\n </servlet-mapping>\n"); 841 842 } 843 } 844 845 848 protected void mergeIntoWebXml() throws IOException { 849 850 File webappBase = new File (uriRoot); 851 File webXml = new File (webappBase, "WEB-INF/web.xml"); 852 File webXml2 = new File (webappBase, "WEB-INF/web2.xml"); 853 String insertStartMarker = 854 Localizer.getMessage("jspc.webinc.insertStart"); 855 String insertEndMarker = 856 Localizer.getMessage("jspc.webinc.insertEnd"); 857 858 BufferedReader reader = new BufferedReader (new FileReader (webXml)); 859 BufferedReader fragmentReader = 860 new BufferedReader (new FileReader (webxmlFile)); 861 PrintWriter writer = new PrintWriter (new FileWriter (webXml2)); 862 863 int pos = -1; 865 String line = null; 866 while (true) { 867 line = reader.readLine(); 868 if (line == null) { 869 break; 870 } 871 if (line.indexOf(insertStartMarker) >= 0) { 873 while (true) { 874 line = reader.readLine(); 875 if (line == null) { 876 return; 877 } 878 if (line.indexOf(insertEndMarker) >= 0) { 879 line = reader.readLine(); 880 line = reader.readLine(); 881 if (line == null) { 882 return; 883 } 884 break; 885 } 886 } 887 } 888 for (int i = 0; i < insertBefore.length; i++) { 889 pos = line.indexOf(insertBefore[i]); 890 if (pos >= 0) 891 break; 892 } 893 if (pos >= 0) { 894 writer.print(line.substring(0, pos)); 895 break; 896 } else { 897 writer.println(line); 898 } 899 } 900 901 writer.println(insertStartMarker); 902 while (true) { 903 String line2 = fragmentReader.readLine(); 904 if (line2 == null) { 905 writer.println(); 906 break; 907 } 908 writer.println(line2); 909 } 910 writer.println(insertEndMarker); 911 writer.println(); 912 913 for (int i = 0; i < pos; i++) { 914 writer.print(" "); 915 } 916 writer.println(line.substring(pos)); 917 918 while (true) { 919 line = reader.readLine(); 920 if (line == null) { 921 break; 922 } 923 writer.println(line); 924 } 925 writer.close(); 926 927 reader.close(); 928 fragmentReader.close(); 929 930 FileInputStream fis = new FileInputStream (webXml2); 931 FileOutputStream fos = new FileOutputStream (webXml); 932 933 byte buf[] = new byte[512]; 934 while (true) { 935 int n = fis.read(buf); 936 if (n < 0) { 937 break; 938 } 939 fos.write(buf, 0, n); 940 } 941 942 fis.close(); 943 fos.close(); 944 945 webXml2.delete(); 946 (new File (webxmlFile)).delete(); 947 948 } 949 950 protected void processFile(String file) 951 throws JasperException 952 { 953 ClassLoader originalClassLoader = null; 954 955 try { 956 if (scratchDir == null) { 958 String temp = System.getProperty("java.io.tmpdir"); 959 if (temp == null) { 960 temp = ""; 961 } 962 scratchDir = new File (new File (temp).getAbsolutePath()); 963 } 964 965 String jspUri=file.replace('\\','/'); 966 JspCompilationContext clctxt = new JspCompilationContext 967 ( jspUri, false, this, context, null, rctxt ); 968 969 970 if ((targetClassName != null) && (targetClassName.length() > 0)) { 971 clctxt.setServletClassName(targetClassName); 972 targetClassName = null; 973 } 974 if (targetPackage != null) { 975 clctxt.setServletPackageName(targetPackage); 976 } 977 978 originalClassLoader = Thread.currentThread().getContextClassLoader(); 979 if( loader==null ) { 980 initClassLoader( clctxt ); 981 } 982 Thread.currentThread().setContextClassLoader(loader); 983 984 clctxt.setClassLoader(loader); 985 clctxt.setClassPath(classPath); 986 987 Compiler clc = clctxt.createCompiler(); 988 989 if( clc.isOutDated(compile) ) { 994 clc.compile(compile, true); 995 } 996 997 generateWebMapping( file, clctxt ); 999 if ( showSuccess ) { 1000 log.info( "Built File: " + file ); 1001 } 1002 1003 } catch (JasperException je) { 1004 Throwable rootCause = je; 1005 while (rootCause instanceof JasperException 1006 && ((JasperException) rootCause).getRootCause() != null) { 1007 rootCause = ((JasperException) rootCause).getRootCause(); 1008 } 1009 if (rootCause != je) { 1010 log.error(Localizer.getMessage("jspc.error.generalException", 1011 file), 1012 rootCause); 1013 } 1014 1015 if(getFailOnError()) { 1017 throw je; 1018 } else { 1019 log.error(je.getMessage()); 1020 } 1021 1022 } catch (Exception e) { 1023 if ((e instanceof FileNotFoundException ) && log.isWarnEnabled()) { 1024 log.warn(Localizer.getMessage("jspc.error.fileDoesNotExist", 1025 e.getMessage())); 1026 } 1027 throw new JasperException(e); 1028 } finally { 1029 if(originalClassLoader != null) { 1030 Thread.currentThread().setContextClassLoader(originalClassLoader); 1031 } 1032 } 1033 } 1034 1035 1039 public void scanFiles( File base ) throws JasperException { 1040 Stack <String > dirs = new Stack <String >(); 1041 dirs.push(base.toString()); 1042 1043 if ((getExtensions() == null) || (getExtensions().size() < 2)) { 1045 addExtension("jsp"); 1046 addExtension("jspx"); 1047 } 1048 1049 while (!dirs.isEmpty()) { 1050 String s = dirs.pop(); 1051 File f = new File (s); 1052 if (f.exists() && f.isDirectory()) { 1053 String [] files = f.list(); 1054 String ext; 1055 for (int i = 0; (files != null) && i < files.length; i++) { 1056 File f2 = new File (s, files[i]); 1057 if (f2.isDirectory()) { 1058 dirs.push(f2.getPath()); 1059 } else { 1060 String path = f2.getPath(); 1061 String uri = path.substring(uriRoot.length()); 1062 ext = files[i].substring(files[i].lastIndexOf('.') +1); 1063 if (getExtensions().contains(ext) || 1064 jspConfig.isJspPage(uri)) { 1065 pages.add(path); 1066 } 1067 } 1068 } 1069 } 1070 } 1071 } 1072 1073 1078 public void execute() throws JasperException { 1079 if(log.isDebugEnabled()) { 1080 log.debug("execute() starting for " + pages.size() + " pages."); 1081 } 1082 1083 try { 1084 if (uriRoot == null) { 1085 if( pages.size() == 0 ) { 1086 throw new JasperException( 1087 Localizer.getMessage("jsp.error.jspc.missingTarget")); 1088 } 1089 String firstJsp = (String ) pages.get( 0 ); 1090 File firstJspF = new File ( firstJsp ); 1091 if (!firstJspF.exists()) { 1092 throw new JasperException( 1093 Localizer.getMessage("jspc.error.fileDoesNotExist", 1094 firstJsp)); 1095 } 1096 locateUriRoot( firstJspF ); 1097 } 1098 1099 if (uriRoot == null) { 1100 throw new JasperException( 1101 Localizer.getMessage("jsp.error.jspc.no_uriroot")); 1102 } 1103 1104 if( context==null ) { 1105 initServletContext(); 1106 } 1107 1108 if (pages.size() == 0) { 1110 scanFiles( new File ( uriRoot )); 1111 } 1112 1113 File uriRootF = new File (uriRoot); 1114 if (!uriRootF.exists() || !uriRootF.isDirectory()) { 1115 throw new JasperException( 1116 Localizer.getMessage("jsp.error.jspc.uriroot_not_dir")); 1117 } 1118 1119 initWebXml(); 1120 1121 Iterator iter = pages.iterator(); 1122 while (iter.hasNext()) { 1123 String nextjsp = iter.next().toString(); 1124 File fjsp = new File (nextjsp); 1125 if (!fjsp.isAbsolute()) { 1126 fjsp = new File (uriRootF, nextjsp); 1127 } 1128 if (!fjsp.exists()) { 1129 if (log.isWarnEnabled()) { 1130 log.warn 1131 (Localizer.getMessage 1132 ("jspc.error.fileDoesNotExist", fjsp.toString())); 1133 } 1134 continue; 1135 } 1136 String s = fjsp.getAbsolutePath(); 1137 if (s.startsWith(uriRoot)) { 1138 nextjsp = s.substring(uriRoot.length()); 1139 } 1140 if (nextjsp.startsWith("." + File.separatorChar)) { 1141 nextjsp = nextjsp.substring(2); 1142 } 1143 processFile(nextjsp); 1144 } 1145 1146 completeWebXml(); 1147 1148 if (addWebXmlMappings) { 1149 mergeIntoWebXml(); 1150 } 1151 1152 } catch (IOException ioe) { 1153 throw new JasperException(ioe); 1154 1155 } catch (JasperException je) { 1156 Throwable rootCause = je; 1157 while (rootCause instanceof JasperException 1158 && ((JasperException) rootCause).getRootCause() != null) { 1159 rootCause = ((JasperException) rootCause).getRootCause(); 1160 } 1161 if (rootCause != je) { 1162 rootCause.printStackTrace(); 1163 } 1164 throw je; 1165 } finally { 1166 if (loader != null) { 1167 LogFactory.release(loader); 1168 } 1169 } 1170 } 1171 1172 1174 protected String nextArg() { 1175 if ((argPos >= args.length) 1176 || (fullstop = SWITCH_FULL_STOP.equals(args[argPos]))) { 1177 return null; 1178 } else { 1179 return args[argPos++]; 1180 } 1181 } 1182 1183 protected String nextFile() { 1184 if (fullstop) argPos++; 1185 if (argPos >= args.length) { 1186 return null; 1187 } else { 1188 return args[argPos++]; 1189 } 1190 } 1191 1192 protected void initWebXml() { 1193 try { 1194 if (webxmlLevel >= INC_WEBXML) { 1195 File fmapings = new File (webxmlFile); 1196 mapout = new FileWriter (fmapings); 1197 servletout = new CharArrayWriter (); 1198 mappingout = new CharArrayWriter (); 1199 } else { 1200 mapout = null; 1201 servletout = null; 1202 mappingout = null; 1203 } 1204 if (webxmlLevel >= ALL_WEBXML) { 1205 mapout.write(Localizer.getMessage("jspc.webxml.header")); 1206 mapout.flush(); 1207 } else if ((webxmlLevel>= INC_WEBXML) && !addWebXmlMappings) { 1208 mapout.write(Localizer.getMessage("jspc.webinc.header")); 1209 mapout.flush(); 1210 } 1211 } catch (IOException ioe) { 1212 mapout = null; 1213 servletout = null; 1214 mappingout = null; 1215 } 1216 } 1217 1218 protected void completeWebXml() { 1219 if (mapout != null) { 1220 try { 1221 servletout.writeTo(mapout); 1222 mappingout.writeTo(mapout); 1223 if (webxmlLevel >= ALL_WEBXML) { 1224 mapout.write(Localizer.getMessage("jspc.webxml.footer")); 1225 } else if ((webxmlLevel >= INC_WEBXML) && !addWebXmlMappings) { 1226 mapout.write(Localizer.getMessage("jspc.webinc.footer")); 1227 } 1228 mapout.close(); 1229 } catch (IOException ioe) { 1230 } 1232 } 1233 } 1234 1235 protected void initServletContext() { 1236 try { 1237 context =new JspCServletContext 1238 (new PrintWriter (System.out), 1239 new URL ("file:" + uriRoot.replace('\\','/') + '/')); 1240 tldLocationsCache = new TldLocationsCache(context, true); 1241 } catch (MalformedURLException me) { 1242 System.out.println("**" + me); 1243 } 1244 rctxt = new JspRuntimeContext(context, this); 1245 jspConfig = new JspConfig(context); 1246 tagPluginManager = new TagPluginManager(context); 1247 } 1248 1249 1256 protected void initClassLoader(JspCompilationContext clctxt) 1257 throws IOException { 1258 1259 classPath = getClassPath(); 1260 1261 ClassLoader jspcLoader = getClass().getClassLoader(); 1262 if (jspcLoader instanceof AntClassLoader) { 1263 classPath += File.pathSeparator 1264 + ((AntClassLoader) jspcLoader).getClasspath(); 1265 } 1266 1267 ArrayList <URL > urls = new ArrayList <URL >(); 1269 StringTokenizer tokenizer = new StringTokenizer (classPath, 1270 File.pathSeparator); 1271 while (tokenizer.hasMoreTokens()) { 1272 String path = tokenizer.nextToken(); 1273 try { 1274 File libFile = new File (path); 1275 urls.add(libFile.toURL()); 1276 } catch (IOException ioe) { 1277 throw new RuntimeException (ioe.toString()); 1281 } 1282 } 1283 1284 File webappBase = new File (uriRoot); 1285 if (webappBase.exists()) { 1286 File classes = new File (webappBase, "/WEB-INF/classes"); 1287 try { 1288 if (classes.exists()) { 1289 classPath = classPath + File.pathSeparator 1290 + classes.getCanonicalPath(); 1291 urls.add(classes.getCanonicalFile().toURL()); 1292 } 1293 } catch (IOException ioe) { 1294 throw new RuntimeException (ioe.toString()); 1298 } 1299 File lib = new File (webappBase, "/WEB-INF/lib"); 1300 if (lib.exists() && lib.isDirectory()) { 1301 String [] libs = lib.list(); 1302 for (int i = 0; i < libs.length; i++) { 1303 if( libs[i].length() <5 ) continue; 1304 String ext=libs[i].substring( libs[i].length() - 4 ); 1305 if (! ".jar".equalsIgnoreCase(ext)) { 1306 if (".tld".equalsIgnoreCase(ext)) { 1307 log.warn("TLD files should not be placed in " 1308 + "/WEB-INF/lib"); 1309 } 1310 continue; 1311 } 1312 try { 1313 File libFile = new File (lib, libs[i]); 1314 classPath = classPath + File.pathSeparator 1315 + libFile.getAbsolutePath(); 1316 urls.add(libFile.getAbsoluteFile().toURL()); 1317 } catch (IOException ioe) { 1318 throw new RuntimeException (ioe.toString()); 1322 } 1323 } 1324 } 1325 } 1326 1327 urls.add(new File (clctxt.getRealPath("/")).getCanonicalFile().toURL()); 1329 1330 URL urlsA[]=new URL [urls.size()]; 1331 urls.toArray(urlsA); 1332 loader = new URLClassLoader (urlsA, this.getClass().getClassLoader()); 1333 1334 } 1335 1336 1341 protected void locateUriRoot( File f ) { 1342 String tUriBase = uriBase; 1343 if (tUriBase == null) { 1344 tUriBase = "/"; 1345 } 1346 try { 1347 if (f.exists()) { 1348 f = new File (f.getAbsolutePath()); 1349 while (f != null) { 1350 File g = new File (f, "WEB-INF"); 1351 if (g.exists() && g.isDirectory()) { 1352 uriRoot = f.getCanonicalPath(); 1353 uriBase = tUriBase; 1354 if (log.isInfoEnabled()) { 1355 log.info(Localizer.getMessage( 1356 "jspc.implicit.uriRoot", 1357 uriRoot)); 1358 } 1359 break; 1360 } 1361 if (f.exists() && f.isDirectory()) { 1362 tUriBase = "/" + f.getName() + "/" + tUriBase; 1363 } 1364 1365 String fParent = f.getParent(); 1366 if (fParent == null) { 1367 break; 1368 } else { 1369 f = new File (fParent); 1370 } 1371 1372 } 1376 1377 if (uriRoot != null) { 1378 File froot = new File (uriRoot); 1379 uriRoot = froot.getCanonicalPath(); 1380 } 1381 } 1382 } catch (IOException ioe) { 1383 } 1387 } 1388 1389 1400 protected File resolveFile(final String s) { 1401 if(getProject() == null) { 1402 return FileUtils.newFileUtils().resolveFile(null, s); 1404 } else { 1405 return FileUtils.newFileUtils().resolveFile(getProject().getBaseDir(), s); 1406 } 1407 } 1408} 1409 | Popular Tags |