1 23 package com.sun.enterprise.management.selfmanagement; 24 25 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 26 import com.sun.enterprise.config.serverbeans.Applications; 27 import com.sun.enterprise.config.serverbeans.ApplicationRef; 28 import com.sun.enterprise.config.serverbeans.Mbean; 29 import com.sun.enterprise.config.serverbeans.Domain; 30 import com.sun.enterprise.config.serverbeans.Clusters; 31 import com.sun.enterprise.config.serverbeans.Cluster; 32 import com.sun.enterprise.config.serverbeans.Server; 33 import com.sun.enterprise.config.serverbeans.ManagementRule; 34 import com.sun.enterprise.config.serverbeans.ElementProperty; 35 import com.sun.enterprise.config.serverbeans.ServerTags; 36 37 import com.sun.enterprise.config.ConfigContext; 38 import com.sun.enterprise.config.ConfigException; 39 import com.sun.enterprise.config.ConfigUpdate; 40 41 import com.sun.enterprise.admin.server.core.AdminService; 42 import com.sun.enterprise.admin.common.ObjectNames; 43 import com.sun.enterprise.admin.selfmanagement.event.*; 44 import com.sun.enterprise.management.selfmanagement.LogMgmtEventsNotificationListener; 45 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration; 46 47 import com.sun.appserv.management.alert.MailAlert; 48 import com.sun.enterprise.server.ApplicationServer; 49 import com.sun.enterprise.server.ServerContext; 50 51 import javax.management.ObjectName ; 52 import javax.management.MBeanServer ; 53 import javax.management.NotificationEmitter ; 54 import javax.management.NotificationListener ; 55 import javax.management.NotificationFilter ; 56 import javax.management.NotificationFilterSupport ; 57 import javax.management.InstanceNotFoundException ; 58 import javax.management.RuntimeOperationsException ; 59 import javax.management.MalformedObjectNameException ; 60 import javax.management.InstanceAlreadyExistsException ; 61 62 import java.util.Collections ; 63 import java.util.Map ; 64 import java.util.HashMap ; 65 import java.util.Hashtable ; 66 import java.util.Set ; 67 import java.util.Iterator ; 68 import java.util.logging.Logger ; 69 import java.util.logging.Level ; 70 import java.lang.reflect.Constructor ; 71 import java.lang.String ; 72 73 import com.sun.logging.LogDomains; 74 import com.sun.enterprise.util.i18n.StringManager; 75 76 82 public class RuleManager { 83 84 85 private static Logger _logger = null; 86 87 88 private static StringManager localStrings = null; 89 90 static { 91 _logger = LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER); 92 localStrings = StringManager.getManager(RuleManager.class); 93 } 94 95 96 private Map <String ,Rule> activeRules = null; 97 98 99 private Map <String ,Rule> inActiveRules = null; 100 101 104 private Map <String ,Rule> noActionRules = null; 105 106 107 private Map <String ,Rule> disabledRules = null; 108 109 110 private final String USER_DEFINED_TYPE = "user"; 111 112 113 private ConfigContext configCtx = null; 114 115 private String instanceName = null; 116 117 118 private MBeanServer instanceMbs = null; 119 120 121 private String mailSMTPHost = null; 122 123 124 private String srvAlertRecipients = null; 125 126 127 private SelfManagementService service = null; 128 129 130 public RuleManager(SelfManagementService service) { 131 this.service = service; 132 instanceName = (ApplicationServer.getServerContext()).getInstanceName(); 133 AdminService adSrv = AdminService.getAdminService(); 134 instanceMbs = (adSrv.getAdminContext()).getMBeanServer(); 135 configCtx = (ApplicationServer.getServerContext()).getConfigContext(); 136 137 activeRules = Collections.synchronizedMap(new HashMap <String ,Rule>()); 138 inActiveRules = Collections.synchronizedMap(new HashMap <String ,Rule>()); 139 noActionRules = Collections.synchronizedMap(new HashMap <String , Rule>()); 140 disabledRules = Collections.synchronizedMap(new HashMap <String ,Rule>()); 141 } 142 143 146 public void addRule(String ruleName, String description, Event ruleEvent, 147 String actionMBeanName, Object handback, 148 ConfigContext ctxToUse) throws InstanceNotFoundException { 149 ObjectName actionObjName = null; 150 Object alertAction = null; 151 Rule ruleInstance = new Rule(ruleName, description); 152 if (ctxToUse!=null) { 154 configCtx = ctxToUse; 155 } 156 157 try { 158 if (actionMBeanName != null) { 159 160 Domain domain = ServerBeansFactory.getDomainBean(configCtx); 161 ApplicationRef appRef = verifyActionMBean(actionMBeanName, domain, 162 ctxToUse); 163 164 165 if (appRef != null) { 166 Applications apps = domain.getApplications(); 167 Mbean definedMBean = apps.getMbeanByName(actionMBeanName); 168 169 170 if (appRef.isEnabled() && definedMBean.isEnabled() ) { 171 if ((definedMBean.getObjectType()).equals(USER_DEFINED_TYPE)) { 172 actionObjName = registerAction(ruleEvent,actionMBeanName, 173 definedMBean,false, handback); 174 } else { 175 actionObjName = registerAction(ruleEvent,actionMBeanName, 177 definedMBean,true, handback); 178 } 179 setupRuleBinding(Rule.ENABLED, ruleInstance, ruleEvent, 181 (Object )actionObjName); 182 } else { 186 189 alertAction = registerAlertAction(ruleEvent, actionMBeanName, 190 ruleName, handback); 191 _logger.log(Level.WARNING,"sgmt.ruleactiondisabled", 192 new Object []{ruleName,actionMBeanName}); 193 194 setupRuleBinding(Rule.INACTIVE, ruleInstance, ruleEvent, 195 alertAction); 196 } 200 } else { 201 _logger.log(Level.SEVERE, "smgt.invalid_action", new Object []{ 203 ruleName, actionMBeanName}); 204 } 206 } else { 207 setupRuleBinding(Rule.NOACTION, ruleInstance, ruleEvent, null); 209 } 210 } catch (ConfigException ex) { 211 _logger.log(Level.INFO, "config_error", ex); 212 } catch (InstanceNotFoundException ex) { 213 _logger.log(Level.INFO, "smgt.error_reg_action", new Object []{ 215 ruleName, actionMBeanName}); 216 registerAlertAction(ruleEvent, actionMBeanName, ruleName, handback); 217 } catch (MalformedObjectNameException ex) { 218 _logger.log(Level.WARNING, "sgmt.error_reg_action", new Object []{ 219 ruleName, actionMBeanName}); 220 } catch (RuntimeOperationsException ex) { 221 _logger.log(Level.WARNING, "smgt.error_reg_action", new Object []{ 223 ruleName,actionMBeanName}); 224 registerAlertAction(ruleEvent, actionMBeanName, ruleName, handback); 225 } catch (RuntimeException ex) { 226 229 _logger.log(Level.WARNING, "smgt.error_reg_action", new Object [] { 230 ruleName, actionMBeanName}); 231 registerAlertAction(ruleEvent, actionMBeanName, ruleName, handback); 232 } 233 } 234 235 244 private ApplicationRef verifyActionMBean(String actionMBeanName, 245 Domain domain, 246 ConfigContext ctxToUse) { 247 ApplicationRef appRef = null; 248 if (ctxToUse != null) { 249 configCtx = ctxToUse; 250 } 251 252 try { 253 Server instanceBean = ServerBeansFactory.getServerBean(configCtx); 254 appRef = instanceBean.getApplicationRefByRef(actionMBeanName); 255 256 if (appRef == null) { 257 Clusters clusters = domain.getClusters(); 259 Cluster[] cluster = clusters.getCluster(); 260 for (Cluster val : cluster) { 261 if ((val.getServerRefByRef(instanceName)) != null) { 262 appRef = val.getApplicationRefByRef(actionMBeanName); 263 break; 264 } 265 } 266 } 267 268 if (appRef != null) { 270 Applications apps = domain.getApplications(); 272 Mbean definedMBean = apps.getMbeanByName(actionMBeanName); 273 if (definedMBean == null ) { 274 appRef = null; 275 } 276 } 277 } catch (ConfigException ex) { 278 _logger.log(Level.INFO, "smgt.config_error", ex); 279 } 280 281 return appRef; 282 } 283 284 289 public void deleteRule(String name) { 290 Event ruleEvent = null; 291 String description = null; 292 Rule ruleInstance = null; 293 294 try { 295 if (activeRules.containsKey(name)) { 296 ruleInstance = activeRules.remove(name); 298 ruleEvent = ruleInstance.getEvent(); 299 NotificationFilter filter = ruleEvent.getNotificationFilter(); 300 ObjectName eventObjName = ruleEvent.getObjectName(); 301 302 ObjectName actionObjName = (ObjectName )ruleInstance.getAction(); 303 instanceMbs.removeNotificationListener(eventObjName,actionObjName, 304 filter, null); 305 ruleEvent.destroy(); 306 307 } else if (inActiveRules.containsKey(name)) { 308 ruleInstance = inActiveRules.remove(name); 310 ruleEvent = ruleInstance.getEvent(); 312 ruleEvent.destroy(); 313 } else if (noActionRules.containsKey(name)) { 314 ruleInstance = noActionRules.remove(name); 316 ruleEvent = ruleInstance.getEvent(); 317 ruleEvent.destroy(); 318 } else if (disabledRules.containsKey(name)) { 319 ruleInstance = disabledRules.remove(name); 321 } else { 322 _logger.log(Level.WARNING,"sgmt.delete_invalidname", name); 324 return; 325 } 326 } catch (Exception ex) { 327 _logger.log(Level.WARNING, "sgmt.error_delete_rule", 328 new Object []{name,ex.toString()}); 329 return; 330 } 331 332 description = ruleInstance.getDescription(); 333 334 _logger.log(Level.INFO,"sgmt.deleted_rule", 335 new Object [] {name,description}); 336 337 ruleInstance = null; 339 } 340 341 342 351 public void addDisabledRule(String ruleName, String ruleDescription) { 352 Rule ruleInstance = new Rule(ruleName, ruleDescription); 353 setupRuleBinding(Rule.DISABLED, ruleInstance, null, null); 354 } 355 356 363 Event checkEventExists(String type, ElementProperty[] eventProps) { 364 return null; 366 367 } 368 369 380 private ObjectName registerAction(Event ruleEvent, String actionMBeanName, 381 Mbean definedActionMBean, boolean isSystemDefined, Object handback) 382 throws InstanceNotFoundException , 383 MalformedObjectNameException { 384 ObjectName actionObjName = null; 385 ObjectName eventObjName = ruleEvent.getObjectName(); 386 NotificationFilter filter = ruleEvent.getNotificationFilter(); 387 388 try { 389 if (isSystemDefined) { 391 CustomMBeanRegistration obj = getCustomMBeanRegistration(); 392 actionObjName = obj.registerMBean(definedActionMBean); 393 instanceMbs.addNotificationListener(eventObjName, actionObjName, 394 filter, null); 395 } else { 396 String objName = definedActionMBean.getObjectName(); 400 if (objName == null) { 401 actionObjName = ObjectNames.getApplicationObjectName( 403 instanceName, 404 actionMBeanName); 405 } else { 406 actionObjName = getCascadedObjectName(objName); 408 } 409 410 instanceMbs.addNotificationListener(eventObjName, actionObjName, 411 filter, null); 412 if (handback!=null) { 413 createLoggingListener (ruleEvent, handback); 414 } 415 } 416 } catch (RuntimeException ex) { 417 if ( ex.getCause() instanceof InstanceAlreadyExistsException ) { 418 String objName = definedActionMBean.getObjectName(); 419 actionObjName = getCascadedObjectName(objName); 420 } else { 421 throw ex; 422 } 423 } 424 425 return actionObjName; 426 } 427 428 438 private Object registerAlertAction(Event ruleEvent, String actionMBeanName, 439 String ruleName, Object handback) 440 throws InstanceNotFoundException { 441 NotificationFilterSupport filter = null; 442 if (srvAlertRecipients == null) { 443 filter = new NotificationFilterSupport (); 445 filter.disableAllTypes(); 446 } 447 String subject = localStrings.getString("ruleMgr.alert_action", 448 ruleName, actionMBeanName); 449 MailAlert alertAction = new MailAlert(); 450 alertAction.setSubject(subject); 451 alertAction.setRecipients(srvAlertRecipients); 452 453 454 ObjectName eventObjName = ruleEvent.getObjectName(); 455 createLoggingListener (ruleEvent, handback); 457 458 return alertAction; 459 } 460 471 private void setupRuleBinding(int ruleState, Rule ruleInstance, 472 Event ruleEvent, Object action) { 473 474 String ruleName = ruleInstance.getName(); 475 476 ruleInstance.setState(ruleState); 477 if (ruleState != Rule.DISABLED) { 478 ruleInstance.setEvent(ruleEvent); 479 ruleInstance.setAction(action); 480 ruleInstance.setEnabled(true); 481 } 482 483 switch (ruleState) { 484 case Rule.ENABLED: 485 activeRules.put(ruleName, ruleInstance); 486 break; 487 488 case Rule.INACTIVE: 489 inActiveRules.put(ruleName, ruleInstance); 490 break; 491 492 case Rule.NOACTION: 493 noActionRules.put(ruleName, ruleInstance); 494 break; 495 496 case Rule.DISABLED: 497 disabledRules.put(ruleName, ruleInstance); 499 break; 500 } 501 } 502 503 507 private CustomMBeanRegistration getCustomMBeanRegistration() { 508 509 final String CUSTOM_REGRISTRATION_IMPL_CLASS = 511 "com.sun.enterprise.admin.mbeans.custom.loading.CustomMBeanRegistrationImpl"; 512 CustomMBeanRegistration customObj = null; 513 514 Constructor ctor = null; 515 Object [] args = null; 516 517 try { 518 Class c = Class.forName(CUSTOM_REGRISTRATION_IMPL_CLASS); 519 Class [] params = new Class []{javax.management.MBeanServer .class}; 520 ctor = c.getConstructor(params); 521 args = new Object []{instanceMbs}; 522 523 customObj = (CustomMBeanRegistration) ctor.newInstance(args); 524 } catch (Exception ex) { 525 _logger.log(Level.FINE,"smgt.internal_error", ex); 526 } 527 528 return customObj; 529 } 530 531 538 public void setMailSMTPHost(String value) { 539 if ( mailSMTPHost != null) { 540 mailSMTPHost = value; 541 } else { 542 mailSMTPHost = "localhost"; 543 } 544 } 545 546 553 public void setRecipients(String value) { 554 srvAlertRecipients = value; 555 } 556 557 560 private ObjectName getCascadedObjectName(String actionMBeanName) 561 throws MalformedObjectNameException { 562 ObjectName configObjName = new ObjectName (actionMBeanName); 563 String serverNameKey = ServerTags.SERVER; 564 Hashtable properties = configObjName.getKeyPropertyList(); 565 properties.put(serverNameKey, instanceName); 566 ObjectName casdObjName = new ObjectName (configObjName.getDomain(), 567 properties); 568 return (casdObjName); 569 } 570 571 572 private void createLoggingListener (Event event, Object handback) 573 throws InstanceNotFoundException { 574 NotificationListener logListener = LogMgmtEventsNotificationListener.getInstance(); 575 instanceMbs.addNotificationListener (event.getObjectName(), logListener, event.getNotificationFilter(), handback); 576 return; 577 } 578 579 587 void updateRule(ManagementRule rule, ConfigUpdate configUp, 588 ConfigContext ctxToUse) throws Exception { 589 String name = rule.getName(); 590 Rule ruleInstance = getRuleInstance(name); 591 592 if (ruleInstance!=null) { 594 synchronized(ruleInstance) { 595 String description = ruleInstance.getDescription(); 596 Set <String > attributesChg = configUp.getAttributeSet(); 597 for (String attribute : attributesChg) { 598 if (attribute.equals(ServerTags.ENABLED)) { 599 String oldValue = configUp.getOldValue(attribute); 600 String newValue = configUp.getNewValue(attribute); 601 _logger.log(Level.INFO,"sgmt.updateruleattribute", 602 new Object []{attribute,oldValue,newValue}); 603 if (ruleInstance.isEnabled() && newValue.equals("false")) { 604 if (service.isServiceEnabled()) { 605 disableRule(ruleInstance,false); 607 _logger.log(Level.INFO,"smgt.disabledrule", 608 new Object []{name,description}); 609 } else { 610 _logger.log(Level.WARNING,"sgmt.error_disablerule_noservice", 612 new Object []{name,description}); 613 } 614 } else if (!ruleInstance.isEnabled() && newValue.equals("true")) { 615 if (service.isServiceEnabled()) { 617 service.addRule(rule,ctxToUse); 618 _logger.log(Level.INFO,"smgt.enabledrule", 619 new Object []{name,description}); 620 } else { 621 _logger.log(Level.WARNING,"sgmt.error_enablerule_noservice", 623 new Object []{name,description}); 624 } 625 } else { 626 _logger.log(Level.INFO,"smgt.cannot_changesstate", 627 new Object []{name,description}); 628 } 629 } 630 } 631 } 632 } 633 } 634 635 private Rule getRuleInstance(String ruleName) { 636 Rule rule = null; 637 if (activeRules.containsKey(ruleName)) { 638 rule = activeRules.get(ruleName); 639 } else if (disabledRules.containsKey(ruleName)) { 640 rule = disabledRules.get(ruleName); 641 } else if (inActiveRules.containsKey(ruleName)) { 642 rule = inActiveRules.get(ruleName); 643 } else if (noActionRules.containsKey(ruleName)) { 644 rule = noActionRules.get(ruleName); 645 } 646 647 return rule; 648 } 649 650 private Map getRuleMap(String ruleName) { 651 Map ruleMap = null; 652 653 if (activeRules.containsKey(ruleName)) { 654 ruleMap = activeRules; 655 } else if (disabledRules.containsKey(ruleName)) { 656 ruleMap = disabledRules; 657 } else if (inActiveRules.containsKey(ruleName)) { 658 ruleMap = inActiveRules; 659 } else if (noActionRules.containsKey(ruleName)) { 660 ruleMap = noActionRules; 661 } 662 663 return ruleMap; 664 } 665 666 private void disableRule(Rule ruleInstance, boolean disableService) throws Exception { 667 673 String ruleName = ruleInstance.getName(); 674 Event ruleEvent = ruleInstance.getEvent(); 675 676 677 try { 678 if (ruleInstance.getState() == Rule.ENABLED) { 679 NotificationFilter filter = ruleEvent.getNotificationFilter(); 681 ObjectName eventObjName = ruleEvent.getObjectName(); 682 ObjectName actionObjName = (ObjectName )ruleInstance.getAction(); 683 684 instanceMbs.removeNotificationListener(eventObjName,actionObjName, 685 filter, null); 686 ruleInstance.setAction(null); 687 if (!disableService) { 688 activeRules.remove(ruleName); 689 } 690 } else if (ruleInstance.getState() == Rule.INACTIVE) { 691 ruleInstance.setAction(null); 693 if (!disableService) { 694 inActiveRules.remove(ruleName); 695 } 696 } else if (ruleInstance.getState() == Rule.NOACTION) { 697 ruleInstance.setAction(null); 698 if (!disableService) { 699 noActionRules.remove(ruleName); 700 } 701 } 702 703 ruleEvent.destroy(); 705 ruleInstance.setEvent(null); 706 if (!disableService) { 707 ruleInstance.setEnabled(false); 709 ruleInstance.setState(Rule.DISABLED); 710 disabledRules.put(ruleName,ruleInstance); 711 712 } 713 } catch (Exception ex) { 714 _logger.log(Level.WARNING,"sgmt.error_disablerule", 715 new Object []{ruleName,ex}); 716 throw ex; 717 } 718 719 } 720 721 725 synchronized void disableService() throws Exception { 726 int size = activeRules.size() + inActiveRules.size() + noActionRules.size(); 727 Map <String ,Rule> completeMap = (Map <String ,Rule>) new HashMap (size); 728 completeMap.putAll(activeRules); 729 completeMap.putAll(inActiveRules); 730 completeMap.putAll(noActionRules); 731 732 Set <String > rules = completeMap.keySet(); 733 for (String name : rules) { 734 Rule ruleInstance = completeMap.get(name); 735 disableRule(ruleInstance,true); 736 } 737 } 738 739 745 void addAction(String ruleName, String actionName, ConfigContext ctxToUse) { 746 Rule ruleInstance = getRuleInstance(ruleName); 747 if (ruleInstance!=null) { 748 synchronized(ruleInstance) { 749 bindAction(ruleInstance,actionName,ctxToUse); 750 } 751 } 752 } 753 754 private void bindAction(Rule ruleInstance, String actionMBeanName, 755 ConfigContext ctxToUse) { 756 String ruleName = ruleInstance.getName(); 757 Event ruleEvent = ruleInstance.getEvent(); 758 ObjectName actionObjName = null; 759 760 try { 761 Domain domain = ServerBeansFactory.getDomainBean(ctxToUse); 762 ApplicationRef appRef = verifyActionMBean(actionMBeanName, domain, ctxToUse); 763 764 765 if (appRef != null) { 766 Applications apps = domain.getApplications(); 767 Mbean definedMBean = apps.getMbeanByName(actionMBeanName); 768 769 770 if (appRef.isEnabled() && definedMBean.isEnabled() ) { 771 if ((definedMBean.getObjectType()).equals(USER_DEFINED_TYPE)) { 772 actionObjName = registerAction(ruleEvent,actionMBeanName, 773 definedMBean,false,null); 774 } else { 775 actionObjName = registerAction(ruleEvent,actionMBeanName, 777 definedMBean,true,null); 778 } 779 ruleInstance.setAction((Object )actionObjName); 781 _logger.log(Level.INFO,"smgt.successaddaction", 782 new Object []{ruleName,actionMBeanName}); 783 } else { 784 787 _logger.log(Level.WARNING,"sgmt.ruleactiondisabled", 790 new Object []{ruleName,actionMBeanName}); 791 ruleInstance.setState(Rule.INACTIVE); 793 } 794 } else { 795 _logger.log(Level.SEVERE, "smgt.invalid_action", new Object []{ 797 ruleName, actionMBeanName}); 798 } 800 } catch (ConfigException ex) { 801 _logger.log(Level.INFO, "config_error", ex); 802 } catch (InstanceNotFoundException ex) { 803 _logger.log(Level.INFO, "smgt.error_reg_action", new Object []{ 805 ruleName, actionMBeanName}); 806 } catch (MalformedObjectNameException ex) { 808 _logger.log(Level.WARNING, "sgmt.error_reg_action", new Object []{ 809 ruleName, actionMBeanName}); 810 } catch (RuntimeOperationsException ex) { 811 _logger.log(Level.WARNING, "smgt.error_reg_action", new Object []{ 813 ruleName,actionMBeanName}); 814 } catch (RuntimeException ex) { 816 819 _logger.log(Level.WARNING, "smgt.error_reg_action", new Object [] { 820 ruleName, actionMBeanName}); 821 } 823 } 824 825 } 826 | Popular Tags |