From d79f1f6fdc8307aa4038bfb2c7607904b89eedbe Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Mon, 5 Nov 2012 18:17:22 -0800 Subject: [PATCH] Replace Adapters and PluggableServices, use Spring to load them --- .../storage/template/DownloadManagerImpl.java | 2 +- .../cloud/agent/manager/AgentManagerImpl.java | 4 +-- server/src/com/cloud/api/ApiServer.java | 9 +++-- .../com/cloud/cluster/ClusterManagerImpl.java | 11 +++---- .../DefaultComponentLibrary.java | 2 +- .../consoleproxy/ConsoleProxyManagerImpl.java | 17 ++++------ .../cloud/ha/HighAvailabilityManagerImpl.java | 28 +++++++--------- .../hypervisor/HypervisorGuruManagerImpl.java | 20 ++++++----- .../migration/Db21to22MigrationUtil.java | 33 ++++++++----------- .../cloud/server/ManagementServerImpl.java | 14 +++----- .../com/cloud/user/AccountManagerImpl.java | 16 ++++----- .../utils/db/DbAnnotatedBaseDerived.java | 2 +- .../db/TransactionContextBuilderTest.java | 19 ++++++++++- 13 files changed, 87 insertions(+), 90 deletions(-) diff --git a/core/src/com/cloud/storage/template/DownloadManagerImpl.java b/core/src/com/cloud/storage/template/DownloadManagerImpl.java index ab81bed7995..f8b075d4a06 100755 --- a/core/src/com/cloud/storage/template/DownloadManagerImpl.java +++ b/core/src/com/cloud/storage/template/DownloadManagerImpl.java @@ -63,7 +63,7 @@ import com.cloud.utils.NumbersUtil; import com.cloud.utils.component.Adapter; import com.cloud.utils.component.Adapters; import com.cloud.utils.component.ComponentLocator; -import com.cloud.utils.component.ComponentLocator.ComponentInfo; +import com.cloud.utils.component.LegacyComponentLocator.ComponentInfo; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.OutputInterpreter; import com.cloud.utils.script.Script; diff --git a/server/src/com/cloud/agent/manager/AgentManagerImpl.java b/server/src/com/cloud/agent/manager/AgentManagerImpl.java index 1ce99fba0dd..d37bd15cb92 100755 --- a/server/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/server/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -218,7 +218,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { protected int _pingInterval; protected long _pingTimeout; - protected AgentMonitor _monitor = null; + @Inject protected AgentMonitor _monitor = null; protected ExecutorService _executor; @@ -272,7 +272,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager { long lastPing = (System.currentTimeMillis() >> 10) - _pingTimeout; _hostDao.markHostsAsDisconnected(_nodeId, lastPing); - _monitor = ComponentLocator.inject(AgentMonitor.class, _nodeId, _hostDao, _vmDao, _dcDao, _podDao, this, _alertMgr, _pingTimeout); + // _monitor = ComponentLocator.inject(AgentMonitor.class, _nodeId, _hostDao, _vmDao, _dcDao, _podDao, this, _alertMgr, _pingTimeout); registerForHostEvents(_monitor, true, true, false); _executor = new ThreadPoolExecutor(threads, threads, 60l, TimeUnit.SECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("AgentTaskPool")); diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java index eb5e7705a5d..f7786f4d394 100755 --- a/server/src/com/cloud/api/ApiServer.java +++ b/server/src/com/cloud/api/ApiServer.java @@ -50,6 +50,7 @@ import java.util.concurrent.TimeUnit; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import javax.inject.Inject; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -80,6 +81,7 @@ import org.apache.http.protocol.ResponseContent; import org.apache.http.protocol.ResponseDate; import org.apache.http.protocol.ResponseServer; import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; import com.cloud.api.response.ApiResponseSerializer; import com.cloud.api.response.ExceptionResponse; @@ -116,6 +118,7 @@ import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CSExceptionErrorCode; import com.cloud.uuididentity.dao.IdentityDao; +@Component public class ApiServer implements HttpRequestHandler { private static final Logger s_logger = Logger.getLogger(ApiServer.class.getName()); private static final Logger s_accessLogger = Logger.getLogger("apiserver." + ApiServer.class.getName()); @@ -133,6 +136,8 @@ public class ApiServer implements HttpRequestHandler { private AsyncJobManager _asyncMgr = null; private Account _systemAccount = null; private User _systemUser = null; + + @Inject List _pluggableServices; private static int _workerCount = 0; @@ -187,9 +192,7 @@ public class ApiServer implements HttpRequestHandler { private String[] getPluggableServicesApiConfigs() { List pluggableServicesApiConfigs = new ArrayList(); - ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); - List services = locator.getAllPluggableServices(); - for (PluggableService service : services) { + for (PluggableService service : _pluggableServices) { pluggableServicesApiConfigs.add(service.getPropertiesFile()); } return pluggableServicesApiConfigs.toArray(new String[0]); diff --git a/server/src/com/cloud/cluster/ClusterManagerImpl.java b/server/src/com/cloud/cluster/ClusterManagerImpl.java index ff7d6b544f3..9cb78914249 100755 --- a/server/src/com/cloud/cluster/ClusterManagerImpl.java +++ b/server/src/com/cloud/cluster/ClusterManagerImpl.java @@ -121,6 +121,9 @@ public class ClusterManagerImpl implements ClusterManager { private final ExecutorService _executor; private ClusterServiceAdapter _currentServiceAdapter; + + @Inject + private List _serviceAdapters; private ManagementServerHostDao _mshostDao; private ManagementServerHostPeerDao _mshostPeerDao; @@ -1306,14 +1309,10 @@ public class ClusterManagerImpl implements ClusterManager { // notification task itself in turn works as a task dispatcher _executor.execute(getClusterPduNotificationTask()); - Adapters adapters = locator.getAdapters(ClusterServiceAdapter.class); - if (adapters == null || !adapters.isSet()) { + if (_serviceAdapters == null) { throw new ConfigurationException("Unable to get cluster service adapters"); } - Enumeration it = adapters.enumeration(); - if(it.hasMoreElements()) { - _currentServiceAdapter = it.nextElement(); - } + _currentServiceAdapter = _serviceAdapters.get(0); if(_currentServiceAdapter == null) { throw new ConfigurationException("Unable to set current cluster service adapter"); diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java index 4edd4021900..e5482d2b20d 100755 --- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java +++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java @@ -179,7 +179,7 @@ import com.cloud.user.dao.UserStatsLogDaoImpl; import com.cloud.utils.component.Adapter; import com.cloud.utils.component.ComponentLibrary; import com.cloud.utils.component.ComponentLibraryBase; -import com.cloud.utils.component.ComponentLocator.ComponentInfo; +import com.cloud.utils.component.LegacyComponentLocator.ComponentInfo; import com.cloud.utils.component.Manager; import com.cloud.utils.component.PluggableService; import com.cloud.utils.db.GenericDao; diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index 0bf2c9662fe..154204bd459 100755 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -187,7 +187,8 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx private int _mgmt_port = 8250; private String _name; - private Adapters _consoleProxyAllocators; + @Inject + private List _consoleProxyAllocators; @Inject private ConsoleProxyDao _consoleProxyDao; @@ -806,11 +807,10 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx private ConsoleProxyAllocator getCurrentAllocator() { // for now, only one adapter is supported - Enumeration it = _consoleProxyAllocators.enumeration(); - if (it.hasMoreElements()) { - return it.nextElement(); - } - + for(ConsoleProxyAllocator allocator : _consoleProxyAllocators) { + return allocator; + } + return null; } @@ -1512,11 +1512,6 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx value = agentMgrConfigs.get("port"); _mgmt_port = NumbersUtil.parseInt(value, 8250); - _consoleProxyAllocators = locator.getAdapters(ConsoleProxyAllocator.class); - if (_consoleProxyAllocators == null || !_consoleProxyAllocators.isSet()) { - throw new ConfigurationException("Unable to get proxy allocators"); - } - _listener = new ConsoleProxyListener(this); _agentMgr.registerForHostEvents(_listener, true, true, false); diff --git a/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java b/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java index a4ca2cf3b27..31f049f8bdc 100755 --- a/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java +++ b/server/src/com/cloud/ha/HighAvailabilityManagerImpl.java @@ -120,10 +120,11 @@ public class HighAvailabilityManagerImpl implements HighAvailabilityManager, Clu @Inject ClusterDetailsDao _clusterDetailsDao; long _serverId; - @com.cloud.utils.component.Inject(adapter = Investigator.class) - Adapters _investigators; - @com.cloud.utils.component.Inject(adapter = FenceBuilder.class) - Adapters _fenceBuilders; + + @Inject + List _investigators; + @Inject + List _fenceBuilders; @Inject AgentManager _agentMgr; @Inject @@ -164,11 +165,8 @@ public class HighAvailabilityManagerImpl implements HighAvailabilityManager, Clu return null; } - final Enumeration en = _investigators.enumeration(); Status hostState = null; - Investigator investigator = null; - while (en.hasMoreElements()) { - investigator = en.nextElement(); + for(Investigator investigator : _investigators) { hostState = investigator.isAgentAlive(host); if (hostState != null) { if (s_logger.isDebugEnabled()) { @@ -419,22 +417,20 @@ public class HighAvailabilityManagerImpl implements HighAvailabilityManager, Clu return null; } - Enumeration en = _investigators.enumeration(); Investigator investigator = null; - while (en.hasMoreElements()) { - investigator = en.nextElement(); + for(Investigator it : _investigators) { + investigator = it; alive = investigator.isVmAlive(vm, host); s_logger.info(investigator.getName() + " found " + vm + "to be alive? " + alive); if (alive != null) { break; } } + boolean fenced = false; if (alive == null) { s_logger.debug("Fencing off VM that we don't know the state of"); - Enumeration enfb = _fenceBuilders.enumeration(); - while (enfb.hasMoreElements()) { - FenceBuilder fb = enfb.nextElement(); + for(FenceBuilder fb : _fenceBuilders) { Boolean result = fb.fenceOff(vm, host); s_logger.info("Fencer " + fb.getName() + " returned " + result); if (result != null && result) { @@ -442,6 +438,7 @@ public class HighAvailabilityManagerImpl implements HighAvailabilityManager, Clu break; } } + } else if (!alive) { fenced = true; } else { @@ -697,9 +694,6 @@ public class HighAvailabilityManagerImpl implements HighAvailabilityManager, Clu _serverId = ((ManagementServer) ComponentLocator.getComponent(ManagementServer.Name)).getId(); - _investigators = locator.getAdapters(Investigator.class); - _fenceBuilders = locator.getAdapters(FenceBuilder.class); - Map params = new HashMap(); final ConfigurationDao configDao = locator.getDao(ConfigurationDao.class); if (configDao != null) { diff --git a/server/src/com/cloud/hypervisor/HypervisorGuruManagerImpl.java b/server/src/com/cloud/hypervisor/HypervisorGuruManagerImpl.java index 9d7eb3b508c..5a32de744a0 100644 --- a/server/src/com/cloud/hypervisor/HypervisorGuruManagerImpl.java +++ b/server/src/com/cloud/hypervisor/HypervisorGuruManagerImpl.java @@ -17,8 +17,10 @@ package com.cloud.hypervisor; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.annotation.PostConstruct; import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; @@ -42,19 +44,21 @@ public class HypervisorGuruManagerImpl implements HypervisorGuruManager { @Inject HostDao _hostDao; String _name; + + @Inject List _hvGuruList; Map _hvGurus = new HashMap(); @Override public boolean configure(String name, Map params) throws ConfigurationException { _name = name; - ComponentLocator locator = ComponentLocator.getCurrentLocator(); - - Adapters hvGurus = locator.getAdapters(HypervisorGuru.class); - for (HypervisorGuru guru : hvGurus) { - _hvGurus.put(guru.getHypervisorType(), guru); - } - - return true; + return true; + } + + @PostConstruct + public void init() { + for(HypervisorGuru guru : _hvGuruList) { + _hvGurus.put(guru.getHypervisorType(), guru); + } } @Override diff --git a/server/src/com/cloud/migration/Db21to22MigrationUtil.java b/server/src/com/cloud/migration/Db21to22MigrationUtil.java index 7cd6d7ee7ab..b6ff9069ed3 100755 --- a/server/src/com/cloud/migration/Db21to22MigrationUtil.java +++ b/server/src/com/cloud/migration/Db21to22MigrationUtil.java @@ -23,6 +23,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Queue; +import javax.inject.Inject; + import org.apache.log4j.xml.DOMConfigurator; import com.cloud.configuration.Resource; @@ -54,16 +56,17 @@ import com.cloud.vm.dao.InstanceGroupDao; import com.cloud.vm.dao.InstanceGroupVMMapDao; public class Db21to22MigrationUtil { - private ClusterDao _clusterDao; - private HostDao _hostDao; - private AccountDao _accountDao; - private DomainDao _domainDao; - private ResourceCountDao _resourceCountDao; - private InstanceGroupDao _vmGroupDao; - private InstanceGroupVMMapDao _groupVMMapDao; - private ConfigurationDao _configurationDao; - private DataCenterDao _zoneDao; - private ResourceManager _resourceMgr; + + @Inject private ClusterDao _clusterDao; + @Inject private HostDao _hostDao; + @Inject private AccountDao _accountDao; + @Inject private DomainDao _domainDao; + @Inject private ResourceCountDao _resourceCountDao; + @Inject private InstanceGroupDao _vmGroupDao; + @Inject private InstanceGroupVMMapDao _groupVMMapDao; + @Inject private ConfigurationDao _configurationDao; + @Inject private DataCenterDao _zoneDao; + @Inject private ResourceManager _resourceMgr; private void doMigration() { setupComponents(); @@ -171,16 +174,6 @@ public class Db21to22MigrationUtil { } private void setupComponents() { - ComponentLocator locator = ComponentLocator.getLocator("migration", "migration-components.xml", "log4j-cloud.xml"); - - _accountDao = locator.getDao(AccountDao.class); - _domainDao = locator.getDao(DomainDao.class); - _resourceCountDao = locator.getDao(ResourceCountDao.class); - _vmGroupDao = locator.getDao(InstanceGroupDao.class); - _groupVMMapDao = locator.getDao(InstanceGroupVMMapDao.class); - _configurationDao = locator.getDao(ConfigurationDao.class); - _zoneDao = locator.getDao(DataCenterDao.class); - _resourceMgr = locator.getManager(ResourceManager.class); } private void setupInstanceGroups() { diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index e0e1536e62a..5b6c56fff23 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -305,7 +305,9 @@ public class ManagementServerImpl implements ManagementServer { private final SSHKeyPairDao _sshKeyPairDao; private final LoadBalancerDao _loadbalancerDao; private final HypervisorCapabilitiesDao _hypervisorCapabilitiesDao; - private final Adapters _hostAllocators; + + @Inject + private List _hostAllocators; private final ConfigurationManager _configMgr; private final ResourceTagDao _resourceTagDao; @@ -387,11 +389,6 @@ public class ManagementServerImpl implements ManagementServer { _hypervisorCapabilitiesDao = locator.getDao(HypervisorCapabilitiesDao.class); - _hostAllocators = locator.getAdapters(HostAllocator.class); - if (_hostAllocators == null || !_hostAllocators.isSet()) { - s_logger.error("Unable to find HostAllocators"); - } - String value = _configs.get("event.purge.interval"); int cleanup = NumbersUtil.parseInt(value, 60 * 60 * 24); // 1 day. @@ -961,15 +958,14 @@ public class ManagementServerImpl implements ManagementServer { } List suitableHosts = new ArrayList(); - Enumeration enHost = _hostAllocators.enumeration(); VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm); DataCenterDeployment plan = new DataCenterDeployment(srcHost.getDataCenterId(), srcHost.getPodId(), srcHost.getClusterId(), null, null, null); ExcludeList excludes = new ExcludeList(); excludes.addHost(srcHostId); - while (enHost.hasMoreElements()) { - final HostAllocator allocator = enHost.nextElement(); + + for(HostAllocator allocator : _hostAllocators) { suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, HostAllocator.RETURN_UPTO_ALL, false); if (suitableHosts != null && !suitableHosts.isEmpty()) { break; diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index 022eb1ba63d..d0ae74828e6 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -222,7 +222,8 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag @Inject Site2SiteVpnManager _vpnMgr; - private Adapters _userAuthenticators; + @Inject + private List _userAuthenticators; private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("AccountChecker")); @@ -230,8 +231,9 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag UserVO _systemUser; AccountVO _systemAccount; - @com.cloud.utils.component.Inject(adapter = SecurityChecker.class) - Adapters _securityCheckers; + + @Inject + List _securityCheckers; int _cleanupInterval; @Override @@ -258,11 +260,6 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag String value = configs.get(Config.AccountCleanupInterval.key()); _cleanupInterval = NumbersUtil.parseInt(value, 60 * 60 * 24); // 1 day. - _userAuthenticators = locator.getAdapters(UserAuthenticator.class); - if (_userAuthenticators == null || !_userAuthenticators.isSet()) { - s_logger.error("Unable to find an user authenticator."); - } - return true; } @@ -1825,8 +1822,7 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag } boolean authenticated = false; - for (Enumeration en = _userAuthenticators.enumeration(); en.hasMoreElements();) { - UserAuthenticator authenticator = en.nextElement(); + for(UserAuthenticator authenticator : _userAuthenticators) { if (authenticator.authenticate(username, password, domainId, requestParameters)) { authenticated = true; break; diff --git a/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java b/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java index 2b845bc3fc9..38e045c86dc 100644 --- a/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java +++ b/utils/test/com/cloud/utils/db/DbAnnotatedBaseDerived.java @@ -19,7 +19,7 @@ package com.cloud.utils.db; import org.springframework.stereotype.Component; @Component -public class DbAnnotatedBaseDerived { +public class DbAnnotatedBaseDerived extends DbAnnotatedBase { @DB public void DbAnnotatedMethod() { diff --git a/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java b/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java index bf1fbebb750..33e7aa07b2a 100644 --- a/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java +++ b/utils/test/com/cloud/utils/db/TransactionContextBuilderTest.java @@ -16,6 +16,9 @@ // under the License. package com.cloud.utils.db; +import java.util.List; +import java.util.Map; + import javax.inject.Inject; import org.junit.Test; @@ -28,12 +31,15 @@ import com.cloud.utils.component.ComponentContext; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:/com/cloud/utils/db/transactioncontextBuilderTest.xml") public class TransactionContextBuilderTest { + @Inject DbAnnotatedBaseDerived _derived; - @Inject DbAnnotatedBase _base; + @Inject + List _list; + @Test public void test() { // _derived.DbAnnotatedMethod(); @@ -42,5 +48,16 @@ public class TransactionContextBuilderTest { // test @DB injection on dynamically constructed objects DbAnnotatedBase base = ComponentContext.inject(new DbAnnotatedBase()); base.MethodWithClassDbAnnotated(); + +/* + Map components = ComponentContext.getApplicationContext().getBeansOfType(DbAnnotatedBase.class); + for(Map.Entry entry : components.entrySet()) { + System.out.println(entry.getKey()); + entry.getValue().MethodWithClassDbAnnotated(); + } +*/ + for(DbAnnotatedBase entry : _list) { + entry.MethodWithClassDbAnnotated(); + } } }