1 23 24 37 package com.sun.enterprise.server; 38 39 import com.sun.enterprise.appclient.jws.AppclientJWSSupportManager; 40 import java.io.File ; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 import java.net.URL ; 44 import java.net.MalformedURLException ; 45 46 import com.sun.enterprise.instance.AppsManager; 47 import com.sun.enterprise.config.ConfigException; 48 49 import com.sun.enterprise.admin.event.ApplicationDeployEvent; 50 import com.sun.enterprise.admin.event.ApplicationDeployEventListener; 51 import com.sun.enterprise.admin.event.AdminEventListenerRegistry; 52 import com.sun.enterprise.admin.event.AdminEventListenerException; 53 import com.sun.enterprise.admin.event.DeployEventListenerHelper; 54 55 import com.sun.enterprise.instance.InstanceEnvironment; 56 import com.sun.enterprise.security.acl.RoleMapper; 57 import com.sun.enterprise.server.pluggable.ApplicationLoaderFactory; 58 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory; 59 60 import com.sun.enterprise.deployment.Application; 61 62 import com.sun.enterprise.util.i18n.StringManager; 63 import com.sun.enterprise.deployment.backend.Deployer; 64 import com.sun.enterprise.deployment.backend.DeployerFactory; 65 import com.sun.enterprise.deployment.backend.DeploymentRequest; 66 import com.sun.enterprise.deployment.backend.DeploymentCommand; 67 import com.sun.enterprise.deployment.backend.DeployableObjectType; 68 import com.sun.enterprise.deployment.backend.IASDeploymentException; 69 import com.sun.enterprise.deployment.autodeploy.AutoDirReDeployer; 70 import com.sun.enterprise.security.SecurityUtil; 71 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapper; 72 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactory; 73 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactoryMgr; 74 import com.sun.enterprise.security.util.IASSecurityException; 75 76 import com.sun.enterprise.config.ConfigContext; 77 78 import java.util.logging.Logger ; 79 import java.util.logging.Level ; 80 import com.sun.logging.LogDomains; 81 82 import com.sun.enterprise.deployment.io.ApplicationDeploymentDescriptorFile; 84 import com.sun.enterprise.deployment.node.J2EEDocumentBuilder; 85 import com.sun.enterprise.admin.event.BaseDeployEvent; 86 import java.util.Iterator ; 87 import com.sun.enterprise.deployment.EjbBundleDescriptor; 88 import javax.management.MBeanException ; 89 import com.sun.enterprise.management.StateManageable; 91 92 93 102 public class ApplicationManager extends AbstractManager 103 implements ApplicationDeployEventListener { 104 105 106 static Logger _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 107 108 109 private static StringManager localStrings = 110 StringManager.getManager(ApplicationManager.class); 111 112 private static final String RAR = "rar"; 113 114 128 ApplicationManager(AppsManager appsManager, 129 ClassLoader connectorClassLoader) { 130 131 super(connectorClassLoader, appsManager); 132 133 AdminEventListenerRegistry.addApplicationDeployEventListener(this); 134 135 136 AppclientJWSSupportManager.getInstance(); 137 } 138 139 146 protected AbstractLoader getLoader(String id) { 147 PluggableFeatureFactory featureFactory = 149 ApplicationServer.getServerContext().getPluggableFeatureFactory(); 150 ApplicationLoaderFactory appLoaderFactory = 151 featureFactory.getApplicationLoaderFactory(); 152 ApplicationLoader appLoader = 153 appLoaderFactory.createApplicationLoader(id, 154 this.parentClassLoader,(AppsManager)this.configManager); 155 _logger.log(Level.FINEST,"ApplicationLoader "+appLoader); 156 return appLoader; 157 } 158 159 168 boolean applicationDeployed(String appID) { 169 return applicationDeployed(false, appID, true); 170 } 171 172 182 boolean applicationDeployed(boolean jsr77, String appID) { 183 return applicationDeployed(jsr77, appID, true); 184 } 185 186 boolean applicationDeployed(boolean jsr77, String appID, 187 ConfigContext dynamicConfigContext) 188 { 189 return applicationDeployed(jsr77, appID, true, 190 dynamicConfigContext); 191 } 192 193 boolean applicationDeployed(boolean jsr77, String appID, 194 ConfigContext dynamicConfigContext, int loadUnloadAction) 195 { 196 return applicationDeployed(jsr77, appID, true, 197 dynamicConfigContext, loadUnloadAction); 198 } 199 200 209 boolean applicationDeployed(boolean jsr77, String appID, boolean addToRM) { 210 return applicationDeployed(jsr77, appID, addToRM, null); 211 } 212 213 boolean applicationDeployed(boolean jsr77, String appID, boolean addToRM, 214 ConfigContext dynamicConfigContext) 215 { 216 return applicationDeployed(jsr77, appID, addToRM, dynamicConfigContext, 217 Constants.LOAD_ALL); 218 } 219 220 221 boolean applicationDeployed(boolean jsr77, String appID, boolean addToRM, 226 ConfigContext dynamicConfigContext, int loadUnloadAction) 227 { 228 229 boolean deployed = false; 230 boolean loadJSR77 = jsr77 || loadJSR77(appID, DeployableObjectType.APP); 231 AbstractLoader appLoader = null; 232 try { 233 if (this.configManager.isSystem(appID)) { 234 return true; 235 } 236 } catch (ConfigException confEx) { 237 _logger.log(Level.WARNING, 238 "core.error_while_loading_app", confEx); 239 } 240 241 try { 242 if (loadUnloadAction == Constants.LOAD_UNSET) { 244 loadUnloadAction = Constants.LOAD_ALL; 245 } 246 247 boolean handlePreLoadAction = 248 (loadUnloadAction == Constants.LOAD_ALL || 249 loadUnloadAction == Constants.LOAD_RAR); 250 251 boolean handlePostLoadAction = 252 (loadUnloadAction == Constants.LOAD_ALL || 253 loadUnloadAction == Constants.LOAD_REST); 254 255 if (handlePreLoadAction) { 257 if (addToRM) { 258 addToReloadMonitor(appID); 259 } 260 } 261 262 if (id2loader.get(appID) != null) { 265 return true; 266 } 267 268 if (handlePreLoadAction) { 273 appLoader = getLoader(appID); 274 278 if (loadUnloadAction == Constants.LOAD_ALL) { 279 id2loader.put(appID, appLoader); 280 } else if (loadUnloadAction == Constants.LOAD_RAR) { 281 id2loader.put(appID + RAR, appLoader); 282 } 283 } else if (loadUnloadAction == Constants.LOAD_REST) { 284 appLoader = (AbstractLoader) id2loader.remove(appID + RAR); 285 if (appLoader != null) { 286 id2loader.put(appID, appLoader); 287 } else { 288 return false; 289 } 290 } 291 292 appLoader.setConfigContext(dynamicConfigContext); 293 appLoader.setLoadUnloadAction(loadUnloadAction); 294 295 if (handlePreLoadAction && loadJSR77) { 298 try { 299 appLoader.createRootMBean(); 300 } catch (MBeanException mbe) { 301 _logger.log(Level.WARNING,"core.error_while_creating_jsr77_root_mbean",mbe); 302 } 303 } 304 305 if (isEnabled(dynamicConfigContext, appID)) { 306 307 _logger.log(Level.FINEST, 308 "[ApplicationManager] Application is enabled: " + appID); 309 310 311 if (handlePreLoadAction) { 313 try { 315 appLoader.setState(StateManageable.STARTING_STATE); 316 } catch (MBeanException mbe) { 317 _logger.log(Level.WARNING, 318 "core.error_while_setting_jsr77_state",mbe); 319 } 320 } 321 322 boolean retSts = appLoader.load(jsr77); 323 deployed = retSts; 324 325 if (handlePostLoadAction && retSts) { 326 327 try { 329 appLoader.setState(StateManageable.RUNNING_STATE); 330 } catch (MBeanException mbe) { 331 _logger.log(Level.WARNING, 332 "core.error_while_setting_jsr77_state",mbe); 333 } 334 335 336 deployed = true; 338 339 SecurityRoleMapperFactory factory = SecurityRoleMapperFactoryMgr.getFactory(); 340 341 if (factory==null) { 342 throw new IllegalArgumentException (localStrings.getString( 343 "enterprise.deployment.deployment.norolemapperfactorydefine", 344 "This application has no role mapper factory defined")); 345 } 346 347 } else if (!retSts) { 348 if (handlePreLoadAction && loadJSR77) { 349 try { 351 appLoader.deleteLeafAndRootMBeans(); 352 } catch (MBeanException mbe) { 353 _logger.log(Level.WARNING, 354 "core.error_while_deleting_jsr77_leaf_and_root_mbean",mbe); 355 } 356 } else if (!loadJSR77){ 357 try { 359 appLoader.setState(StateManageable.FAILED_STATE); 360 } catch (MBeanException mbe) { 361 _logger.log(Level.WARNING, 362 "core.error_while_setting_jsr77_state",mbe); 363 } 364 } 365 369 try { 370 appLoader.done(); 371 } catch (Throwable thr) { 372 String msg = localStrings.getString("core.application_not_loaded", appID); 373 _logger.log(Level.INFO, msg, thr); 374 } 375 _logger.log(Level.WARNING, 376 "core.application_not_loaded", appID); 377 } 378 379 } else { 380 _logger.log(Level.INFO, "core.application_disabled", appID); 381 deployed = false; 382 } 383 } catch (ConfigException confEx) { 384 _logger.log(Level.WARNING, "core.error_while_loading_app", confEx); 385 } finally { 386 if (appLoader != null) { 388 appLoader.setConfigContext(null); 389 } 390 } 391 392 return deployed; 393 } 394 395 396 404 boolean applicationUndeployed(String appID) { 405 return applicationUndeployed(false, appID, true); 406 } 407 408 417 boolean applicationUndeployed(boolean jsr77, String appID) { 418 return applicationUndeployed(jsr77, appID, true); 419 } 420 421 430 boolean applicationUndeployed(boolean jsr77, String appID, boolean clearRM) { 431 return applicationUndeployed(jsr77, appID,clearRM, false); 432 } 433 434 435 boolean applicationUndeployed(boolean jsr77, String appID, boolean clearRM, boolean cascade) { 436 437 return applicationUndeployed(jsr77, appID,clearRM, cascade, Constants.UNLOAD_ALL); 438 } 439 440 boolean applicationUndeployed(boolean jsr77, String appID, boolean clearRM, boolean cascade, int loadUnloadAction) { 445 446 if (loadUnloadAction == Constants.LOAD_UNSET) { 448 loadUnloadAction = Constants.UNLOAD_ALL; 449 } 450 451 boolean handlePreUnloadAction = 452 (loadUnloadAction == Constants.UNLOAD_ALL || 453 loadUnloadAction == Constants.UNLOAD_REST); 454 455 boolean handlePostUnloadAction = 456 (loadUnloadAction == Constants.UNLOAD_ALL || 457 loadUnloadAction == Constants.UNLOAD_RAR); 458 459 if (handlePreUnloadAction && clearRM) { 461 removeFromReloadMonitor(appID); 462 } 463 464 AbstractLoader appLoader = (AbstractLoader) id2loader.get(appID); 465 466 if (handlePostUnloadAction) { 467 appLoader = (AbstractLoader) id2loader.remove(appID); 468 } 469 470 if ((appLoader == null) || 472 (appLoader != null && appLoader.getApplication() == null)) { 473 return true; 474 } 475 476 appLoader.setCascade(cascade); 477 appLoader.setLoadUnloadAction(loadUnloadAction); 478 Application app = appLoader.getApplication(); 479 480 if (handlePreUnloadAction) { 481 try { 483 appLoader.setState(StateManageable.STOPPING_STATE); 484 } catch (MBeanException mbe) { 485 _logger.log(Level.WARNING, 486 "core.error_while_setting_jsr77_state",mbe); 487 } 488 489 RoleMapper.removeRoleMapper(app.getRoleMapper().getName()); 490 491 if (jsr77) { 492 try { 494 appLoader.deleteLeafAndRootMBeans(); 495 } catch (MBeanException mbe) { 496 _logger.log(Level.WARNING, 497 "core.error_while_deleting_jsr77_leaf_and_root_mbeans",mbe); 498 } 499 } else { 500 try { 502 appLoader.setState(StateManageable.STOPPED_STATE); 503 } catch (MBeanException mbe) { 504 _logger.log(Level.WARNING, 505 "core.error_while_setting_jsr77_state",mbe); 506 } 507 } 508 509 } 510 511 boolean undeployed = appLoader.unload(jsr77); 512 513 if (loadUnloadAction == Constants.UNLOAD_REST) { 514 return undeployed; 515 } 516 517 if (undeployed) { 518 if (app.getEjbComponentCount() > 0) { 522 _logger.log(Level.INFO, 523 "core.application_unloaded_ejb", appID); 524 } 525 } else { 526 _logger.log(Level.INFO, 527 "core.application_not_unloaded", appID); 528 } 529 530 return undeployed; 531 } 532 533 534 private void holdRequest() { } 535 536 private void holdRequest(String appID) { } 537 538 540 550 public synchronized boolean reload(MonitorableEntry entry) { 551 552 String appName = entry.getId(); 553 boolean status = false; 554 555 try { 556 DeploymentRequest req = new DeploymentRequest( 557 this.configManager.getInstanceEnvironment(), 558 DeployableObjectType.APP, 559 DeploymentCommand.DEPLOY); 560 561 req.setFileSource(entry.getMonitoredFile().getParentFile()); 563 564 req.setName(appName); 566 567 req.setForced(true); 569 570 AutoDirReDeployer deployer = new AutoDirReDeployer(req); 571 status = deployer.redeploy(); 572 573 } 574 catch (IASDeploymentException de) 575 { 576 _logger.log(Level.WARNING,"core.error_while_redeploying_app",de); 577 return false; 578 } 579 return status; 580 } 581 583 584 586 public synchronized void applicationDeployed(ApplicationDeployEvent event) 587 throws AdminEventListenerException { 588 try { 589 if (_logger.isLoggable(Level.FINEST)) { 590 _logger.log(Level.FINEST, 591 "[ApplicationManager] Handling event " + event.toString()); 592 } 593 594 DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event); 595 596 this.configManager.refreshConfigContext( event.getConfigContext() ); 598 599 boolean jsr77 = false; 601 String action = event.getAction(); 602 if ( (action.equals(BaseDeployEvent.DEPLOY)) || 603 (action.equals(BaseDeployEvent.REDEPLOY)) ) { 604 jsr77 = true; 605 } 606 607 String appName = event.getApplicationName(); 608 609 ConnectorResourcesLoader connecorResourcesLoader = null; 614 if(event.getForceDeploy()) { 615 ResourcesUtil.getInstance().setConfigContext(event.getConfigContext()); 623 ResourcesUtil.getInstance().setReinitConfigContext(false); 624 ServerContext sc = ApplicationServer.getServerContext(); 625 connecorResourcesLoader = new ConnectorResourcesLoader(sc); 626 627 if (event.getLoadUnloadAction() == Constants.LOAD_RAR) { 629 connecorResourcesLoader.loadEmbeddedRarRAConfigs(appName); 630 } 631 } 632 633 if (isEnabled(event.getConfigContext(), appName) && 634 !applicationDeployed(jsr77, appName, event.getConfigContext(), 635 event.getLoadUnloadAction()) ) { 636 637 String msg = localStrings.getString( 638 "applicationmgr.application_deployed_failed", appName); 639 registerException(event, msg); 640 } 641 642 if(event.getLoadUnloadAction() == Constants.LOAD_REST && 644 event.getForceDeploy()) { 645 AbstractLoader appLoader = (AbstractLoader)id2loader.get(appName); 646 if (appLoader != null) { 647 Application appDescriptor = appLoader.getApplication(); 648 connecorResourcesLoader.loadEmbeddedRarResources(appName,appDescriptor); 649 } 650 } 651 } catch (ConfigException ce) { 652 throw new AdminEventListenerException(ce.getMessage()); 653 } finally { 654 ResourcesUtil.getInstance().setReinitConfigContext(true); 655 } 656 } 657 658 public synchronized void applicationUndeployed( 659 ApplicationDeployEvent event) throws AdminEventListenerException { 660 661 boolean jsr77 = false; 663 String action = event.getAction(); 664 if ( (action.equals(BaseDeployEvent.UNDEPLOY)) || 665 (action.equals(BaseDeployEvent.REDEPLOY)) ) { 666 jsr77 = true; 667 } 668 669 try { 670 if (_logger.isLoggable(Level.FINEST)) { 671 _logger.log(Level.FINEST, 672 "[ApplicationManager] Handling event " + event.toString()); 673 } 674 675 this.configManager.refreshConfigContext( 677 event.getOldConfigContext()); 678 679 String appName = event.getApplicationName(); 680 if(event.getForceDeploy()) { 684 event.setCascade(true); 685 } 686 687 boolean undeployed = applicationUndeployed(jsr77, appName, true, event.getCascade(), event.getLoadUnloadAction()); 688 689 if (!undeployed) { 690 String msg = localStrings.getString( 691 "applicationmgr.application_undeployed_failed", appName); 692 registerException(event, msg); 693 } 694 } catch(ConfigException ce) { 695 throw new AdminEventListenerException(ce.getMessage()); 696 } 697 } 698 699 public synchronized void applicationRedeployed( 700 ApplicationDeployEvent event) throws AdminEventListenerException { 701 702 try { 703 if (_logger.isLoggable(Level.FINEST)) { 704 _logger.log(Level.FINEST, 705 "[ApplicationManager] Handling event " + event.toString()); 706 } 707 708 this.configManager.refreshConfigContext( event.getConfigContext() ); 710 711 String appName = event.getApplicationName(); 712 boolean ok = applicationUndeployed(true, appName); 713 if (ok) { 714 ok = applicationDeployed(true, appName, event.getConfigContext()); 715 } 716 717 if (!ok) { 718 String msg = localStrings.getString( 719 "applicationmgr.application_redeployed_failed", appName); 720 registerException(event, msg); 721 } 722 } catch(ConfigException ce) { 723 throw new AdminEventListenerException(ce.getMessage()); 724 } 725 } 726 727 public synchronized void applicationEnabled(ApplicationDeployEvent event) 728 throws AdminEventListenerException { 729 730 ResourcesUtil resutil = ResourcesUtil.getInstance(); 731 try { 732 if (_logger.isLoggable(Level.FINEST)) { 733 _logger.log(Level.FINEST, 734 "[ApplicationManager] Handling event " + event.toString()); 735 } 736 737 if (!isEnabled(event.getConfigContext(), event.getApplicationName())) { 739 return; 740 } 741 742 this.configManager.refreshConfigContext( event.getConfigContext() ); 744 745 String appName = event.getApplicationName(); 746 boolean cascade = true; 747 ServerContext sc = ApplicationServer.getServerContext(); 748 ConnectorResourcesLoader connecorResourcesLoader = 749 new ConnectorResourcesLoader(sc); 750 resutil.setConfigContext(event.getConfigContext()); 751 resutil.setReinitConfigContext(false); 752 753 connecorResourcesLoader.loadEmbeddedRarRAConfigs(appName); 754 boolean enabled = applicationDeployed(false, appName, event.getConfigContext()); 755 756 if (!enabled) { 757 String msg = localStrings.getString( 758 "applicationmgr.application_enabled_failed", appName); 759 registerException(event, msg); 760 } 761 762 AbstractLoader appLoader = 763 (AbstractLoader)id2loader.get(appName); 764 Application appDescriptor = appLoader.getApplication(); 765 if (appDescriptor != null) { 766 connecorResourcesLoader.loadEmbeddedRarResources( 767 appName,appDescriptor); 768 } else { 769 _logger.log(Level.FINE, 770 "[ApplicationManager] Application descriptor is NULL. Skip loading embedded rar resources..."); 771 } 772 } catch(ConfigException ce) { 773 throw new AdminEventListenerException(ce.getMessage()); 774 } catch(Throwable th) { 775 AdminEventListenerException aele = 776 new AdminEventListenerException(th.getMessage()); 777 aele.initCause(th); 778 throw aele; 779 }finally { 780 resutil.setReinitConfigContext(true); 781 } 782 } 783 784 public synchronized void applicationDisabled(ApplicationDeployEvent event) 785 throws AdminEventListenerException { 786 787 try { 788 if (_logger.isLoggable(Level.FINEST)) { 789 _logger.log(Level.FINEST, 790 "[ApplicationManager] Handling event " + event.toString()); 791 } 792 793 this.configManager.refreshConfigContext( event.getConfigContext() ); 795 796 String appName = event.getApplicationName(); 797 AbstractLoader appLoader = (AbstractLoader) id2loader.get(appName); 798 if(appLoader == null) { 799 if (_logger.isLoggable(Level.FINEST)) { 800 _logger.log(Level.FINEST, 801 "[ApplicationManager] appLoader Null. Returning applicationDisabled"); 802 } 803 return; 804 805 } 806 event.setCascade(true); 807 boolean disabled = applicationUndeployed(false, appName,true,true); 808 809 if (!disabled) { 810 String msg = localStrings.getString( 811 "applicationmgr.application_disabled_failed", appName); 812 registerException(event, msg); 813 } 814 } catch(ConfigException ce) { 815 throw new AdminEventListenerException(ce.getMessage()); 816 } 817 } 818 819 820 827 public void applicationReferenceAdded(ApplicationDeployEvent event) 828 throws AdminEventListenerException { 829 } 830 831 838 public void applicationReferenceRemoved(ApplicationDeployEvent event) 839 throws AdminEventListenerException { 840 841 } 842 843 } 845 | Popular Tags |