FileDocCategorySizeDatePackage
SelfManagementService.javaAPI DocGlassfish v2 API19216Fri May 04 22:35:04 BST 2007com.sun.enterprise.management.selfmanagement

SelfManagementService

public class SelfManagementService extends Object implements com.sun.appserv.server.ServerLifecycle
The core class for the self management service. As part of server startup, it reads in the instance management rules and sets them up.
author
Pankaj Jairath

Fields Summary
private static final Logger
_logger
private static SelfManagementService
serviceInstance
private RuleManager
rulesManager
private com.sun.enterprise.admin.selfmanagement.event.EventBuilder
eventBuilder
private boolean
isServiceEnabled
Constructors Summary
public SelfManagementService()
Creates a new instance of SelfManagementService

    
              
           
      
        
    
Methods Summary
public voidaddDisabledRule(com.sun.enterprise.config.serverbeans.ManagementRule rule)
Adds to the list of disabled rules

        //pass the invocation to rule manager
        String ruleName = rule.getName();
        String ruleDescription = rule.getDescription();
        rulesManager.addDisabledRule(ruleName, ruleDescription);
    
    
public voidaddRule(com.sun.enterprise.config.serverbeans.ManagementRule rule, com.sun.enterprise.config.ConfigContext ctxToUse)
Invokes the rule managers add rule to form the runtime binding

                  
        String actionMBeanName = null;
        boolean isSkip = false;
        String ruleName = rule.getName();
        String ruleDescription = rule.getDescription();
        
        try {
            final int ATTRIB_COUNT = 3;
            com.sun.enterprise.config.serverbeans.Event event = null;
            com.sun.enterprise.admin.selfmanagement.event.Event ruleEvent = null;  
            Map logHandback = new HashMap<String,String>(ATTRIB_COUNT);
            
            _logger.log(Level.INFO, "smgt.service_rule_enabled", 
                        new Object[]{ruleName, ruleDescription});
                        
            //getEvent                       
            event = rule.getEvent();
            String type = event.getType();
            String description = event.getDescription();
                                                             
            logHandback.put(ServerTags.TYPE, type);                 
            logHandback.put(ServerTags.RECORD_EVENT, String.valueOf(event.isRecordEvent()));
            logHandback.put(ServerTags.LEVEL,event.getLevel());
            
            //Properties eventProperities = new Properties();
                       
            ElementProperty[] eventProps = event.getElementProperty();
                                                      
            /*activate after dependency support provided*/
            //Event event = checkEventExists(type, eventProps);
                                
            //EventBuilder eventBuilder = EventBuilder.getInstance();
            ruleEvent = getEvent(type, eventProps,description, ruleName);
            if (ruleEvent == null) {
                //skip this rule, errorenous event props
                return ;
            }
        
            Action ruleAction = rule.getAction(); 
            if (ruleAction != null) {
                actionMBeanName = ruleAction.getActionMbeanName();
            }
       
            // pass the invocation to the contained core instance
            rulesManager.addRule(ruleName, description, ruleEvent,
                                 actionMBeanName, logHandback, ctxToUse);
        } catch (InstanceNotFoundException ex) {
            _logger.log(Level.INFO, "smgt.internal_error", ex);
        }
    
private com.sun.enterprise.admin.selfmanagement.event.EventcheckEventExist(java.lang.String type, com.sun.enterprise.config.serverbeans.ElementProperty[] eventProps)
Checks if the event has been already defined for the management rules.

return
Event The existing event or null if no such event exists.

        //pass the invocation to the contained core instance
        com.sun.enterprise.admin.selfmanagement.event.Event event = 
                               rulesManager.checkEventExists(type, eventProps);
                                                   
        return event;      
    
public voiddeleteAction(java.lang.String rule)
Deletes an action associated with the configured rule

param
name: Name of the rule for which action is to be deleted

       rulesManager.deleteAction(rule);
   
public voiddeleteRule(java.lang.String rule)
Deletes a configured rule

param
name: Name of the rule to delete

         //delegate it to the rule manager
         rulesManager.deleteRule(rule);
    
private com.sun.enterprise.admin.selfmanagement.event.EventgetEvent(java.lang.String type, com.sun.enterprise.config.serverbeans.ElementProperty[] eventProps, java.lang.String description, java.lang.String ruleName)
Obtains the configured event

return
The event instance

        com.sun.enterprise.admin.selfmanagement.event.Event eventInstance = null;
        
        try {
            eventInstance = eventBuilder.getEvent(type, eventProps, description);            
        } catch( IllegalArgumentException ex) {
            // some issue with configuring the event props
            _logger.log(Level.INFO, "smgt.errorenous_event", new Object[] 
                         {ruleName, type});                        
        }
        
        return eventInstance;
    
public static com.sun.enterprise.management.selfmanagement.SelfManagementServicegetService()
Obtain instance to the service

        return serviceInstance;
    
public voidhandleActionAdd(com.sun.enterprise.config.serverbeans.ManagementRule rule, com.sun.enterprise.config.ConfigContext ctxToUse)
Handle Action addition

param
rule - ManagementRule for which action is being added
param
action - Action being added

       String ruleName = rule.getName();
       String description = rule.getDescription();
       Action actionToAdd = rule.getAction();
       //Should be the case with validator
       if (actionToAdd!=null) {
           String actionName = actionToAdd.getActionMbeanName();
           //validator? 
           if (isServiceEnabled && rule.isEnabled())  {
               if (actionName!=null) {

                   rulesManager.addAction(ruleName,actionName,ctxToUse);
               }
           } else {
               _logger.log(Level.INFO, "smgt.erroraddaction_notenabled",
                           new Object[]{ruleName,actionName});
           }
       } else {
           _logger.log(Level.SEVERE,"smgt.error_add_noactionreceived",  
                       new Object[]{ruleName,description});
           throw new RuntimeException();
       }
   
public voidhandleActionUpdate(com.sun.enterprise.config.serverbeans.ManagementRule rule, com.sun.enterprise.config.ConfigUpdate configUp)
Handle Action upate

param
rule : ManagementRule element that has been updated configUp : The set of config changes on the child Action element

       //not yet supported
   
voidhandleEnableService(com.sun.enterprise.config.ConfigContext configCtx)
handles the management rules being enabled

param
configCtx The instance config context containing the management rules

       try {
           Config instanceConfig = ServerBeansFactory.getConfigBean(configCtx);
           ManagementRules selfManagementRules = instanceConfig.getManagementRules();
           ManagementRule[] rules = selfManagementRules.getManagementRule();

           for (ManagementRule rule : rules) {
               if (rule.isEnabled()) {
                   addRule(rule, configCtx);
               } else {
                   addDisabledRule(rule);
               }
           }
       } catch (ConfigException ex) {
           _logger.log(Level.WARNING,"sgmt.error_enableservice", ex);           
       } 
   
public voidhandleRuleUpdate(com.sun.enterprise.config.serverbeans.ManagementRule rule, com.sun.enterprise.config.ConfigUpdate configUp, com.sun.enterprise.config.ConfigContext ctxToUse)
Handle ManagementRule update

param
rule : ManagementRule being updated configUp : The set of config changes on the rule.

       //pass on to the delegate 
       rulesManager.updateRule(rule, configUp, ctxToUse); 
   
public voidhandleRulesUpdate(com.sun.enterprise.config.ConfigContext cfgCtx, com.sun.enterprise.config.ConfigUpdate configUp)
handle Update for parent node - Management Rules

param
configUpdate for the node

       Set<String> attributesChg = configUp.getAttributeSet();
       for (String attribute : attributesChg) {
           if (attribute.equals(ServerTags.ENABLED)) {
               String newValue = configUp.getNewValue(attribute); 
               if (isServiceEnabled && newValue.equals("false")) {
                   _logger.log(Level.INFO,"sgmt.disableservice");
                   synchronized(this) {
                       isServiceEnabled = false;
                       rulesManager.disableService();
                   }
               } else if (!isServiceEnabled && newValue.equals("true")) {
                   _logger.log(Level.INFO,"sgmt.enableservice");
                   synchronized(this) {
                       isServiceEnabled = true;
                       handleEnableService(cfgCtx);
                   }
               }
           }
       }  
    
booleanisServiceEnabled()

       return isServiceEnabled;
   
public voidonInitialization(com.sun.enterprise.server.ServerContext sc)
Server is initializing subsystems and setting up the runtime environment. Prepare for the beginning of active use of the public methods of this subsystem. This method is called before any of the public methods of this subsystem are utilized.

param
sc ServerContext the server runtime context.
exception
IllegalStateException if this subsystem has already been started
exception
ServerLifecycleException if this subsystem detects a fatal error that prevents this subsystem from being used

        /*
         *would need support from domain.xml management_rules to support
         *service properties.
         */
        final String RULES_MAIL_SMTP_HOST = "mail.smtp.host";
        final String RULES_ALERT_RECIPIENTS = "rules.recipients";
                
        // initialize the Rules Manager  
        rulesManager = new RuleManager(this);
        eventBuilder = EventBuilder.getInstance();
        _logger.log(Level.FINE, "smgt.service_init");
        
        String mailSMTPHost = java.lang.System.getProperty(RULES_MAIL_SMTP_HOST);
        String alertRecip = java.lang.System.getProperty(RULES_ALERT_RECIPIENTS);
        rulesManager.setMailSMTPHost(mailSMTPHost);
        rulesManager.setRecipients(alertRecip);
        serviceInstance = this;

        _logger.log(Level.FINE, "smgt.service_initialized");
    
public voidonReady(com.sun.enterprise.server.ServerContext sc)
Server is starting up applications

param
sc ServerContext the server runtime context.
exception
ServerLifecycleException if this subsystem detects a fatal error that prevents this subsystem from being used

        
                
        try {
            ConfigContext instanceConfigContext = sc.getConfigContext();
            Config instanceConfig = ServerBeansFactory.getConfigBean(
                                                         instanceConfigContext);
            ManagementRules selfManagementRules = instanceConfig.getManagementRules();
       
            if ( selfManagementRules != null ) {
                if ( selfManagementRules.isEnabled() ) {
                    _logger.log(Level.INFO, "smgt.service_enabled");               
                    isServiceEnabled = true;
                    
                    ManagementRule[] rules = selfManagementRules.getManagementRule(); 
                    if (rules != null) {
                        for (int i = 0; i < rules.length; i++) {
                            ManagementRule rule = rules[i];              
                            String ruleName = rule.getName();
                            String ruleDescription = rule.getDescription();
                        
                            //check if rule is enabled then obtain event else skip
                           if (rule.isEnabled()) {
                               // statically read ctx is used
                               addRule(rule,null);
                           } else {
                               _logger.log(Level.FINE, "smgt.service_rule_disabled",
                                       new Object[]{ruleName, ruleDescription});       
                               //addDisabledRule(ruleName, ruleDescription);
                               addDisabledRule(rule);
                           }
                       }
                   } else {
                        _logger.log(Level.FINE,"smgt.service_no_rules_defined"); 
                   }    
                } else {
                      _logger.log(Level.INFO,"smgt.service_disabled");
                }
            } else {
                  _logger.log(Level.INFO, "smgt.service_not_configured");
            }

            //activate this after the admin backend support is achieved 
            AdminEventListenerRegistry.addEventListener(ManagementRuleEvent.eventType,
                                                    new ManagementRuleReconfig());
        } catch (ConfigException ex) {
            //just log, core can still come up, just warn at severe                        
            _logger.log(Level.SEVERE, "smgt.config_error", ex);  
            //throw new ServerLifeCycleException(ex.getMessage(), ex);
        } catch (Exception ex) {
            _logger.log(Level.SEVERE, "sgmt.config_error, ex");
        }
    
public voidonShutdown()
Server is shutting down applications

exception
ServerLifecycleException if this subsystem detects a fatal error that prevents this subsystem from being used

        _logger.log(Level.FINE, "smgt.service_ontermination");
    
public voidonStartup(com.sun.enterprise.server.ServerContext sc)
Server has complted loading the applications and is ready to serve requests.

param
sc ServerContext the server runtime context.
exception
ServerLifecycleException if this subsystem detects a fatal error that prevents this subsystem from being used

        _logger.log(Level.FINE, "smgt.service_onready");

    
public voidonTermination()
Server is terminating the subsystems and the runtime environment. Gracefully terminate the active use of the public methods of this subsystem. This method should be the last one called on a given instance of this subsystem.

exception
ServerLifecycleException if this subsystem detects a fatal error that prevents this subsystem from being used

        _logger.log(Level.FINE, "smgt.service_onshutdown");