1 29 30 package com.caucho.jsp; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.FileSetType; 34 import com.caucho.config.types.PathPatternType; 35 import com.caucho.java.JavaCompiler; 36 import com.caucho.java.LineMap; 37 import com.caucho.lifecycle.Lifecycle; 38 import com.caucho.log.Log; 39 import com.caucho.server.webapp.WebApp; 40 import com.caucho.util.CompileException; 41 import com.caucho.util.L10N; 42 import com.caucho.vfs.Path; 43 import com.caucho.vfs.Vfs; 44 45 import javax.annotation.PostConstruct; 46 import javax.servlet.jsp.JspFactory ; 47 import java.util.ArrayList ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 54 public class JspPrecompileResource { 55 private static final Logger log = Log.open(JspPrecompileResource.class); 56 private static final L10N L = new L10N(JspPrecompileResource.class); 57 58 private FileSetType _fileSet; 59 60 private WebApp _webApp; 61 62 private final Lifecycle _lifecycle = new Lifecycle(); 63 64 67 public void setWebApp(WebApp app) 68 { 69 _webApp = app; 70 } 71 72 75 public FileSetType createFileset() 76 { 77 if (_fileSet == null) { 78 _fileSet = new FileSetType(); 79 _fileSet.setDir(Vfs.lookup()); 80 } 81 82 return _fileSet; 83 } 84 85 88 public FileSetType createFileSet() 89 { 90 return createFileset(); 91 } 92 93 96 @PostConstruct 97 public void init() 98 throws ConfigException 99 { 100 Path pwd = Vfs.lookup(); 101 102 if (_fileSet == null) { 103 createFileset().addInclude(new PathPatternType("**/*.jsp")); 104 } 105 106 if (_webApp == null) { 107 _webApp = WebApp.getLocal(); 108 } 109 110 if (_webApp == null) 111 throw new ConfigException(L.l("JspPrecompileResource must be used in a web-app context.")); 112 113 start(); 114 } 115 116 119 public void start() 120 { 121 if (! _lifecycle.toActive()) 122 return; 123 124 126 if (JspFactory.getDefaultFactory() == null) 127 JspFactory.setDefaultFactory(new QJspFactory()); 128 129 JspCompiler compiler = new JspCompiler(); 130 compiler.setWebApp(_webApp); 131 132 ArrayList <Path> paths = _fileSet.getPaths(); 133 ArrayList <String > classes = new ArrayList <String >(); 134 135 String contextPath = _webApp.getContextPath(); 136 if (! contextPath.endsWith("/")) 137 contextPath = contextPath + "/"; 138 139 Path pwd = Vfs.lookup(); 140 141 for (int i = 0; i < paths.size(); i++) { 142 Path path = paths.get(i); 143 144 String uri = path.getPath().substring(pwd.getPath().length()); 145 146 if (_webApp.getContext(contextPath + uri) != _webApp) 147 continue; 148 149 String className = JspCompiler.urlToClassName(uri); 150 151 try { 152 CauchoPage page = (CauchoPage) compiler.loadClass(className, true); 153 154 page.init(pwd); 155 156 if (! page._caucho_isModified()) { 157 log.fine("pre-loaded " + uri); 158 continue; 159 } 160 } catch (ClassNotFoundException e) { 161 } catch (Throwable e) { 162 log.log(Level.FINER, e.toString(), e); 163 } 164 165 log.fine("compiling " + uri); 166 167 try { 168 JspCompilerInstance compilerInst; 169 compilerInst = compiler.getCompilerInstance(path, uri, className); 170 171 JspGenerator generator = compilerInst.generate(); 172 173 if (generator.isStatic()) 174 continue; 175 176 LineMap lineMap = generator.getLineMap(); 177 178 classes.add(className.replace('.', '/') + ".java"); 179 } catch (Exception e) { 180 if (e instanceof CompileException) 181 log.warning(e.getMessage()); 182 else 183 log.log(Level.WARNING, e.toString(), e); 184 185 continue; 186 } 187 } 188 189 if (classes.size() == 0) 190 return; 191 192 try { 193 JavaCompiler javaCompiler = JavaCompiler.create(null); 194 javaCompiler.setClassDir(compiler.getClassDir()); 195 196 String files[] = new String [classes.size()]; 197 classes.toArray(files); 198 199 javaCompiler.compileBatch(files); 200 } catch (Exception e) { 201 if (e instanceof CompileException) 202 log.warning(e.getMessage()); 203 else 204 log.log(Level.WARNING, e.toString(), e); 205 } 206 } 207 } 208 | Popular Tags |