1 19 20 package com.sslexplorer.server.jetty; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.jasper.JasperException; 30 import org.apache.jasper.JspC; 31 import org.mortbay.http.ResourceCache; 32 33 import com.sslexplorer.boot.ContextHolder; 34 import com.sslexplorer.boot.Util; 35 36 43 public class JspPrecompiler { 44 45 final static Log log = LogFactory.getLog(JspPrecompiler.class); 46 47 51 public JspPrecompiler() { 52 } 53 54 55 61 public void compile(CustomWebApplicationContext context) throws Exception { 62 63 64 File oldWebAppDir = new File ("webapp" + File.separator + "WEB-INF" + File.separator + "applications"); 66 if(oldWebAppDir.exists()) { 67 Util.delTree(oldWebAppDir); 68 } 69 70 73 List <String > uris = new ArrayList <String >(); 74 List <ResourceCache> resourceCaches = new ArrayList <ResourceCache>(context.getResourceCaches()); 75 Collections.reverse(resourceCaches); 76 for(ResourceCache c : resourceCaches) { 77 scanRoot(c.getBaseResource().getFile().getAbsolutePath(), uris); 78 } 79 File coreWebapp = new File ("webapp"); 80 scanRoot(coreWebapp.getAbsolutePath(), uris); 81 82 83 84 JspC jspc = new JspC() { 85 public void scanFiles( File base ) throws JasperException { 86 addExtension("jsp"); 87 addExtension("jspx"); 88 addExtension("jspf"); 89 super.scanFiles(base); 90 }; 91 }; 92 jspc.setClassPath(Util.getClassPath(context.getClassLoader())); 93 jspc.setCompile(true); 94 File dir = ContextHolder.getContext().getTempDirectory(); 95 jspc.setOutputDir(dir.getAbsolutePath()); 96 jspc.setAddWebXmlMappings(false); 97 98 102 coreOnlyPrecompile(uris, coreWebapp); 103 104 jspc.setJspFiles(asCommaSeparatedList(uris)); 105 106 jspc.setListErrors(true); 107 jspc.setVerbose(9); 108 jspc.execute(); 109 } 110 111 void coreOnlyPrecompile(List <String > uris, File coreWebapp) { 112 for(String uri : new ArrayList <String >(uris)) { 113 if(!uri.startsWith(coreWebapp.getAbsolutePath())) { 114 uris.remove(uri); 115 } 116 } 117 } 118 119 120 String asCommaSeparatedList(List <String > uris) { 121 StringBuffer buf = new StringBuffer (); 122 for(String uri : uris) { 123 if(buf.length() != 0) { 124 buf.append(","); 125 } 126 buf.append(uri); 127 } 128 return buf.toString(); 129 } 130 131 String asList(List <String > uris) { 132 StringBuffer buf = new StringBuffer (); 133 for(String uri : uris) { 134 if(buf.length() != 0) { 135 buf.append("\n"); 136 } 137 buf.append(uri); 138 } 139 return buf.toString(); 140 } 141 142 143 void scanRoot(String root, List <String > uris) { 144 List <String > paths = new ArrayList <String >(); 145 File dir = new File (root); 146 scanDir(dir, uris, paths, ""); 147 } 148 149 void scanDir(File file, List <String > uris, List <String > paths, String path) { 150 if(file.isDirectory()) { 151 File [] files = file.listFiles(); 152 for(int i = 0 ; i < files.length; i++) { 153 String newPath = path.equals("") ? files[i].getName() : ( path + File.separator + files[i].getName() ) ; 154 scanDir(files[i], uris, paths, newPath); 155 } 156 } 157 else { 158 if(file.getName().endsWith(".jsp") || file.getName().endsWith(".jspf")) { 159 String uri = file.getAbsolutePath(); 160 if(!paths.contains(path)) { 161 uris.add(uri); 162 paths.add(path); 163 } 164 } 165 } 166 } 167 168 } 169 | Popular Tags |