1 23 package com.sun.enterprise.management.selfmanagement; 24 25 import com.sun.appserv.server.ServerLifecycle; 26 import com.sun.appserv.server.ServerLifecycleException; 27 import com.sun.enterprise.server.ServerContext; 28 29 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 30 import com.sun.enterprise.config.serverbeans.Config; 31 import com.sun.enterprise.config.serverbeans.ElementProperty; 32 import com.sun.enterprise.config.serverbeans.ManagementRules; 33 import com.sun.enterprise.config.serverbeans.ManagementRule; 34 import com.sun.enterprise.config.serverbeans.Event; 35 import com.sun.enterprise.config.serverbeans.Action; 36 import com.sun.enterprise.config.serverbeans.ServerTags; 37 import com.sun.enterprise.config.ConfigUpdate; 38 39 import com.sun.enterprise.config.ConfigContext; 40 import com.sun.enterprise.config.ConfigException; 41 42 import com.sun.enterprise.admin.AdminContext; 43 import com.sun.enterprise.admin.server.core.AdminService; 44 import com.sun.enterprise.admin.event.AdminEventListenerRegistry; 45 import com.sun.enterprise.admin.event.selfmanagement.ManagementRuleEvent; 46 import com.sun.enterprise.management.selfmanagement.reconfig.ManagementRuleReconfig; 47 48 import javax.management.MBeanServer ; 49 import javax.management.InstanceNotFoundException ; 50 import javax.management.MalformedObjectNameException ; 51 52 import com.sun.enterprise.admin.selfmanagement.event.EventBuilder; 54 55 import java.util.logging.Logger ; 56 import java.util.logging.Level ; 57 import java.lang.String ; 58 import java.util.HashMap ; 59 import java.util.Map ; 60 import java.util.Set ; 61 import com.sun.logging.LogDomains; 62 63 70 public class SelfManagementService implements ServerLifecycle { 71 72 static private Logger _logger = LogDomains.getLogger( 73 LogDomains.SELF_MANAGEMENT_LOGGER); 74 75 private static SelfManagementService serviceInstance = null; 76 77 private RuleManager rulesManager = null; 78 79 private EventBuilder eventBuilder = null; 80 81 private boolean isServiceEnabled = false; 82 83 84 85 public SelfManagementService() { 86 87 } 88 89 102 public void onInitialization(ServerContext sc) 103 throws ServerLifecycleException { 104 108 final String RULES_MAIL_SMTP_HOST = "mail.smtp.host"; 109 final String RULES_ALERT_RECIPIENTS = "rules.recipients"; 110 111 rulesManager = new RuleManager(this); 113 eventBuilder = EventBuilder.getInstance(); 114 _logger.log(Level.FINE, "smgt.service_init"); 115 116 String mailSMTPHost = java.lang.System.getProperty(RULES_MAIL_SMTP_HOST); 117 String alertRecip = java.lang.System.getProperty(RULES_ALERT_RECIPIENTS); 118 rulesManager.setMailSMTPHost(mailSMTPHost); 119 rulesManager.setRecipients(alertRecip); 120 serviceInstance = this; 121 122 _logger.log(Level.INFO, "sgmt.service_initialized"); 123 } 124 125 133 public void onReady(ServerContext sc) 134 throws ServerLifecycleException { 135 136 try { 137 ConfigContext instanceConfigContext = sc.getConfigContext(); 138 Config instanceConfig = ServerBeansFactory.getConfigBean( 139 instanceConfigContext); 140 ManagementRules selfManagementRules = instanceConfig.getManagementRules(); 141 142 if ( selfManagementRules != null ) { 143 if ( selfManagementRules.isEnabled() ) { 144 _logger.log(Level.INFO, "smgt.service_enabled"); 145 isServiceEnabled = true; 146 147 ManagementRule[] rules = selfManagementRules.getManagementRule(); 148 if (rules != null) { 149 for (int i = 0; i < rules.length; i++) { 150 ManagementRule rule = rules[i]; 151 String ruleName = rule.getName(); 152 String ruleDescription = rule.getDescription(); 153 154 if (rule.isEnabled()) { 156 addRule(rule,null); 158 } else { 159 _logger.log(Level.INFO, "smgt.service_rule_disabled", 160 new Object []{ruleName, ruleDescription}); 161 addDisabledRule(rule); 163 } 164 } 165 } else { 166 _logger.log(Level.INFO,"smgt.service_no_rules_defined"); 167 } 168 } else { 169 _logger.log(Level.INFO,"smgt.service_disabled"); 170 } 171 } else { 172 _logger.log(Level.INFO, "smgt.service_not_configured"); 173 } 174 175 AdminEventListenerRegistry.addEventListener(ManagementRuleEvent.eventType, 177 new ManagementRuleReconfig()); 178 } catch (ConfigException ex) { 179 _logger.log(Level.SEVERE, "smgt.config_error", ex); 181 } catch (Exception ex) { 183 _logger.log(Level.SEVERE, "sgmt.config_error, ex"); 184 } 185 } 186 187 195 public void onStartup(ServerContext sc) throws ServerLifecycleException { 196 _logger.log(Level.FINE, "smgt.service_onready"); 197 198 } 199 200 206 public void onShutdown() 207 throws ServerLifecycleException { 208 _logger.log(Level.FINE, "smgt.service_ontermination"); 209 } 210 211 220 public void onTermination() 221 throws ServerLifecycleException { 222 _logger.log(Level.FINE, "smgt.service_onshutdown"); 223 } 224 225 231 private com.sun.enterprise.admin.selfmanagement.event.Event checkEventExist( 232 String type, ElementProperty[] eventProps) { 233 com.sun.enterprise.admin.selfmanagement.event.Event event = 235 rulesManager.checkEventExists(type, eventProps); 236 237 return event; 238 } 239 240 243 public void addRule(ManagementRule rule, ConfigContext ctxToUse) { 244 245 String actionMBeanName = null; 246 boolean isSkip = false; 247 String ruleName = rule.getName(); 248 String ruleDescription = rule.getDescription(); 249 250 try { 251 final int ATTRIB_COUNT = 3; 252 com.sun.enterprise.config.serverbeans.Event event = null; 253 com.sun.enterprise.admin.selfmanagement.event.Event ruleEvent = null; 254 Map logHandback = new HashMap <String ,String >(ATTRIB_COUNT); 255 256 _logger.log(Level.INFO, "smgt.service_rule_enabled", 257 new Object []{ruleName, ruleDescription}); 258 259 event = rule.getEvent(); 261 String type = event.getType(); 262 String description = event.getDescription(); 263 264 logHandback.put(ServerTags.TYPE, type); 265 logHandback.put(ServerTags.RECORD_EVENT, String.valueOf(event.isRecordEvent())); 266 logHandback.put(ServerTags.LEVEL,event.getLevel()); 267 268 270 ElementProperty[] eventProps = event.getElementProperty(); 271 272 273 275 ruleEvent = getEvent(type, eventProps,description, ruleName); 277 if (ruleEvent == null) { 278 return ; 280 } 281 282 Action ruleAction = rule.getAction(); 283 if (ruleAction != null) { 284 actionMBeanName = ruleAction.getActionMbeanName(); 285 } 286 287 rulesManager.addRule(ruleName, description, ruleEvent, 289 actionMBeanName, logHandback, ctxToUse); 290 } catch (InstanceNotFoundException ex) { 291 _logger.log(Level.INFO, "smgt.internal_error", ex); 292 } 293 } 294 295 301 public void deleteRule(String rule) { 302 rulesManager.deleteRule(rule); 304 } 305 306 309 public void addDisabledRule(ManagementRule rule) { 310 String ruleName = rule.getName(); 312 String ruleDescription = rule.getDescription(); 313 rulesManager.addDisabledRule(ruleName, ruleDescription); 314 315 } 316 317 322 private com.sun.enterprise.admin.selfmanagement.event.Event getEvent( 323 String type, 324 ElementProperty[] eventProps, 325 String description, 326 String ruleName) { 327 com.sun.enterprise.admin.selfmanagement.event.Event eventInstance = null; 328 329 try { 330 eventInstance = eventBuilder.getEvent(type, eventProps, description); 331 } catch( IllegalArgumentException ex) { 332 _logger.log(Level.INFO, "smgt.errorenous_event", new Object [] 334 {ruleName, type}); 335 } 336 337 return eventInstance; 338 } 339 340 341 public static SelfManagementService getService() { 342 return serviceInstance; 343 } 344 345 350 public void handleRulesUpdate(ConfigContext cfgCtx, ConfigUpdate configUp) 351 throws Exception { 352 Set <String > attributesChg = configUp.getAttributeSet(); 353 for (String attribute : attributesChg) { 354 if (attribute.equals(ServerTags.ENABLED)) { 355 String newValue = configUp.getNewValue(attribute); 356 if (isServiceEnabled && newValue.equals("false")) { 357 _logger.log(Level.INFO,"sgmt.disableservice"); 358 synchronized(this) { 359 isServiceEnabled = false; 360 rulesManager.disableService(); 361 } 362 } else if (!isServiceEnabled && newValue.equals("true")) { 363 _logger.log(Level.INFO,"sgmt.enableservice"); 364 synchronized(this) { 365 isServiceEnabled = true; 366 handleEnableService(cfgCtx); 367 } 368 } 369 } 370 } 371 } 372 373 379 public void handleActionAdd(ManagementRule rule, ConfigContext ctxToUse) 380 throws Exception { 381 String ruleName = rule.getName(); 382 String description = rule.getDescription(); 383 Action actionToAdd = rule.getAction(); 384 if (actionToAdd!=null) { 386 String actionName = actionToAdd.getActionMbeanName(); 387 if (isServiceEnabled && rule.isEnabled()) { 389 if (actionName!=null) { 390 391 rulesManager.addAction(ruleName,actionName,ctxToUse); 392 } 393 } else { 394 _logger.log(Level.INFO, "smgt.erroraddaction_notenabled", 395 new Object []{ruleName,actionName}); 396 } 397 } else { 398 _logger.log(Level.SEVERE,"smgt.error_add_noactionreceived", 399 new Object []{ruleName,description}); 400 throw new RuntimeException (); 401 } 402 } 403 404 410 public void handleActionUpdate(ManagementRule rule, ConfigUpdate configUp) { 411 } 413 414 420 public void handleRuleUpdate(ManagementRule rule, ConfigUpdate configUp, 421 ConfigContext ctxToUse) 422 throws Exception { 423 rulesManager.updateRule(rule, configUp, ctxToUse); 425 } 426 427 433 void handleEnableService(ConfigContext configCtx) { 434 try { 435 Config instanceConfig = ServerBeansFactory.getConfigBean(configCtx); 436 ManagementRules selfManagementRules = instanceConfig.getManagementRules(); 437 ManagementRule[] rules = selfManagementRules.getManagementRule(); 438 439 for (ManagementRule rule : rules) { 440 if (rule.isEnabled()) { 441 addRule(rule, configCtx); 442 } else { 443 addDisabledRule(rule); 444 } 445 } 446 } catch (ConfigException ex) { 447 _logger.log(Level.WARNING,"sgmt.error_enableservice", ex); 448 } 449 } 450 451 boolean isServiceEnabled() { 452 return isServiceEnabled; 453 } 454 } 455 | Popular Tags |