1 29 30 package com.caucho.jsp; 31 32 import com.caucho.java.LineMap; 33 import com.caucho.loader.DynamicClassLoader; 34 import com.caucho.loader.SimpleLoader; 35 import com.caucho.log.Log; 36 import com.caucho.server.connection.CauchoRequest; 37 import com.caucho.server.connection.CauchoResponse; 38 import com.caucho.server.util.CauchoSystem; 39 import com.caucho.server.webapp.WebApp; 40 import com.caucho.util.L10N; 41 import com.caucho.vfs.Depend; 42 import com.caucho.vfs.Path; 43 import com.caucho.vfs.PersistentDependency; 44 45 import javax.servlet.SingleThreadModel ; 46 import javax.servlet.jsp.HttpJspPage ; 47 import javax.servlet.jsp.JspFactory ; 48 import java.io.IOException ; 49 import java.util.ArrayList ; 50 import java.util.logging.Level ; 51 import java.util.logging.Logger ; 52 53 56 public class JspManager extends PageManager { 57 private final static L10N L = new L10N(JspManager.class); 58 private final static Logger log = Log.open(JspManager.class); 59 60 private static int _count; 61 62 private boolean _isXml; 63 private boolean _isLoadTldOnInit; 64 private boolean _precompile = true; 65 66 public JspManager() 67 { 68 if (JspFactory.getDefaultFactory() == null) 69 JspFactory.setDefaultFactory(new QJspFactory()); 70 } 71 72 75 public boolean getPrecompile() 76 { 77 return _precompile; 78 } 79 80 83 public void setXml(boolean isXml) 84 { 85 _isXml = isXml; 86 } 87 88 91 public void setLoadTldOnInit(boolean isLoadOnInit) 92 { 93 _isLoadTldOnInit = isLoadOnInit; 94 } 95 96 99 public void init() 100 { 101 if (_isLoadTldOnInit) { 102 try { 103 WebApp app = getWebApp(); 104 105 TldManager tld = TldManager.create(new AppResourceManager(app), app); 106 107 tld.init(); 109 } catch (Exception e) { 110 log.log(Level.WARNING, e.toString(), e); 111 } 112 } 113 } 114 115 125 Page createGeneratedPage(Path path, String uri, String className, 126 ArrayList <PersistentDependency> dependList) 127 throws Exception 128 { 129 return createPage(path, uri, className, dependList, true); 130 } 131 132 142 Page createPage(Path path, String uri, String className, 143 ArrayList <PersistentDependency> dependList) 144 throws Exception 145 { 146 return createPage(path, uri, className, dependList, false); 147 } 148 149 159 private Page createPage(Path path, String uri, String className, 160 ArrayList <PersistentDependency> dependList, 161 boolean isGenerated) 162 throws Exception 163 { 164 Class jspClass = null; 165 166 Page page = compile(path, uri, className, dependList, isGenerated); 167 168 if (page == null) { 169 return null; 170 } 171 172 174 page = loadPage(page, null); 176 177 boolean alwaysModified = false; 178 if (alwaysModified) 179 page._caucho_setAlwaysModified(); 180 181 return page; 182 } 183 184 protected void initPageManager() 185 { 186 JspCompiler compiler = new JspCompiler(); 187 188 compiler.setWebApp(_webApp); 189 compiler.setXml(_isXml); 190 191 try { 192 compiler.init(); 193 } catch (Exception e) { 194 throw new RuntimeException (e); 195 } 196 } 197 198 Page compile(Path path, String uri, String className, 199 ArrayList <PersistentDependency> dependList, 200 boolean isGenerated) 201 throws Exception 202 { 203 WebApp app = getWebApp(); 204 JspCompiler compiler = new JspCompiler(); 205 206 compiler.setWebApp(_webApp); 207 compiler.setXml(_isXml); 208 209 Page page = null; 210 211 try { 212 if (_precompile || _autoCompile) 213 page = preload(className, app.getClassLoader(), app.getAppDir()); 214 } catch (Throwable e) { 215 log.log(Level.WARNING, e.toString(), e); 216 } 217 218 if (page != null) { 219 if (log.isLoggable(Level.FINE)) 220 log.fine("loading pre-compiled page for " + uri); 221 222 return page; 223 } 224 225 if (path == null || ! path.canRead() || path.isDirectory() || 226 ! _autoCompile) { 227 return null; 228 } 229 230 JspCompilerInstance compilerInst = 231 compiler.getCompilerInstance(path, uri, className); 232 233 compilerInst.setGeneratedSource(isGenerated); 234 compilerInst.addDependList(dependList); 235 236 page = compilerInst.compile(); 237 238 Path classPath = getClassDir().lookup(className.replace('.', '/') + 239 ".class"); 240 241 if (classPath.canRead()) 242 page._caucho_addDepend(new Depend(classPath)); 243 244 return page; 245 } 246 247 void killPage(CauchoRequest request, CauchoResponse response, Page page) 248 { 249 } 250 251 254 Page preload(String className, 255 ClassLoader parentLoader, 256 Path appDir) 257 throws Exception 258 { 259 DynamicClassLoader loader; 260 261 String fullClassName = className; 262 String mangledName = fullClassName.replace('.', '/'); 263 264 Path classPath = getClassDir().lookup(mangledName + ".class"); 265 266 270 271 loader = SimpleLoader.create(parentLoader, getClassDir(), null); 272 273 Class cl = null; 274 275 try { 277 cl = CauchoSystem.loadClass(fullClassName, false, loader); 278 } catch (ClassNotFoundException e) { 279 log.finest(e.toString()); 280 281 return null; 282 } catch (NoClassDefFoundError e) { 283 log.finest(e.toString()); 284 285 return null; 286 } catch (OutOfMemoryError e) { 287 throw e; 288 } catch (Throwable e) { 289 log.log(Level.FINEST, e.toString(), e); 290 291 if (_autoCompile) { 292 try { 293 log.warning("removing generated file " + 294 classPath + " due to " + e.toString()); 295 296 classPath.remove(); 297 } catch (IOException e1) { 298 log.log(Level.FINE, e1.toString(), e1); 299 } 300 } 301 302 return null; 303 } 304 305 HttpJspPage jspPage = (HttpJspPage ) cl.newInstance(); 306 Page page = null; 307 308 if (jspPage instanceof CauchoPage) { 309 CauchoPage cPage = (CauchoPage) jspPage; 310 311 cPage.init(appDir); 312 313 if (cPage instanceof Page) 314 ((Page) cPage)._caucho_setJspManager(this); 315 316 if (_autoCompile && cPage._caucho_isModified()) 317 return null; 318 else if (cPage instanceof Page) 319 page = (Page) cPage; 320 else 321 page = new WrapperPage(jspPage); 322 } 323 else if (jspPage instanceof SingleThreadModel ) 324 page = new SingleThreadWrapperPage((HttpJspPage ) jspPage); 325 else 326 page = new WrapperPage(jspPage); 327 328 page._caucho_addDepend(new Depend(classPath)); 329 330 return page; 331 } 332 333 private Page preloadStatic(String mangledName) 336 { 337 String staticName = mangledName + ".static"; 338 String dependName = mangledName + ".depend"; 339 340 Path staticPath = getClassDir().lookup(staticName); 341 342 if (! staticPath.canRead()) 343 return null; 344 345 Path dependPath = getClassDir().lookup(dependName); 346 347 if (! dependPath.canRead()) 348 return null; 349 350 try { 351 ArrayList <Depend> dependList = StaticPage.parseDepend(dependPath); 352 353 if (dependList == null) 354 return null; 355 356 StaticPage page = new StaticPage(staticPath, true); 357 358 for (int i = 0; i < dependList.size(); i++) { 359 Depend depend = dependList.get(i); 360 361 if (depend.isModified()) 362 return null; 363 364 page._caucho_addDepend(depend); 365 } 366 367 return page; 368 } catch (Throwable e) { 369 log.log(Level.FINER, e.toString(), e); 370 } 371 372 return null; 373 } 374 375 376 382 private Page loadPage(Page page, LineMap lineMap) 383 throws Exception 384 { 385 page.init(_webApp.getAppDir()); 386 387 400 401 page._caucho_setJspManager(this); 402 403 return page; 404 } 405 406 void unload(Page page) 407 { 408 } 409 } 410 | Popular Tags |