diff --git a/build/build-cloud.xml b/build/build-cloud.xml index 8cc213bad19..71ad2a05be2 100755 --- a/build/build-cloud.xml +++ b/build/build-cloud.xml @@ -589,6 +589,8 @@ + + diff --git a/build/developer.xml b/build/developer.xml index 058b3ec28f9..f0a622fdce3 100755 --- a/build/developer.xml +++ b/build/developer.xml @@ -214,7 +214,7 @@ - + @@ -224,13 +224,17 @@ - + + + + + @@ -250,12 +254,13 @@ - + + diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java index a94fa8182f9..36f5fabc5b0 100644 --- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java +++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java @@ -18,9 +18,7 @@ package com.cloud.configuration; import java.io.Serializable; -import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -128,9 +126,9 @@ import com.cloud.user.dao.SSHKeyPairDaoImpl; import com.cloud.user.dao.UserAccountDaoImpl; import com.cloud.user.dao.UserDaoImpl; import com.cloud.user.dao.UserStatisticsDaoImpl; -import com.cloud.utils.Pair; 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.Manager; import com.cloud.utils.db.GenericDao; @@ -148,21 +146,7 @@ import com.cloud.vm.dao.UserVmDaoImpl; import com.cloud.vm.dao.UserVmDetailsDaoImpl; import com.cloud.vm.dao.VMInstanceDaoImpl; -public class DefaultComponentLibrary implements ComponentLibrary { - - protected final Map>> _daos = new LinkedHashMap>>(); - - protected ComponentInfo> addDao(String name, Class> clazz) { - return addDao(name, clazz, new ArrayList>(), true); - } - - protected ComponentInfo> addDao(String name, Class> clazz, List> params, boolean singleton) { - ComponentInfo> componentInfo = new ComponentInfo>(name, clazz, params, singleton); - for (String key : componentInfo.getKeys()) { - _daos.put(key, componentInfo); - } - return componentInfo; - } +public class DefaultComponentLibrary extends ComponentLibraryBase implements ComponentLibrary { protected void populateDaos() { addDao("StackMaidDao", StackMaidDaoImpl.class); @@ -263,10 +247,7 @@ public class DefaultComponentLibrary implements ComponentLibrary { addDao("OvsTunnelAccountDao", OvsTunnelAccountDaoImpl.class); addDao("StoragePoolWorkDao", StoragePoolWorkDaoImpl.class); } - - Map> _managers = new HashMap>(); - Map>> _adapters = new HashMap>>(); - + @Override public synchronized Map>> getDaos() { if (_daos.size() == 0) { @@ -275,18 +256,6 @@ public class DefaultComponentLibrary implements ComponentLibrary { return _daos; } - protected ComponentInfo addManager(String name, Class clazz, List> params, boolean singleton) { - ComponentInfo info = new ComponentInfo(name, clazz, params, singleton); - for (String key : info.getKeys()) { - _managers.put(key, info); - } - return info; - } - - protected ComponentInfo addManager(String name, Class clazz) { - return addManager(name, clazz, new ArrayList>(), true); - } - protected void populateManagers() { addManager("StackMaidManager", StackMaidManagerImpl.class); addManager("agent manager", AgentManagerImpl.class); @@ -328,23 +297,6 @@ public class DefaultComponentLibrary implements ComponentLibrary { info.addParameter("consoleproxy.sslEnabled", "true"); } - protected List> addAdapterChain(Class interphace, List>> adapters) { - ArrayList> lst = new ArrayList>(adapters.size()); - for (Pair> adapter : adapters) { - @SuppressWarnings("unchecked") - Class clazz = (Class)adapter.second(); - lst.add(new ComponentInfo(adapter.first(), clazz)); - } - _adapters.put(interphace.getName(), lst); - return lst; - } - - protected void addOneAdapter(Class interphace, String name, Class adapterClass) { - List>> adapters = new ArrayList>>(); - adapters.add(new Pair>(name, adapterClass)); - addAdapterChain(interphace, adapters); - } - @Override public synchronized Map> getManagers() { if (_managers.size() == 0) { @@ -370,5 +322,4 @@ public class DefaultComponentLibrary implements ComponentLibrary { factories.put(EntityManager.class, EntityManagerImpl.class); return factories; } - } diff --git a/server/test/com/cloud/upgrade/dao/VersionDaoImplTest.java b/server/test/com/cloud/upgrade/dao/VersionDaoImplTest.java index e2d358d1b17..d085ef83566 100644 --- a/server/test/com/cloud/upgrade/dao/VersionDaoImplTest.java +++ b/server/test/com/cloud/upgrade/dao/VersionDaoImplTest.java @@ -42,9 +42,6 @@ import com.cloud.utils.db.Transaction; public class VersionDaoImplTest extends TestCase { private static final Logger s_logger = Logger.getLogger(VersionDaoImplTest.class); - /** - * @throws java.lang.Exception - */ @Override @Before public void setUp() throws Exception { @@ -82,12 +79,7 @@ public class VersionDaoImplTest extends TestCase { throw new RuntimeException("Unable to close DB connection", e); } } - - - /** - * @throws java.lang.Exception - */ @Override @After public void tearDown() throws Exception { @@ -96,7 +88,7 @@ public class VersionDaoImplTest extends TestCase { public void test217to22Upgrade() { s_logger.debug("Finding sample data from 2.1.7"); executeScript("VersionDaoImplTest/2.1.7/2.1.7.sample.sql"); - + VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class); String version = dao.getCurrentVersion(); diff --git a/utils/src/com/cloud/utils/component/ComponentLibraryBase.java b/utils/src/com/cloud/utils/component/ComponentLibraryBase.java new file mode 100644 index 00000000000..8f811af8c5b --- /dev/null +++ b/utils/src/com/cloud/utils/component/ComponentLibraryBase.java @@ -0,0 +1,77 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.utils.component; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.cloud.utils.Pair; +import com.cloud.utils.component.ComponentLocator.ComponentInfo; +import com.cloud.utils.db.GenericDao; + +public abstract class ComponentLibraryBase implements ComponentLibrary { + + protected final Map>> _daos = new LinkedHashMap>>(); + + protected ComponentInfo> addDao(String name, Class> clazz) { + return addDao(name, clazz, new ArrayList>(), true); + } + + protected ComponentInfo> addDao(String name, Class> clazz, List> params, boolean singleton) { + ComponentInfo> componentInfo = new ComponentInfo>(name, clazz, params, singleton); + for (String key : componentInfo.getKeys()) { + _daos.put(key, componentInfo); + } + return componentInfo; + } + + protected Map> _managers = new LinkedHashMap>(); + protected Map>> _adapters = new LinkedHashMap>>(); + + protected ComponentInfo addManager(String name, Class clazz, List> params, boolean singleton) { + ComponentInfo info = new ComponentInfo(name, clazz, params, singleton); + for (String key : info.getKeys()) { + _managers.put(key, info); + } + return info; + } + + protected ComponentInfo addManager(String name, Class clazz) { + return addManager(name, clazz, new ArrayList>(), true); + } + + protected List> addAdapterChain(Class interphace, List>> adapters) { + ArrayList> lst = new ArrayList>(adapters.size()); + for (Pair> adapter : adapters) { + @SuppressWarnings("unchecked") + Class clazz = (Class)adapter.second(); + lst.add(new ComponentInfo(adapter.first(), clazz)); + } + _adapters.put(interphace.getName(), lst); + return lst; + } + + protected ComponentInfo addOneAdapter(Class interphace, String name, Class adapterClass) { + List>> adapters = new ArrayList>>(); + adapters.add(new Pair>(name, adapterClass)); + return addAdapterChain(interphace, adapters).get(0); + } +} diff --git a/utils/src/com/cloud/utils/component/ComponentLocator.java b/utils/src/com/cloud/utils/component/ComponentLocator.java index 6743bf7a222..9b4ae31ae38 100755 --- a/utils/src/com/cloud/utils/component/ComponentLocator.java +++ b/utils/src/com/cloud/utils/component/ComponentLocator.java @@ -90,7 +90,7 @@ public class ComponentLocator implements ComponentLocatorMBean { protected HashMap> _adapterMap; protected HashMap> _managerMap; - protected LinkedHashMap>> _daoMap; + protected LinkedHashMap>> _daoMap; protected String _serverName; protected Object _component; protected HashMap, Class> _factories; @@ -101,6 +101,7 @@ public class ComponentLocator implements ComponentLocatorMBean { public ComponentLocator(String server) { _serverName = server; + Runtime.getRuntime().addShutdownHook(new CleanupThread()); } public String getLocatorName() { @@ -615,7 +616,7 @@ public class ComponentLocator implements ComponentLocatorMBean { } @Override - public Map> getAdapters() { + public Map> getAdapterNames() { HashMap> result = new HashMap>(); for (Map.Entry> entry : _adapterMap.entrySet()) { Adapters adapters = entry.getValue(); @@ -632,13 +633,13 @@ public class ComponentLocator implements ComponentLocatorMBean { public Map> getAllAccessibleAdapters() { Map> parentResults = new HashMap>(); - Map> results = getAdapters(); + Map> results = getAdapterNames(); parentResults.putAll(results); return parentResults; } @Override - public Collection getManagers() { + public Collection getManagerNames() { Collection names = new HashSet(); for (Map.Entry> entry : _managerMap.entrySet()) { names.add(entry.getValue().name); @@ -647,7 +648,7 @@ public class ComponentLocator implements ComponentLocatorMBean { } @Override - public Collection getDaos() { + public Collection getDaoNames() { Collection names = new HashSet(); for (Map.Entry>> entry : _daoMap.entrySet()) { names.add(entry.getValue().name); @@ -666,6 +667,20 @@ public class ComponentLocator implements ComponentLocatorMBean { } return new Adapters(key, new ArrayList>()); } + + protected void resetInterceptors(InterceptorLibrary library) { + library.addInterceptors(s_interceptors); + if (s_interceptors.size() > 0) { + s_callbacks = new Callback[s_interceptors.size() + 2]; + int i = 0; + s_callbacks[i++] = NoOp.INSTANCE; + s_callbacks[i++] = new InterceptorDispatcher(); + for (AnnotationInterceptor interceptor : s_interceptors) { + s_callbacks[i++] = interceptor.getCallback(); + } + s_callbackFilter = new InterceptorFilter(); + } + } protected static ComponentLocator getLocatorInternal(String server, boolean setInThreadLocal, String configFileName, String log4jFilename) { synchronized(s_once) { @@ -859,18 +874,8 @@ public class ComponentLocator implements ComponentLocatorMBean { String libraryName = getAttribute(atts, "library"); try { Class libraryClazz = Class.forName(libraryName); - InterceptorLibrary library = (InterceptorLibrary)libraryClazz.newInstance(); - library.addInterceptors(s_interceptors); - if (s_interceptors.size() > 0) { - s_callbacks = new Callback[s_interceptors.size() + 2]; - int i = 0; - s_callbacks[i++] = NoOp.INSTANCE; - s_callbacks[i++] = new InterceptorDispatcher(); - for (AnnotationInterceptor interceptor : s_interceptors) { - s_callbacks[i++] = interceptor.getCallback(); - } - s_callbackFilter = new InterceptorFilter(); - } + InterceptorLibrary library = (InterceptorLibrary)libraryClazz.newInstance(); + resetInterceptors(library); } catch (ClassNotFoundException e) { throw new CloudRuntimeException("Unable to find " + libraryName, e); } catch (InstantiationException e) { diff --git a/utils/src/com/cloud/utils/component/ComponentLocatorMBean.java b/utils/src/com/cloud/utils/component/ComponentLocatorMBean.java index 83cedc06205..72d5c34a3ed 100755 --- a/utils/src/com/cloud/utils/component/ComponentLocatorMBean.java +++ b/utils/src/com/cloud/utils/component/ComponentLocatorMBean.java @@ -29,16 +29,16 @@ public interface ComponentLocatorMBean extends ManagementBean { /** * @return the list of adapters accessible by this component locator. **/ - Map> getAdapters(); + Map> getAdapterNames(); /** * @return the list of managers accessible by this component locator. **/ - Collection getManagers(); + Collection getManagerNames(); /** * @return the list of DAOs accessible by this component locator. */ - Collection getDaos(); + Collection getDaoNames(); } diff --git a/utils/test/com/cloud/utils/component/MockComponentLocator.java b/utils/test/com/cloud/utils/component/MockComponentLocator.java new file mode 100644 index 00000000000..a93abea0b8e --- /dev/null +++ b/utils/test/com/cloud/utils/component/MockComponentLocator.java @@ -0,0 +1,109 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.utils.component; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import net.sf.cglib.proxy.Callback; +import net.sf.cglib.proxy.NoOp; + +import com.cloud.utils.Pair; +import com.cloud.utils.db.DatabaseCallback; +import com.cloud.utils.db.DatabaseCallbackFilter; +import com.cloud.utils.db.GenericDao; + +/** + * MockComponentLocator allows you to define exactly the components you need + * to test your component. This gives you a lot of flexibility in terms of + * defining mock components. + */ +public class MockComponentLocator extends ComponentLocator { + MockComponentLibrary _library = new MockComponentLibrary(); + + public MockComponentLocator(String server) { + super(server); + } + + public ComponentInfo> addDao(String name, Class> dao) { + return _library.addDao(name, dao); + } + + public ComponentInfo addManager(String name, Class manager) { + return _library.addManager(name, manager); + } + + public ComponentInfo addOneAdapter(Class interphace, String name, Class adapterClass) { + return _library.addOneAdapter(interphace, name, adapterClass); + } + + public List> addAdapterChain(Class interphace, List>> adapters) { + return _library.addAdapterChain(interphace, adapters); + } + + @Override + protected Pair>>> parse2(String filename) { + Pair>>> result = new Pair>>>(new XmlHandler("fake"), new HashMap>>()); + _daoMap = new LinkedHashMap>>(); + _managerMap = new LinkedHashMap>(); + _adapterMap = new HashMap>(); + _factories = new HashMap, Class>(); + _daoMap.putAll(_library.getDaos()); + _managerMap.putAll(_library.getManagers()); + result.second().putAll(_library.getAdapters()); + _factories.putAll(_library.getFactories()); + return result; + } + + public void makeActive(InterceptorLibrary interceptors) { + s_singletons.clear(); + s_locators.clear(); + s_factories.clear(); + s_callbacks = new Callback[] { NoOp.INSTANCE, new DatabaseCallback()}; + s_callbackFilter = new DatabaseCallbackFilter(); + s_interceptors.clear(); + resetInterceptors(interceptors); + s_tl.set(this); + parse("fake file"); + } + + protected class MockComponentLibrary extends ComponentLibraryBase implements ComponentLibrary { + @Override + public Map>> getAdapters() { + return _adapters; + } + + @Override + public Map, Class> getFactories() { + return new HashMap, Class>(); + } + + @Override + public Map>> getDaos() { + return _daos; + } + + @Override + public Map> getManagers() { + return _managers; + } + } +}