1 23 24 package com.sun.enterprise.web; 25 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 import java.util.ResourceBundle ; 29 import java.util.Hashtable ; 30 import java.util.Enumeration ; 31 import java.text.MessageFormat ; 32 import java.lang.instrument.IllegalClassFormatException ; 33 import java.net.URLClassLoader ; 34 import java.net.URL ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import javax.servlet.ServletContext ; 38 import javax.persistence.spi.ClassTransformer; 39 40 import org.apache.catalina.Globals; 41 import org.apache.catalina.Lifecycle; 42 import org.apache.catalina.LifecycleEvent; 43 import org.apache.catalina.LifecycleListener; 44 import org.apache.catalina.LifecycleException; 45 import org.apache.catalina.Wrapper; 46 import org.apache.catalina.loader.WebappClassLoader; 47 import com.sun.enterprise.deployment.runtime.web.SunWebApp; 48 import com.sun.enterprise.deployment.runtime.web.WebProperty; 49 import com.sun.enterprise.deployment.WebBundleDescriptor; 50 import com.sun.enterprise.deployment.Application; 51 import com.sun.enterprise.deployment.util.WebValidatorWithCL; 52 import com.sun.enterprise.deployment.util.WebBundleVisitor; 53 import com.sun.enterprise.util.logging.IASLevel; 54 55 import com.sun.logging.LogDomains; 56 import com.sun.appserv.web.cache.CacheManager; 57 import com.sun.appserv.server.util.ASClassLoaderUtil; 58 import com.sun.appserv.BytecodePreprocessor; 59 import com.sun.enterprise.web.jsp.ResourceInjectorImpl; 60 import com.sun.enterprise.server.PersistenceUnitLoaderImpl; 61 import com.sun.enterprise.server.PersistenceUnitLoader; 62 import com.sun.enterprise.loader.InstrumentableClassLoader; 63 import com.sun.enterprise.config.ConfigException; 64 65 69 70 final class WebModuleListener 71 implements LifecycleListener { 72 73 76 private static Logger _logger; 77 78 81 private static boolean _debugLog; 82 83 88 private String instanceClassPath; 89 90 94 private WebBundleDescriptor wbd; 95 96 100 private String explodedLocation; 101 102 112 public WebModuleListener(String instanceClassPath, 113 String explodedLocation, 114 WebBundleDescriptor wbd) { 115 this.instanceClassPath = instanceClassPath; 116 this.wbd = wbd; 117 this.explodedLocation = explodedLocation; 118 } 119 120 121 125 public void lifecycleEvent(LifecycleEvent event) { 126 127 if (_logger == null) { 128 _logger = LogDomains.getLogger(LogDomains.WEB_LOGGER); 129 _debugLog = _logger.isLoggable(Level.FINE); 130 } 131 132 WebModule webModule; 133 134 try { 136 webModule = (WebModule) event.getLifecycle(); 137 } catch (ClassCastException e) { 138 _logger.log(Level.WARNING, "webmodule.listener.classcastException", 139 event.getLifecycle()); 140 return; 141 } 142 143 if (event.getType().equals(Lifecycle.START_EVENT)) { 145 if (wbd != null && wbd.getApplication() != null && 147 wbd.getApplication().isVirtual()) { 148 wbd.setClassLoader(webModule.getLoader().getClassLoader()); 149 wbd.visit((WebBundleVisitor) new WebValidatorWithCL()); 150 } 151 152 loadPersistenceUnits(webModule); 153 configureDefaultServlet(webModule); 154 configureJspParameters(webModule); 155 startCacheManager(webModule); 156 } else if (event.getType().equals(Lifecycle.STOP_EVENT)) { 157 unloadPersistenceUnits(webModule); 158 stopCacheManager(webModule); 159 } 160 } 161 162 private void loadPersistenceUnits(final WebModule webModule) { 163 _logger.logp(Level.FINE, "WebModuleListener", "loadPersistenceUnits", 164 "wbd = {0} for {1}", new Object []{wbd, webModule.getName()}); 165 if(wbd == null) { 166 return; 168 } 169 final Application application = wbd.getApplication(); 170 if(application != null && application.isVirtual()) { 173 try{ 174 new PersistenceUnitLoaderImpl().load(new ApplicationInfoImpl( 175 explodedLocation, wbd, webModule)); 176 } catch(Exception e){ 177 throw new RuntimeException (e); 178 } 179 } 180 } 181 182 private boolean unloadPersistenceUnits(final WebModule webModule) { 183 _logger.logp(Level.FINE, "WebModuleListener", "unloadPersistenceUnits", 184 "wbd = {0} for {1}", new Object []{wbd, webModule.getName()}); 185 if(wbd == null) { 186 return true; 188 } 189 final Application application = wbd.getApplication(); 190 if(application != null && application.isVirtual()) { 193 try{ 194 new PersistenceUnitLoaderImpl().unload(new ApplicationInfoImpl( 195 explodedLocation, wbd, webModule)); 196 } catch(Exception e){ 197 _logger.log(Level.WARNING, e.getMessage(), e); 198 return false; 199 } 200 } 201 return true; 202 } 203 204 207 private static class ApplicationInfoImpl 208 implements PersistenceUnitLoader.ApplicationInfo { 209 private WebBundleDescriptor wbd; 210 private String location; 211 private InstrumentableClassLoader classLoader; 212 public ApplicationInfoImpl(String location, WebBundleDescriptor wbd, WebModule wm) { 213 this.wbd = wbd; 214 this.location = location; 215 this.classLoader = new InstrumentableWebappClassLoader( 216 WebappClassLoader.class.cast(wm.getLoader().getClassLoader())); 217 } 218 219 public Application getApplication() { 220 return wbd.getApplication(); 221 } 222 223 public InstrumentableClassLoader getClassLoader() { 224 return classLoader; 225 } 226 227 public String getApplicationLocation() { 228 return location; 229 } 230 231 } 233 239 private static final class InstrumentableWebappClassLoader extends ClassLoader 240 implements InstrumentableClassLoader { 241 242 private final WebappClassLoader webappClassLoader; 244 245 public InstrumentableWebappClassLoader(WebappClassLoader webappClassLoader) { 246 super(webappClassLoader.getParent()); 248 this.webappClassLoader = webappClassLoader; 249 } 250 251 253 public ClassLoader copy() { 254 _logger.entering("WebModuleListener$InstrumentableWebappClassLoader", "copy"); 255 return new URLClassLoader (webappClassLoader.getURLs(), getParent()); 257 } 258 259 public void addTransformer(final ClassTransformer transformer) { 260 webappClassLoader.addByteCodePreprocessor(new BytecodePreprocessor(){ 261 265 266 public boolean initialize(Hashtable parameters) { 267 return true; 268 } 269 270 public byte[] preprocess(String resourceName, byte[] classBytes) { 271 try { 272 String classname = resourceName.substring(0, 274 resourceName.length() - 6); byte[] newBytes = transformer.transform( 276 webappClassLoader, classname, null, null, classBytes); 277 return newBytes == null ? classBytes : newBytes; 281 } catch (IllegalClassFormatException e) { 282 _logger.logp(Level.WARNING, 283 "WebModuleListener$InstrumentableClassLoader$BytecodePreprocessor", 284 "preprocess", e.getMessage()); 285 throw new RuntimeException (e); 286 } 287 } 288 }); 289 } 290 291 294 @Override public Class <?> loadClass(String name) throws ClassNotFoundException { 295 return webappClassLoader.loadClass(name); 296 } 297 298 @Override public URL getResource(String name) { 299 return webappClassLoader.getResource(name); 300 } 301 302 @Override public Enumeration <URL > getResources(String name) 303 throws IOException { 304 return webappClassLoader.getResources(name); 305 } 306 307 @Override public InputStream getResourceAsStream(String name) { 308 return webappClassLoader.getResourceAsStream(name); 309 } 310 311 @Override public synchronized void setDefaultAssertionStatus(boolean enabled) { 312 webappClassLoader.setDefaultAssertionStatus(enabled); 313 } 314 315 @Override public synchronized void setPackageAssertionStatus( 316 String packageName, boolean enabled) { 317 webappClassLoader.setPackageAssertionStatus(packageName, enabled); 318 } 319 320 @Override public synchronized void setClassAssertionStatus( 321 String className, boolean enabled) { 322 webappClassLoader.setClassAssertionStatus(className, enabled); 323 } 324 325 @Override public synchronized void clearAssertionStatus() { 326 webappClassLoader.clearAssertionStatus(); 327 } 328 329 @Override public String toString() { 330 StringBuilder sb = new StringBuilder ("InstrumentableWebappClassLoader\r\n"); 331 sb.append(" Parent -> "); 332 sb.append(webappClassLoader); 333 return sb.toString(); 334 } 335 336 } 338 340 344 private void configureJspParameters(WebModule webModule) { 345 346 SunWebApp bean = webModule.getIasWebAppConfigBean(); 347 348 String name = webModule.findServletMapping(Constants.JSP_URL_PATTERN); 350 Wrapper wrapper = (Wrapper)webModule.findChild(name); 351 if (wrapper == null) 352 return; 353 354 String servletClass = wrapper.getServletClass(); 355 if (servletClass != null 358 && servletClass.equals(Constants.APACHE_JSP_SERVLET_CLASS)) { 359 360 if (webModule.getTldValidation()) { 361 wrapper.addInitParameter("enableTldValidation", "true"); 362 } 363 if (bean != null && bean.getJspConfig() != null) { 364 WebProperty[] props = bean.getJspConfig().getWebProperty(); 365 for (int i = 0; i < props.length; i++) { 366 String pname = props[i].getAttributeValue("name"); 367 String pvalue = props[i].getAttributeValue("value"); 368 if (_debugLog) { 369 _logger.fine("jsp-config property for [" 370 + webModule.getID() + "] is [" + pname 371 + "] = [" + pvalue + "]"); 372 } 373 wrapper.addInitParameter(pname, pvalue); 374 } 375 } 376 377 wrapper.addInitParameter("logVerbosityLevel",getJasperLogLevel()); 379 380 wrapper.addInitParameter("com.sun.appserv.jsp.resource.injector", 381 ResourceInjectorImpl.class.getName()); 382 383 String sysClassPath = ASClassLoaderUtil.getWebModuleClassPath(webModule.getID()); 385 if (_logger.isLoggable(Level.FINE)) { 386 _logger.fine(" sysClasspath for " + webModule.getID() + " is \n" 387 + sysClassPath + "\n"); 388 } 389 if (instanceClassPath != null 390 && instanceClassPath.length() > 0) { 391 sysClassPath += instanceClassPath; 392 } 393 wrapper.addInitParameter("com.sun.appserv.jsp.classpath", 394 sysClassPath); 395 } 397 } 398 399 403 private String getJasperLogLevel() { 404 Level level = _logger.getLevel(); 405 if (level.equals((Level )IASLevel.FATAL)) 406 return "fatal"; 407 else if (level.equals(Level.WARNING)) 408 return "warning"; 409 else if (level.equals(Level.FINE)) 410 return "information"; 411 else if (level.equals(Level.FINER) || level.equals(Level.FINEST)) 412 return "debug"; 413 else 414 return "warning"; 415 } 416 417 private void startCacheManager(WebModule webModule) { 418 419 SunWebApp bean = webModule.getIasWebAppConfigBean(); 420 421 if (bean != null) { 423 CacheManager cm = null; 424 try { 425 cm = CacheModule.configureResponseCache(webModule, bean); 426 } catch (Exception ee) { 427 _logger.log(Level.WARNING, 428 "webmodule.listener.cachemgrException", ee); 429 } 430 431 if (cm != null) { 432 try { 433 cm.start(); 435 if (_debugLog) { 436 _logger.fine("Cache Manager started"); 437 } 438 ServletContext ctxt = webModule.getServletContext(); 441 ctxt.setAttribute(CacheManager.CACHE_MANAGER_ATTR_NAME, cm); 442 443 } catch (LifecycleException ee) { 444 _logger.log(Level.WARNING, ee.getMessage(), 445 ee.getThrowable()); 446 } 447 } 448 } 449 } 450 451 private void stopCacheManager(WebModule webModule) { 452 ServletContext ctxt = webModule.getServletContext(); 453 CacheManager cm = (CacheManager)ctxt.getAttribute( 454 CacheManager.CACHE_MANAGER_ATTR_NAME); 455 if (cm != null) { 456 try { 457 cm.stop(); 458 if (_debugLog) { 459 _logger.fine("Cache Manager stopped"); 460 } 461 ctxt.removeAttribute(CacheManager.CACHE_MANAGER_ATTR_NAME); 462 } catch (LifecycleException ee) { 463 _logger.log(Level.WARNING, ee.getMessage(), ee.getThrowable()); 464 } 465 } 466 } 467 468 469 473 private void configureDefaultServlet(WebModule webModule) { 474 475 Wrapper wrapper = (Wrapper)webModule.findChild("default"); 477 if (wrapper == null) { 478 return; 479 } 480 481 String servletClass = wrapper.getServletClass(); 482 if (servletClass == null 483 || !servletClass.equals(Globals.DEFAULT_SERVLET_CLASS_NAME)) { 484 return; 485 } 486 487 String fileEncoding = webModule.getFileEncoding(); 488 if (fileEncoding != null) { 489 wrapper.addInitParameter("fileEncoding", fileEncoding); 490 } 491 } 492 } 493 | Popular Tags |