1 23 24 package com.sun.enterprise.server; 25 26 import com.sun.enterprise.admin.event.AdminEventListenerException; 27 import com.sun.enterprise.admin.event.AdminEventListenerRegistry; 28 import com.sun.enterprise.admin.event.DeployEventListenerHelper; 29 import com.sun.enterprise.admin.event.ModuleDeployEvent; 30 import com.sun.enterprise.admin.event.ModuleDeployEventListener; 31 32 import com.sun.enterprise.admin.common.MBeanServerFactory; 33 import javax.management.MBeanServer ; 34 import javax.management.ObjectName ; 35 36 import com.sun.enterprise.config.ConfigBean; 37 import com.sun.enterprise.config.ConfigContext; 38 import com.sun.enterprise.config.ConfigException; 39 import com.sun.enterprise.config.serverbeans.ApplicationHelper; 40 import com.sun.enterprise.config.serverbeans.ApplicationRef; 41 import com.sun.enterprise.config.serverbeans.ApplicationRef; 42 import com.sun.enterprise.config.serverbeans.Server; 43 import com.sun.enterprise.config.serverbeans.Server; 44 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 45 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 46 import com.sun.enterprise.config.serverbeans.WebModule; 47 import com.sun.enterprise.deployment.Application; 48 import com.sun.enterprise.deployment.WebBundleDescriptor; 49 import com.sun.enterprise.deployment.WebServiceEndpoint; 50 import com.sun.enterprise.deployment.WebServicesDescriptor; 51 import com.sun.enterprise.instance.WebModulesManager; 52 import com.sun.enterprise.security.acl.RoleMapper; 53 import com.sun.enterprise.Switch; 54 import com.sun.enterprise.util.i18n.StringManager; 55 import com.sun.enterprise.util.StringUtils; 56 import com.sun.enterprise.web.VirtualServer; 57 import com.sun.enterprise.web.WebContainer; 58 import com.sun.enterprise.web.WebModuleConfig; 59 import com.sun.logging.LogDomains; 60 61 import java.io.File ; 62 import java.util.List ; 63 import java.util.Vector ; 64 import java.util.Iterator ; 65 import java.util.Hashtable ; 66 import java.util.logging.Logger ; 67 import java.util.logging.Level ; 68 69 import org.apache.catalina.Container; 70 import org.apache.catalina.Context; 71 import org.apache.catalina.Deployer; 72 import org.apache.catalina.Engine; 73 import org.apache.catalina.Host; 74 import org.apache.catalina.core.StandardHost; 75 76 82 83 public class WebModuleDeployEventListener extends DummyWebModuleManager 84 implements ModuleDeployEventListener { 85 86 87 88 static Logger _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 89 90 91 private static StringManager localStrings = 92 StringManager.getManager(WebModuleDeployEventListener.class); 93 94 private Hashtable cachedWebModules = new Hashtable (); 95 96 97 100 public WebModuleDeployEventListener(WebModulesManager manager, ClassLoader loader) { 101 super(manager, loader); 102 AdminEventListenerRegistry.addModuleDeployEventListener(this); 103 } 104 105 106 108 109 112 protected WebContainer webContainer = null; 113 114 115 117 118 126 private WebModuleConfig loadWebModuleConfig(WebModule wm) { 127 128 WebModuleConfig wmInfo = new WebModuleConfig(); 129 wmInfo.setBean(wm); 130 String wmID = wm.getName(); 131 String location = wm.getLocation(); 132 try { 133 Application app = getWebModulesManager().getDescriptor(wmID, 134 location); 135 WebBundleDescriptor wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor(); 136 wmInfo.setDescriptor(wbd); 137 String vs = ServerBeansFactory.getVirtualServersByAppName( 138 wm.getConfigContext(), wmID); 139 wmInfo.setVirtualServers(vs); 140 } catch (ConfigException ce) { 141 wmInfo = null; 142 } 143 return wmInfo; 144 145 } 146 147 private void moduleDeployed(ConfigContext config, String moduleName) 148 throws AdminEventListenerException { 149 150 WebModule webModule = (WebModule)cachedWebModules.get(moduleName); 151 if ( webModule == null ){ 152 webModule = getWebModule(config, moduleName); 153 cachedWebModules.put(moduleName, webModule); 154 } 155 156 if (!isEnabled(config, moduleName)) { 158 return; 159 } 160 String location = webModule.getLocation(); 161 File moduleBase = new File (location); 165 String modulesRoot = getWebContainer().getModulesRoot(); 166 if (!moduleBase.isAbsolute()) { 167 location = modulesRoot+File.separator+location; 168 webModule.setLocation(location); 169 } 170 WebModuleConfig wmInfo = loadWebModuleConfig(webModule); 171 List <Throwable > throwables = 172 getWebContainer().loadWebModule(wmInfo, "null"); 173 if (throwables != null && !throwables.isEmpty()) { 174 String msg = throwables.get(0).getMessage(); 177 AdminEventListenerException ex = 178 new AdminEventListenerException(msg); 179 ex.initCause(throwables.get(0)); 180 throw ex; 181 } 182 } 183 184 185 private void moduleUndeployed(ConfigContext config, String moduleName) 186 throws AdminEventListenerException { 187 188 WebModule webModule = (WebModule)cachedWebModules.get(moduleName); 189 if ( webModule == null ){ 190 webModule = getWebModule(config, moduleName); 191 } else { 192 cachedWebModules.remove(moduleName); 193 } 194 195 String appName = null; 196 WebBundleDescriptor wbd; 197 try { 198 Application app = getWebModulesManager().getDescriptor( 199 webModule.getName(), webModule.getLocation()); 200 RoleMapper.removeRoleMapper(app.getRoleMapper().getName()); 201 wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor(); 202 appName = app.getRegistrationName(); 203 } catch (ConfigException ce) { 204 throw new AdminEventListenerException(ce); 205 } 206 String contextRoot = webModule.getContextRoot(); 207 String virtualServers = null; 208 try { 209 virtualServers = ServerBeansFactory 210 .getVirtualServersByAppName(config,moduleName); 211 } catch(ConfigException ce) { 212 if (_logger.isLoggable(Level.FINEST)) { 213 _logger.log(Level.FINEST, 214 "Exception getting virtual servers by app name " 215 + moduleName, ce); 216 } 217 } 218 try { 219 getWebContainer().unloadWebModule(contextRoot, appName, 220 virtualServers, wbd); 221 } finally { 222 try { 223 Switch.getSwitch().getNamingManager().unbindObjects(wbd); 224 } catch (javax.naming.NamingException nameEx) { 225 _logger.log(Level.FINEST, "[WebModuleDeployEventListener] " 226 + " Exception during namingManager.unbindObject", 227 nameEx); 228 } 229 } 230 } 231 232 234 235 237 238 246 public synchronized void moduleDeployed(ModuleDeployEvent event) 247 throws AdminEventListenerException { 248 249 if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 250 if (_logger.isLoggable(Level.FINEST)) { 251 _logger.log(Level.FINEST, 252 "[WebModuleDeployEventListener] Handling event " + event.toString()); 253 } 254 255 DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event); 256 257 ConfigContext config = event.getConfigContext(); 258 String moduleName = event.getModuleName(); 259 260 try { 262 getWebModulesManager().refreshConfigContext(config); 263 } catch (ConfigException ce) { 264 throw new AdminEventListenerException(ce.getMessage()); 265 } 266 267 setEnable( moduleName, false); 271 AbstractLoader dummyLoader = getLoader(moduleName); 272 dummyLoader.load(false); 273 setEnable( moduleName, true); 274 275 moduleDeployed(config, moduleName); 276 } 277 278 } 279 280 private void setEnable(String moduleName, boolean flag) { 281 try { 282 ConfigContext ctx = ApplicationServer.getServerContext().getConfigContext(); 283 ConfigBean app = ApplicationHelper.findApplication(ctx, moduleName); 284 app.setEnabled(flag); 285 Server server = ServerBeansFactory.getServerBean(ctx); 286 ApplicationRef appRef = server.getApplicationRefByRef(moduleName); 287 appRef.setEnabled(flag); 288 } catch (ConfigException ce) { 289 if (_logger.isLoggable(Level.FINER)) { 290 _logger.log(Level.FINER, 291 "[WebModuleDeployEventListener] setEnable failed for " + moduleName); 292 } 293 } 294 } 295 296 297 305 public synchronized void moduleUndeployed(ModuleDeployEvent event) 306 throws AdminEventListenerException { 307 308 if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 309 if (_logger.isLoggable(Level.FINEST)) { 310 _logger.log(Level.FINEST, 311 "[WebModuleDeployEventListener] Handling event " + event.toString()); 312 } 313 ConfigContext config = event.getOldConfigContext(); 314 String moduleName = event.getModuleName(); 315 316 try { 318 getWebModulesManager().refreshConfigContext(config); 319 } catch (ConfigException ce) { 320 throw new AdminEventListenerException(ce.getMessage()); 321 } 322 323 moduleUndeployed(config, moduleName); 324 } 325 326 } 327 328 329 337 public synchronized void moduleRedeployed(ModuleDeployEvent event) 338 throws AdminEventListenerException { 339 340 if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 341 if (_logger.isLoggable(Level.FINEST)) { 342 _logger.log(Level.FINEST, 343 "[WebModuleDeployEventListener] Handling event " + event.toString()); 344 } 345 String moduleName = event.getModuleName(); 346 347 ConfigContext oldConfig = event.getOldConfigContext(); 349 moduleUndeployed(oldConfig, moduleName); 350 351 ConfigContext config = event.getConfigContext(); 353 moduleDeployed(config, moduleName); 354 } 355 356 } 357 358 359 367 public synchronized void moduleEnabled(ModuleDeployEvent event) 368 throws AdminEventListenerException { 369 370 if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 371 if (_logger.isLoggable(Level.FINEST)) { 372 _logger.log(Level.FINEST, 373 "[WebModuleDeployEventListener] Handling event " + event.toString()); 374 } 375 ConfigContext config = event.getConfigContext(); 376 String moduleName = event.getModuleName(); 377 WebModule webModule = (WebModule)cachedWebModules.get(moduleName); 378 if ( webModule == null ){ 379 webModule = getWebModule(config, moduleName); 380 cachedWebModules.put(moduleName, webModule); 381 } 382 383 if (!isEnabled(event.getConfigContext(), moduleName)) { 385 return; 386 } 387 388 String location = webModule.getLocation(); 389 File moduleBase = new File (location); 393 String modulesRoot = getWebContainer().getModulesRoot(); 394 if (!moduleBase.isAbsolute()) { 395 location = modulesRoot+File.separator+location; 396 webModule.setLocation(location); 397 } 398 WebModuleConfig wmInfo = loadWebModuleConfig(webModule); 399 getWebContainer().enableWebModule(wmInfo, "null"); 400 } 401 402 } 403 404 405 413 public synchronized void moduleDisabled(ModuleDeployEvent event) 414 throws AdminEventListenerException { 415 416 if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 417 if (_logger.isLoggable(Level.FINEST)) { 418 _logger.log(Level.FINEST, 419 "[WebModuleDeployEventListener] Handling event " + event.toString()); 420 } 421 ConfigContext config = event.getConfigContext(); 422 String moduleName = event.getModuleName(); 423 WebModule webModule = (WebModule)cachedWebModules.get(moduleName); 424 if ( webModule == null ){ 425 webModule = getWebModule(config, moduleName); 426 } 427 428 String appName = null; 429 WebBundleDescriptor wbd; 430 try { 431 Application app = getWebModulesManager().getDescriptor( 432 webModule.getName(), webModule.getLocation()); 433 wbd = 434 (WebBundleDescriptor) app.getStandaloneBundleDescriptor(); 435 appName = app.getRegistrationName(); 436 } catch (ConfigException ce) { 437 throw new AdminEventListenerException(ce); 438 } 439 String contextRoot = webModule.getContextRoot(); 440 String virtualServers = null; 441 try { 442 virtualServers = ServerBeansFactory 443 .getVirtualServersByAppName(config,moduleName); 444 } catch(ConfigException ce) { 445 if (_logger.isLoggable(Level.FINEST)) { 446 _logger.log(Level.FINEST, 447 "Exception getting virtual servers by app name " 448 + moduleName, ce); 449 } 450 } 451 getWebContainer().disableWebModule(contextRoot,appName, 452 virtualServers); 453 } 454 455 } 456 457 private WebModule getWebModule(ConfigContext config, String moduleName) 458 throws AdminEventListenerException { 459 WebModule webModule = null; 460 try { 461 webModule = (WebModule) ApplicationHelper.findApplication( 462 config, moduleName); 463 } catch (ConfigException ce) { 464 throw new AdminEventListenerException(ce); 465 } 466 467 if (webModule == null) { 468 String msg = localStrings.getString( 469 "webmodule.module_not_found", 470 moduleName); 471 throw new AdminEventListenerException(msg); 472 } 473 return webModule; 474 } 475 476 483 public void moduleReferenceAdded(ModuleDeployEvent event) 484 throws AdminEventListenerException { 485 } 486 487 494 public void moduleReferenceRemoved(ModuleDeployEvent event) 495 throws AdminEventListenerException { 496 } 497 498 499 501 510 protected boolean isEnabled(ConfigContext config, String moduleName) { 511 try { 512 ConfigBean app = ApplicationHelper.findApplication(config, moduleName); 513 514 Server server = ServerBeansFactory.getServerBean(config); 515 ApplicationRef appRef = server.getApplicationRefByRef(moduleName); 516 517 return ((app != null && app.isEnabled()) && 518 (appRef != null && appRef.isEnabled())); 519 } catch (ConfigException e) { 520 AdminEventListenerException ex = new AdminEventListenerException(); 521 ex.initCause(e); 522 _logger.log(Level.FINE, "Error in finding " + moduleName, e); 523 524 return false; 526 } 527 } 528 529 private WebModulesManager getWebModulesManager() throws ConfigException { 530 return (WebModulesManager) this.configManager; 531 } 532 533 private WebContainer getWebContainer() { 534 if (webContainer == null) { 535 this.webContainer = WebContainer.getInstance(); 536 } 537 return this.webContainer; 538 } 539 540 } 541 | Popular Tags |