Return-Path: X-Original-To: apmail-incubator-cloudstack-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-cloudstack-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6448AEECC for ; Fri, 1 Feb 2013 23:27:43 +0000 (UTC) Received: (qmail 69558 invoked by uid 500); 1 Feb 2013 23:27:33 -0000 Delivered-To: apmail-incubator-cloudstack-commits-archive@incubator.apache.org Received: (qmail 69242 invoked by uid 500); 1 Feb 2013 23:27:32 -0000 Mailing-List: contact cloudstack-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cloudstack-dev@incubator.apache.org Delivered-To: mailing list cloudstack-commits@incubator.apache.org Received: (qmail 68622 invoked by uid 99); 1 Feb 2013 23:27:31 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 01 Feb 2013 23:27:31 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id B0C34829159; Fri, 1 Feb 2013 23:27:31 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kelveny@apache.org To: cloudstack-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [23/50] [abbrv] Improve component lifecycle management with system run-level concept Message-Id: <20130201232731.B0C34829159@tyr.zones.apache.org> Date: Fri, 1 Feb 2013 23:27:31 +0000 (UTC) http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/server/test/com/cloud/vpc/MockVpcManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/test/com/cloud/vpc/MockVpcManagerImpl.java b/server/test/com/cloud/vpc/MockVpcManagerImpl.java index d024018..0a44a49 100644 --- a/server/test/com/cloud/vpc/MockVpcManagerImpl.java +++ b/server/test/com/cloud/vpc/MockVpcManagerImpl.java @@ -54,12 +54,13 @@ import com.cloud.user.Account; import com.cloud.user.User; import com.cloud.utils.Pair; import com.cloud.utils.component.Manager; +import com.cloud.utils.component.ManagerBase; import com.cloud.vm.DomainRouterVO; import com.cloud.vpc.dao.MockVpcDaoImpl; @Component @Local(value = { VpcManager.class, VpcService.class }) -public class MockVpcManagerImpl implements VpcManager, Manager{ +public class MockVpcManagerImpl extends ManagerBase implements VpcManager { @Inject MockVpcDaoImpl _vpcDao; /* (non-Javadoc) http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java ---------------------------------------------------------------------- diff --git a/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java b/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java index 0ea29d9..6d8531a 100644 --- a/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java +++ b/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java @@ -47,6 +47,7 @@ import com.cloud.user.Account; import com.cloud.user.User; import com.cloud.uservm.UserVm; import com.cloud.utils.component.Manager; +import com.cloud.utils.component.ManagerBase; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.NicProfile; import com.cloud.vm.VirtualMachineProfile; @@ -54,8 +55,8 @@ import com.cloud.vm.VirtualMachineProfile.Param; @Component @Local(value = {VpcVirtualNetworkApplianceManager.class, VpcVirtualNetworkApplianceService.class}) -public class MockVpcVirtualNetworkApplianceManager implements VpcVirtualNetworkApplianceManager, -VpcVirtualNetworkApplianceService, Manager { +public class MockVpcVirtualNetworkApplianceManager extends ManagerBase implements VpcVirtualNetworkApplianceManager, +VpcVirtualNetworkApplianceService { /* (non-Javadoc) * @see com.cloud.network.router.VirtualNetworkApplianceManager#sendSshKeysToHost(java.lang.Long, java.lang.String, java.lang.String) http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/usage/src/com/cloud/usage/UsageAlertManagerImpl.java ---------------------------------------------------------------------- diff --git a/usage/src/com/cloud/usage/UsageAlertManagerImpl.java b/usage/src/com/cloud/usage/UsageAlertManagerImpl.java index f6e0374..a0765b2 100644 --- a/usage/src/com/cloud/usage/UsageAlertManagerImpl.java +++ b/usage/src/com/cloud/usage/UsageAlertManagerImpl.java @@ -40,6 +40,7 @@ import com.cloud.alert.AlertVO; import com.cloud.alert.dao.AlertDao; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.utils.NumbersUtil; +import com.cloud.utils.component.ManagerBase; import com.sun.mail.smtp.SMTPMessage; import com.sun.mail.smtp.SMTPSSLTransport; @@ -47,18 +48,15 @@ import com.sun.mail.smtp.SMTPTransport; @Component @Local(value={AlertManager.class}) -public class UsageAlertManagerImpl implements AlertManager { +public class UsageAlertManagerImpl extends ManagerBase implements AlertManager { private static final Logger s_logger = Logger.getLogger(UsageAlertManagerImpl.class.getName()); - private String _name = null; private EmailAlert _emailAlert; @Inject private AlertDao _alertDao; @Inject private ConfigurationDao _configDao; @Override public boolean configure(String name, Map params) throws ConfigurationException { - _name = name; - Map configs = _configDao.getConfiguration("management-server", params); // set up the email system for alerts @@ -86,21 +84,6 @@ public class UsageAlertManagerImpl implements AlertManager { } @Override - public String getName() { - return _name; - } - - @Override - public boolean start() { - return true; - } - - @Override - public boolean stop() { - return true; - } - - @Override public void clearAlert(short alertType, long dataCenterId, long podId) { try { if (_emailAlert != null) { http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/usage/src/com/cloud/usage/UsageManagerImpl.java ---------------------------------------------------------------------- diff --git a/usage/src/com/cloud/usage/UsageManagerImpl.java b/usage/src/com/cloud/usage/UsageManagerImpl.java index 438ab72..3e143b1 100644 --- a/usage/src/com/cloud/usage/UsageManagerImpl.java +++ b/usage/src/com/cloud/usage/UsageManagerImpl.java @@ -69,6 +69,7 @@ import com.cloud.user.dao.AccountDao; import com.cloud.user.dao.UserStatisticsDao; +import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.Filter; @@ -78,7 +79,7 @@ import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; @Local(value={UsageManager.class}) -public class UsageManagerImpl implements UsageManager, Runnable { +public class UsageManagerImpl extends ManagerBase implements UsageManager, Runnable { public static final Logger s_logger = Logger.getLogger(UsageManagerImpl.class.getName()); protected static final String DAILY = "DAILY"; @@ -109,7 +110,6 @@ public class UsageManagerImpl implements UsageManager, Runnable { @Inject ConfigurationDao _configDao; private String m_version = null; - private String m_name = null; private final Calendar m_jobExecTime = Calendar.getInstance(); private int m_aggregationDuration = 0; private int m_sanityCheckInterval = 0; @@ -152,8 +152,6 @@ public class UsageManagerImpl implements UsageManager, Runnable { s_logger.info("Implementation Version is " + m_version); } - m_name = name; - Map configs = _configDao.getConfiguration(params); if (params != null) { @@ -222,10 +220,6 @@ public class UsageManagerImpl implements UsageManager, Runnable { return true; } - public String getName() { - return m_name; - } - public boolean start() { if (s_logger.isInfoEnabled()) { s_logger.info("Starting Usage Manager"); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/backoff/impl/ConstantTimeBackoff.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/backoff/impl/ConstantTimeBackoff.java b/utils/src/com/cloud/utils/backoff/impl/ConstantTimeBackoff.java index fb76234..976e369 100755 --- a/utils/src/com/cloud/utils/backoff/impl/ConstantTimeBackoff.java +++ b/utils/src/com/cloud/utils/backoff/impl/ConstantTimeBackoff.java @@ -25,6 +25,7 @@ import javax.ejb.Local; import com.cloud.utils.NumbersUtil; import com.cloud.utils.backoff.BackoffAlgorithm; +import com.cloud.utils.component.AdapterBase; /** * Implementation of the Agent Manager. This class controls the connection @@ -36,10 +37,9 @@ import com.cloud.utils.backoff.BackoffAlgorithm; * } **/ @Local(value={BackoffAlgorithm.class}) -public class ConstantTimeBackoff implements BackoffAlgorithm, ConstantTimeBackoffMBean { +public class ConstantTimeBackoff extends AdapterBase implements BackoffAlgorithm, ConstantTimeBackoffMBean { int _count = 0; long _time; - String _name; ConcurrentHashMap _asleep = new ConcurrentHashMap(); @Override @@ -63,17 +63,11 @@ public class ConstantTimeBackoff implements BackoffAlgorithm, ConstantTimeBackof @Override public boolean configure(String name, Map params) { - _name = name; _time = NumbersUtil.parseLong((String)params.get("seconds"), 5) * 1000; return true; } @Override - public String getName() { - return _name; - } - - @Override public Collection getWaiters() { return _asleep.keySet(); } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/Adapter.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/Adapter.java b/utils/src/com/cloud/utils/component/Adapter.java index 66326df..ec03474 100755 --- a/utils/src/com/cloud/utils/component/Adapter.java +++ b/utils/src/com/cloud/utils/component/Adapter.java @@ -16,47 +16,9 @@ // under the License. package com.cloud.utils.component; -import java.util.Map; - -import javax.naming.ConfigurationException; - /** * Adapter defines methods for pluggable code within the Cloud Stack. An * Adapters are a departure from regular structured programming. */ -public interface Adapter { - - /** - * configure is called when an adapter is initialized. - * - * @param name - * The name of the adapter. - * @param params - * A map of configuration parameters. - * @return Returning false means the configuration did not go well and the - * adapter can not be used. - */ - boolean configure(String name, Map params) throws ConfigurationException; - - /** - * - */ - String getName(); - - /** - * startAdapter() signals the adapter that it can start. - * - * @return true if the adapter can start, false otherwise. - */ - boolean start(); - - /** - * stopAdapter() signals the adapter that it should be shutdown. Returns - * false means that the adapter is not ready to be stopped and should be - * called again. - * - * @return true if the adapter can stop, false indicates the adapter is not - * ready to stop. - */ - boolean stop(); +public interface Adapter extends ComponentLifecycle { } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/AdapterBase.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/AdapterBase.java b/utils/src/com/cloud/utils/component/AdapterBase.java index 29f3c23..405762d 100644 --- a/utils/src/com/cloud/utils/component/AdapterBase.java +++ b/utils/src/com/cloud/utils/component/AdapterBase.java @@ -17,47 +17,18 @@ package com.cloud.utils.component; import java.util.List; -import java.util.Map; - -import javax.naming.ConfigurationException; // Typical Adapter implementation. -public class AdapterBase implements Adapter { - protected String _name; - - @Override - public boolean configure(String name, Map params) throws ConfigurationException { - return true; - } - - @Override - public String getName() { - if(_name == null) - return this.getClass().getSimpleName(); - - return _name; - } - - public void setName(String name) { - _name = name; - } +public class AdapterBase extends ComponentLifecycleBase implements Adapter { - @Override - public boolean start() { - return true; - } - - @Override - public boolean stop() { - return true; - } - - public static T getAdapterByName(List adapters, String name) { + public AdapterBase() { + } + + public static T getAdapterByName(List adapters, String name) { for(T adapter : adapters) { if(adapter.getName() != null && adapter.getName().equalsIgnoreCase(name)) return adapter; } return null; } - } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/ComponentContext.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/ComponentContext.java b/utils/src/com/cloud/utils/component/ComponentContext.java index 9d5e9c4..1b15f00 100644 --- a/utils/src/com/cloud/utils/component/ComponentContext.java +++ b/utils/src/com/cloud/utils/component/ComponentContext.java @@ -17,13 +17,13 @@ package com.cloud.utils.component; -import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; -import java.util.List; import java.util.Map; -import javax.annotation.PostConstruct; +import javax.management.InstanceAlreadyExistsException; +import javax.management.MBeanRegistrationException; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; @@ -38,8 +38,9 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; -import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.TransactionContextBuilder; +import com.cloud.utils.mgmt.JmxUtil; +import com.cloud.utils.mgmt.ManagementBean; /** * @@ -60,93 +61,87 @@ public class ComponentContext implements ApplicationContextAware { public static ApplicationContext getApplicationContext() { return s_appContext; - } + } public static void initComponentsLifeCycle() { - @SuppressWarnings("rawtype") - Collection daos = ComponentContext.getApplicationContext().getBeansOfType(GenericDao.class).values(); - Collection mgrs = ComponentContext.getApplicationContext().getBeansOfType(Manager.class).values(); - Collection adapters = ComponentContext.getApplicationContext().getBeansOfType(Adapter.class).values(); - - Map params = new HashMap(); - for (GenericDao dao : daos) { - try { - s_logger.info("Configuring DAO: " + ComponentContext.getTargetClass(dao).getName()); - dao.configure(dao.getClass().getSimpleName(), params); - } catch (ConfigurationException e) { - s_logger.error("Unable to configure DAO: " + dao.getClass().getSimpleName(), e); - System.exit(1); - } - } - - List avoidMap = new ArrayList(); - for (Manager manager : mgrs) { - if (avoidMap.contains(manager.getName())) { - s_logger.info("Skip manager: " + ComponentContext.getTargetClass(manager).getName() + " as it is already started"); - continue; - } - - try { - s_logger.info("Configuring manager: " + ComponentContext.getTargetClass(manager).getName() + "..."); - manager.configure(manager.getClass().getSimpleName(), params); - avoidMap.add(manager.getName()); - } catch (Exception e) { - s_logger.error("Problems to start manager:" + ComponentContext.getTargetClass(manager).getName(), e); - System.exit(1); - } - } - - for (Adapter adapter : adapters) { - if (avoidMap.contains(adapter.getName())) { - s_logger.info("Skip adapter: " + ComponentContext.getTargetClass(adapter).getName() + " as it is already started"); - continue; - } - - try { - s_logger.info("Configuring adapter: " + ComponentContext.getTargetClass(adapter).getName() + "..."); - adapter.configure(adapter.getName(), params); - avoidMap.add(adapter.getName()); - } catch (Exception e) { - s_logger.error("Problems to start adapter:" + ComponentContext.getTargetClass(adapter).getName(), e); - System.exit(1); - } - } - - avoidMap.clear(); - - for (Manager manager : mgrs) { - if (avoidMap.contains(manager.getName())) { - s_logger.info("Skip start on manager: " + ComponentContext.getTargetClass(manager).getName() + " as it is already started"); - continue; - } - - try { - s_logger.info("Starting manager: " + ComponentContext.getTargetClass(manager).getName() + "..."); - manager.start(); - avoidMap.add(manager.getName()); - } catch (Exception e) { - s_logger.error("Problems to start manager:" + ComponentContext.getTargetClass(manager).getName(), e); - System.exit(1); - } - } - - for (Adapter adapter : adapters) { - if (avoidMap.contains(adapter.getName())) { - s_logger.info("Skip start on adapter: " + ComponentContext.getTargetClass(adapter).getName() + " as it is already started"); - continue; - } - - try { - s_logger.info("Startinging adapter: " + ComponentContext.getTargetClass(adapter).getName() + "..."); - adapter.start(); - avoidMap.add(adapter.getName()); - } catch (Exception e) { - s_logger.error("Problems to start adapter:" + ComponentContext.getTargetClass(adapter).getName(), e); - System.exit(1); - } + Map lifecyleComponents = getApplicationContext().getBeansOfType(ComponentLifecycle.class); + + Map[] classifiedComponents = new Map[ComponentLifecycle.MAX_RUN_LEVELS]; + for(int i = 0; i < ComponentLifecycle.MAX_RUN_LEVELS; i++) { + classifiedComponents[i] = new HashMap(); + } + + for(Map.Entry entry : lifecyleComponents.entrySet()) { + classifiedComponents[entry.getValue().getRunLevel()].put(entry.getKey(), entry.getValue()); + } + + // configuration phase + Map avoidMap = new HashMap(); + for(int i = 0; i < ComponentLifecycle.MAX_RUN_LEVELS; i++) { + for(Map.Entry entry : ((Map)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 entry : ((Map)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); + } + } + } + + static void registerMBean(ManagementBean mbean) { + try { + JmxUtil.registerMBean(mbean); + } catch (MalformedObjectNameException e) { + s_logger.warn("Unable to register MBean: " + mbean.getName(), e); + } catch (InstanceAlreadyExistsException e) { + s_logger.warn("Unable to register MBean: " + mbean.getName(), e); + } catch (MBeanRegistrationException e) { + s_logger.warn("Unable to register MBean: " + mbean.getName(), e); + } catch (NotCompliantMBeanException e) { + s_logger.warn("Unable to register MBean: " + mbean.getName(), e); } + s_logger.info("Registered MBean: " + mbean.getName()); } - + public static T getComponent(String name) { assert(s_appContext != null); return (T)s_appContext.getBean(name); @@ -182,37 +177,6 @@ public class ComponentContext implements ApplicationContextAware { return s_appContext.getBeansOfType(beanType); } - public static boolean isPrimary(Object instance, Class beanType) { - // we assume single line of interface inheritance of beanType - Class componentType = beanType; - Class targetClass = getTargetClass(instance); - - Class interfaces[] = targetClass.getInterfaces(); - for(Class intf : interfaces) { - if(beanType.isAssignableFrom(intf) && intf != beanType) { - componentType = intf; - break; - } - } - - Map matchedTypes = (Map)ComponentContext.getComponentsOfType(componentType); - if(matchedTypes.size() > 1) { - Primary primary = targetClass.getAnnotation(Primary.class); - if(primary != null) { - s_logger.info(targetClass.getName() + " is the primary component of " + componentType.getName()); - return true; - } - - s_logger.warn(targetClass.getName() + " is not the primary component of " + componentType.getName() + ", there are other candidates"); - for(T candidate : matchedTypes.values()) { - s_logger.warn("Candidate " + getTargetClass(candidate).getName()); - } - return false; - } - - return true; - } - public static Class getTargetClass(Object instance) { while(instance instanceof Advised) { try { http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/ComponentLifecycle.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/ComponentLifecycle.java b/utils/src/com/cloud/utils/component/ComponentLifecycle.java new file mode 100644 index 0000000..ea671af --- /dev/null +++ b/utils/src/com/cloud/utils/component/ComponentLifecycle.java @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.component; + +import java.util.Map; + +import javax.naming.ConfigurationException; + +public interface ComponentLifecycle { + public static final int RUN_LEVEL_SYSTEM_BOOTSTRAP = 0; // for system level bootstrap components + public static final int RUN_LEVEL_SYSTEM = 1; // for system level service components (i.e., DAOs) + public static final int RUN_LEVEL_FRAMEWORK_BOOTSTRAP = 2; // for framework startup checkers (i.e., DB migration check) + public static final int RUN_LEVEL_FRAMEWORK = 3; // for framework bootstrap components(i.e., clustering management components) + public static final int RUN_LEVEL_COMPONENT_BOOTSTRAP = 4; // general manager components + public static final int RUN_LEVEL_COMPONENT = 5; // regular adapters, plugin components + public static final int RUN_LEVEL_APPLICATION_MAINLOOP = 6; + public static final int MAX_RUN_LEVELS = 7; + + + String getName(); + void setName(String name); + + void setConfigParams(Map params); + Map getConfigParams(); + + int getRunLevel(); + void setRunLevel(int level); + + public boolean configure(String name, Map params) throws ConfigurationException; + + /** + * Start any background tasks. + * + * @return true if the tasks were started, false otherwise. + */ + public boolean start(); + + /** + * Stop any background tasks. + * + * @return true background tasks were stopped, false otherwise. + */ + public boolean stop(); +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/ComponentLifecycleBase.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/ComponentLifecycleBase.java b/utils/src/com/cloud/utils/component/ComponentLifecycleBase.java new file mode 100644 index 0000000..8c7d09d --- /dev/null +++ b/utils/src/com/cloud/utils/component/ComponentLifecycleBase.java @@ -0,0 +1,80 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.component; + +import java.util.HashMap; +import java.util.Map; + +import javax.naming.ConfigurationException; + +public class ComponentLifecycleBase implements ComponentLifecycle { + + protected String _name; + protected int _runLevel; + protected Map _configParams = new HashMap(); + + public ComponentLifecycleBase() { + _name = this.getClass().getSimpleName(); + _runLevel = RUN_LEVEL_COMPONENT; + } + + @Override + public String getName() { + return _name; + } + + @Override + public void setName(String name) { + _name = name; + } + + @Override + public void setConfigParams(Map params) { + _configParams = params; + } + + @Override + public Map getConfigParams() { + return _configParams; + } + + @Override + public int getRunLevel() { + return _runLevel; + } + + @Override + public void setRunLevel(int level) { + _runLevel = level; + } + + @Override + public boolean configure(String name, Map params) + throws ConfigurationException { + return true; + } + + @Override + public boolean start() { + return true; + } + + @Override + public boolean stop() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/Manager.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/Manager.java b/utils/src/com/cloud/utils/component/Manager.java index da55d06..d53d5ae 100755 --- a/utils/src/com/cloud/utils/component/Manager.java +++ b/utils/src/com/cloud/utils/component/Manager.java @@ -16,45 +16,9 @@ // under the License. package com.cloud.utils.component; -import java.util.Map; - -import javax.naming.ConfigurationException; - /** * * For now we only expose some simple methods. In the future, we can use this **/ -public interface Manager { - /** - * Configuration with parameters. If there are background tasks, they - * shouldn't be started yet. Wait for the start() call. - * - * @param name - * The managers name. - * @param params - * Configuration parameters. - * @return true if the configuration was successful, false otherwise. - */ - public boolean configure(String name, Map params) throws ConfigurationException; - - /** - * Start any background tasks. - * - * @return true if the tasks were started, false otherwise. - */ - public boolean start(); - - /** - * Stop any background tasks. - * - * @return true background tasks were stopped, false otherwise. - */ - public boolean stop(); - - /** - * Get the name of this manager. - * - * @return the name. - */ - public String getName(); +public interface Manager extends ComponentLifecycle { } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/component/ManagerBase.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/component/ManagerBase.java b/utils/src/com/cloud/utils/component/ManagerBase.java new file mode 100644 index 0000000..529ef62 --- /dev/null +++ b/utils/src/com/cloud/utils/component/ManagerBase.java @@ -0,0 +1,24 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.component; + +public class ManagerBase extends ComponentLifecycleBase { + public ManagerBase() { + // set default run level for manager components + setRunLevel(ComponentLifecycle.RUN_LEVEL_COMPONENT_BOOTSTRAP); + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java b/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java index a9c670d..63f841d 100755 --- a/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java +++ b/utils/src/com/cloud/utils/crypt/EncryptionSecretKeyChecker.java @@ -35,11 +35,13 @@ import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; import com.cloud.utils.PropertiesUtil; +import com.cloud.utils.component.AdapterBase; +import com.cloud.utils.component.ComponentLifecycle; import com.cloud.utils.component.SystemIntegrityChecker; import com.cloud.utils.exception.CloudRuntimeException; @Local(value = {SystemIntegrityChecker.class}) -public class EncryptionSecretKeyChecker implements SystemIntegrityChecker { +public class EncryptionSecretKeyChecker extends AdapterBase implements SystemIntegrityChecker { private static final Logger s_logger = Logger.getLogger(EncryptionSecretKeyChecker.class); @@ -47,6 +49,10 @@ public class EncryptionSecretKeyChecker implements SystemIntegrityChecker { private static final String s_envKey = "CLOUD_SECRET_KEY"; private static StandardPBEStringEncryptor s_encryptor = new StandardPBEStringEncryptor(); private static boolean s_useEncryption = false; + + public EncryptionSecretKeyChecker() { + setRunLevel(ComponentLifecycle.RUN_LEVEL_FRAMEWORK_BOOTSTRAP); + } @Override public void check() { @@ -144,4 +150,15 @@ public class EncryptionSecretKeyChecker implements SystemIntegrityChecker { s_encryptor.setConfig(stringConfig); s_useEncryption = true; } + + @Override + public boolean start() { + try { + check(); + } catch (Exception e) { + s_logger.error("System integrity check exception", e); + System.exit(1); + } + return true; + } } http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/17652325/utils/src/com/cloud/utils/db/GenericDaoBase.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/db/GenericDaoBase.java b/utils/src/com/cloud/utils/db/GenericDaoBase.java index 5a166b2..623c349 100755 --- a/utils/src/com/cloud/utils/db/GenericDaoBase.java +++ b/utils/src/com/cloud/utils/db/GenericDaoBase.java @@ -68,6 +68,8 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; import com.cloud.utils.Ternary; +import com.cloud.utils.component.ComponentLifecycle; +import com.cloud.utils.component.ComponentLifecycleBase; import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.SearchCriteria.SelectType; import com.cloud.utils.exception.CloudRuntimeException; @@ -111,7 +113,7 @@ import edu.emory.mathcs.backport.java.util.Collections; * **/ @DB -public abstract class GenericDaoBase implements GenericDao { +public abstract class GenericDaoBase extends ComponentLifecycleBase implements GenericDao { private final static Logger s_logger = Logger.getLogger(GenericDaoBase.class); protected final static TimeZone s_gmtTimeZone = TimeZone.getTimeZone("GMT"); @@ -157,9 +159,6 @@ public abstract class GenericDaoBase implements Gene protected static final SequenceFetcher s_seqFetcher = SequenceFetcher.getInstance(); - protected String _name; - protected Map _configParams = new HashMap(); - public static GenericDaoBase getDao(Class entityType) { @SuppressWarnings("unchecked") GenericDaoBase dao = (GenericDaoBase)s_daoMaps.get(entityType); @@ -181,14 +180,6 @@ public abstract class GenericDaoBase implements Gene return _allAttributes; } - public void setConfigParams(Map params) { - _configParams = params; - } - - public Map getConfigParams() { - return _configParams; - } - @SuppressWarnings("unchecked") protected GenericDaoBase() { Type t = getClass().getGenericSuperclass(); @@ -278,6 +269,8 @@ public abstract class GenericDaoBase implements Gene s_logger.trace(info.selectSql); } } + + setRunLevel(ComponentLifecycle.RUN_LEVEL_SYSTEM); } @Override @DB(txn=false) @@ -1767,11 +1760,6 @@ public abstract class GenericDaoBase implements Gene } @DB(txn=false) - public String getName() { - return _name; - } - - @DB(txn=false) public static UpdateBuilder getUpdateBuilder(final T entityObject) { final Factory factory = (Factory)entityObject; assert(factory != null); @@ -1880,5 +1868,4 @@ public abstract class GenericDaoBase implements Gene return sql; } - }