Search in sources :

Example 1 with ManagementBean

use of com.cloud.utils.mgmt.ManagementBean in project cloudstack by apache.

the class ComponentContext method initComponentsLifeCycle.

public static void initComponentsLifeCycle() {
    if (!s_initializeBeans)
        return;
    AutowireCapableBeanFactory beanFactory = s_appContext.getAutowireCapableBeanFactory();
    Map<String, ComponentMethodInterceptable> interceptableComponents = getApplicationContext().getBeansOfType(ComponentMethodInterceptable.class);
    for (Map.Entry<String, ComponentMethodInterceptable> entry : interceptableComponents.entrySet()) {
        Object bean = getTargetObject(entry.getValue());
        beanFactory.configureBean(bean, entry.getKey());
    }
    Map<String, ComponentLifecycle> lifecycleComponents = getApplicationContext().getBeansOfType(ComponentLifecycle.class);
    Map<String, ComponentLifecycle>[] classifiedComponents = new Map[ComponentLifecycle.MAX_RUN_LEVELS];
    for (int i = 0; i < ComponentLifecycle.MAX_RUN_LEVELS; i++) {
        classifiedComponents[i] = new HashMap<String, ComponentLifecycle>();
    }
    for (Map.Entry<String, ComponentLifecycle> entry : lifecycleComponents.entrySet()) {
        classifiedComponents[entry.getValue().getRunLevel()].put(entry.getKey(), entry.getValue());
    }
    // Run the SystemIntegrityCheckers first
    Map<String, SystemIntegrityChecker> integrityCheckers = getApplicationContext().getBeansOfType(SystemIntegrityChecker.class);
    for (Entry<String, SystemIntegrityChecker> entry : integrityCheckers.entrySet()) {
        s_logger.info("Running SystemIntegrityChecker " + entry.getKey());
        try {
            entry.getValue().check();
        } catch (Throwable e) {
            s_logger.error("System integrity check failed. Refuse to startup", e);
            System.exit(1);
        }
    }
    // configuration phase
    Map<String, String> avoidMap = new HashMap<String, String>();
    for (int i = 0; i < ComponentLifecycle.MAX_RUN_LEVELS; i++) {
        for (Map.Entry<String, ComponentLifecycle> entry : classifiedComponents[i].entrySet()) {
            ComponentLifecycle component = entry.getValue();
            String implClassName = ComponentContext.getTargetClass(component).getName();
            s_logger.info("Configuring " + implClassName);
            if (avoidMap.containsKey(implClassName)) {
                s_logger.info("Skip configuration of " + implClassName + " as it is already configured");
                continue;
            }
            try {
                component.configure(component.getName(), component.getConfigParams());
            } catch (ConfigurationException e) {
                s_logger.error("Unhandled exception", e);
                throw new RuntimeException("Unable to configure " + implClassName, e);
            }
            avoidMap.put(implClassName, implClassName);
        }
    }
    // starting phase
    avoidMap.clear();
    for (int i = 0; i < ComponentLifecycle.MAX_RUN_LEVELS; i++) {
        for (Map.Entry<String, ComponentLifecycle> entry : classifiedComponents[i].entrySet()) {
            ComponentLifecycle component = entry.getValue();
            String implClassName = ComponentContext.getTargetClass(component).getName();
            s_logger.info("Starting " + implClassName);
            if (avoidMap.containsKey(implClassName)) {
                s_logger.info("Skip configuration of " + implClassName + " as it is already configured");
                continue;
            }
            try {
                component.start();
                if (getTargetObject(component) instanceof ManagementBean)
                    registerMBean((ManagementBean) getTargetObject(component));
            } catch (Exception e) {
                s_logger.error("Unhandled exception", e);
                throw new RuntimeException("Unable to start " + implClassName, e);
            }
            avoidMap.put(implClassName, implClassName);
        }
    }
}
Also used : ManagementBean(com.cloud.utils.mgmt.ManagementBean) HashMap(java.util.HashMap) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) ConfigurationException(javax.naming.ConfigurationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) ConfigurationException(javax.naming.ConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ManagementBean (com.cloud.utils.mgmt.ManagementBean)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 ConfigurationException (javax.naming.ConfigurationException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)1