Mercurial > hg > thermostat-ng > agent
changeset 2704:54b6408e08b6
Migrating vm-memory plugin to declarative services
Reviewed-by: jerboaa, jmatsuok
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023677.html
line wrap: on
line diff
--- a/plugins/vm-memory/agent/pom.xml Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/agent/pom.xml Fri Jun 23 10:25:42 2017 -0400 @@ -56,7 +56,6 @@ <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> <Bundle-SymbolicName>com.redhat.thermostat.vm.memory.agent</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.vm.memory.agent.internal.Activator</Bundle-Activator> <Export-Package /> <Private-Package> com.redhat.thermostat.vm.memory.agent.internal @@ -66,6 +65,18 @@ </instructions> </configuration> </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-scrdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> <dependencies> @@ -120,5 +131,10 @@ <version>${project.version}</version> <scope>test</scope> </dependency> + <!-- declarative services --> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> </dependencies> </project>
--- a/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/Activator.java Fri Jun 23 15:41:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.vm.memory.agent.internal; - -import com.redhat.thermostat.jvm.overview.agent.VmListenerBackend; -import com.redhat.thermostat.jvm.overview.agent.VmStatusListenerRegistrar; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -import com.redhat.thermostat.backend.Backend; -import com.redhat.thermostat.backend.BackendService; -import com.redhat.thermostat.common.MultipleServiceTracker; -import com.redhat.thermostat.common.MultipleServiceTracker.Action; -import com.redhat.thermostat.common.MultipleServiceTracker.DependencyProvider; -import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; -import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; - -public class Activator implements BundleActivator { - - private MultipleServiceTracker tracker; - private VmListenerBackend backend; - private ServiceRegistration<Backend> reg; - - @Override - public void start(final BundleContext context) throws Exception { - - final VmStatusListenerRegistrar registrar = new VmStatusListenerRegistrar(context); - - Class<?>[] deps = new Class<?>[] { - BackendService.class, - VmMemoryStatDAO.class, - VmTlabStatDAO.class, - WriterID.class, // vm memory backend uses it - }; - tracker = new MultipleServiceTracker(context, deps, new Action() { - - @Override - public void dependenciesAvailable(DependencyProvider services) { - VmMemoryStatDAO vmMemoryStatDao = services.get(VmMemoryStatDAO.class); - VmTlabStatDAO vmTlabStatDao = services.get(VmTlabStatDAO.class); - Version version = new Version(context.getBundle()); - WriterID writerId = services.get(WriterID.class); - backend = new VmMemoryBackend(vmMemoryStatDao, vmTlabStatDao, version, registrar, writerId); - reg = context.registerService(Backend.class, backend, null); - } - - @Override - public void dependenciesUnavailable() { - if (backend.isActive()) { - backend.deactivate(); - } - reg.unregister(); - } - }); - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - tracker.close(); - } - - /* - * For testing purposes only. - */ - VmListenerBackend getBackend() { - return backend; - } -} -
--- a/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/VmMemoryBackend.java Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/VmMemoryBackend.java Fri Jun 23 10:25:42 2017 -0400 @@ -36,6 +36,14 @@ package com.redhat.thermostat.vm.memory.agent.internal; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.osgi.framework.BundleContext; + +import com.redhat.thermostat.backend.Backend; import com.redhat.thermostat.common.Version; import com.redhat.thermostat.jvm.overview.agent.VmListenerBackend; import com.redhat.thermostat.jvm.overview.agent.VmStatusListenerRegistrar; @@ -45,25 +53,59 @@ import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; +@Component +@Service(value = Backend.class) public class VmMemoryBackend extends VmListenerBackend { - private final VmMemoryStatDAO vmMemoryStats; - private final VmTlabStatDAO tlabStats; - - public VmMemoryBackend(VmMemoryStatDAO vmMemoryStatDAO, - VmTlabStatDAO vmTlabStatDAO, - Version version, - VmStatusListenerRegistrar registrar, - WriterID writerId) { - super("VM Memory Backend", - "Gathers memory statistics about a JVM", - "Red Hat, Inc.", - true); - this.vmMemoryStats = vmMemoryStatDAO; - this.tlabStats = vmTlabStatDAO; + private final ListenerCreator listenerCreator; + + @Reference + private VmMemoryStatDAO vmMemoryStatDAO; + + @Reference + private VmTlabStatDAO vmTlabStatDAO; + + @Reference + private WriterID writerId; + + public VmMemoryBackend() { + this(new ListenerCreator()); + } + + public VmMemoryBackend(ListenerCreator listenerCreator) { + super("VM Memory Backend", "Gathers memory statistics about a JVM", "Red Hat, Inc.", true); + this.listenerCreator = listenerCreator; + } + + @Activate + protected void componentActivated(BundleContext context) { + VmStatusListenerRegistrar registrar = new VmStatusListenerRegistrar(context); + Version version = new Version(context.getBundle()); initialize(writerId, registrar, version.getVersionNumber()); } + @Deactivate + protected void componentDeactivated() { + if (isActive()) { + deactivate(); + } + } + + // DS bind method + protected void bindVmMemoryStatDAO(VmMemoryStatDAO dao) { + this.vmMemoryStatDAO = dao; + } + + // DS bind method + protected void bindVmTlabStatDAO(VmTlabStatDAO vmTlabStatDAO) { + this.vmTlabStatDAO = vmTlabStatDAO; + } + + // DS bind method + protected void bindWriterId(WriterID id) { + this.writerId = id; + } + @Override public int getOrderValue() { return Constants.ORDER; @@ -71,8 +113,13 @@ @Override protected VmUpdateListener createVmListener(String writerId, String vmId, int pid) { - return new VmMemoryVmListener(writerId, vmMemoryStats, tlabStats, vmId); + return listenerCreator.create(writerId, vmMemoryStatDAO, vmTlabStatDAO, vmId); } - + + static class ListenerCreator { + VmMemoryVmListener create(String writerId, VmMemoryStatDAO dao, VmTlabStatDAO tlabDao, String vmId) { + return new VmMemoryVmListener(writerId, dao, tlabDao, vmId); + } + } }
--- a/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/ActivatorTest.java Fri Jun 23 15:41:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.vm.memory.agent.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.redhat.thermostat.jvm.overview.agent.VmListenerBackend; -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.Version; - -import com.redhat.thermostat.backend.Backend; -import com.redhat.thermostat.backend.BackendService; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.testutils.StubBundleContext; -import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; -import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; - -public class ActivatorTest { - - @Test - public void verifyActivatorDoesNotRegisterServiceOnMissingDeps() throws Exception { - StubBundleContext context = new StubBundleContext(); - - Activator activator = new Activator(); - - activator.start(context); - - assertEquals(0, context.getAllServices().size()); - assertEquals(4, context.getServiceListeners().size()); - - activator.stop(context); - } - - @Test - public void verifyActivatorRegistersServices() throws Exception { - StubBundleContext context = new StubBundleContext() { - @Override - public Bundle getBundle() { - Bundle result = mock(Bundle.class); - when(result.getVersion()).thenReturn(Version.emptyVersion); - return result; - } - }; - - BackendService service = mock(BackendService.class); - VmMemoryStatDAO vmMemoryStatDAO = mock(VmMemoryStatDAO.class); - VmTlabStatDAO vmTlabStatDAO = mock(VmTlabStatDAO.class); - WriterID id = mock(WriterID.class); - - context.registerService(BackendService.class, service, null); - context.registerService(VmMemoryStatDAO.class, vmMemoryStatDAO, null); - context.registerService(VmTlabStatDAO.class, vmTlabStatDAO, null); - - context.registerService(WriterID.class, id, null); - - Activator activator = new Activator(); - - activator.start(context); - - assertTrue(context.isServiceRegistered(Backend.class.getName(), VmMemoryBackend.class)); - VmListenerBackend backend = activator.getBackend(); - assertNotNull(backend); - - // core thermostat activates the backend when the backend is detected - // do it manually for the test - backend.activate(); - - activator.stop(context); - - assertFalse(backend.isActive()); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(4, context.getAllServices().size()); - } - -} -
--- a/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/VmMemoryBackendTest.java Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/VmMemoryBackendTest.java Fri Jun 23 10:25:42 2017 -0400 @@ -36,42 +36,121 @@ package com.redhat.thermostat.vm.memory.agent.internal; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.redhat.thermostat.common.Ordered; import com.redhat.thermostat.jvm.overview.agent.VmStatusListenerRegistrar; -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.Ordered; -import com.redhat.thermostat.common.Version; +import com.redhat.thermostat.storage.core.WriterID; +import com.redhat.thermostat.vm.memory.agent.internal.VmMemoryBackend.ListenerCreator; import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; +import org.junit.BeforeClass; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Version; public class VmMemoryBackendTest { - - private VmMemoryBackend backend; + + private static ListenerCreator listenerCreator; + private static WriterID id; + private static Version version; + + @BeforeClass + public static void setup() { + listenerCreator = mock(ListenerCreator.class); + id = mock(WriterID.class); + version = new Version(1, 2, 3); + } + + @Test + public void testComponentActivated() { + TestVmMemoryBackend backend = new TestVmMemoryBackend(listenerCreator); + + BundleContext context = mock(BundleContext.class); + Bundle bundle = mock(Bundle.class); + when(bundle.getVersion()).thenReturn(version); + when(context.getBundle()).thenReturn(bundle); + backend.bindWriterId(id); + + assertFalse(backend.wasInitializeCalled); + backend.componentActivated(context); + assertTrue(backend.wasInitializeCalled); + verify(context).getBundle(); + + assertEquals(version.toString(), backend.getVersion()); + assertEquals(id, backend.writerId); + assertNotNull(backend.registrar); + } - @Before - public void setup() { - VmMemoryStatDAO vmMemoryStatDao = mock(VmMemoryStatDAO.class); - VmTlabStatDAO vmTlabStatDao = mock(VmTlabStatDAO.class); - - Version version = mock(Version.class); - when(version.getVersionNumber()).thenReturn("0.0.0"); + @Test + public void testComponentActivateAndDeactivate() { + TestVmMemoryBackend backend = new TestVmMemoryBackend(listenerCreator); + VmStatusListenerRegistrar registrar = mock(VmStatusListenerRegistrar.class); + backend.initialize(id, registrar, version.toString()); + + assertFalse(backend.isActive()); + verify(registrar, times(0)).register(backend); + + assertTrue(backend.activate()); + + assertTrue(backend.isActive()); + verify(registrar).register(backend); + verify(registrar, times(0)).unregister(backend); + + backend.componentDeactivated(); - VmStatusListenerRegistrar registrar = mock(VmStatusListenerRegistrar.class); - - backend = new VmMemoryBackend(vmMemoryStatDao, vmTlabStatDao, version, registrar, null); + assertFalse(backend.isActive()); + verify(registrar).unregister(backend); + } + + @Test + public void testCreateVmListener() { + final String writerId = "myAgent"; + final String vmId = "myJVM"; + final int pid = 1234; + + TestVmMemoryBackend backend = new TestVmMemoryBackend(listenerCreator); + VmMemoryStatDAO dao = mock(VmMemoryStatDAO.class); + VmTlabStatDAO tlabDao = mock(VmTlabStatDAO.class); + backend.bindVmMemoryStatDAO(dao); + backend.bindVmTlabStatDAO(tlabDao); + backend.createVmListener(writerId, vmId, pid); + + verify(listenerCreator).create(writerId, dao, tlabDao, vmId); } @Test public void testOrderValue() { + TestVmMemoryBackend backend = new TestVmMemoryBackend(listenerCreator); int orderValue = backend.getOrderValue(); - assertTrue(orderValue >= Ordered.ORDER_MEMORY_GROUP); assertTrue(orderValue < Ordered.ORDER_NETWORK_GROUP); } + + static class TestVmMemoryBackend extends VmMemoryBackend { + + VmStatusListenerRegistrar registrar; + WriterID writerId; + boolean wasInitializeCalled = false; + + TestVmMemoryBackend(ListenerCreator creator) { + super(creator); + } + + @Override + protected void initialize(WriterID id, VmStatusListenerRegistrar registrar, String version) { + super.initialize(id, registrar, version); + this.wasInitializeCalled = true; + this.registrar = registrar; + this.writerId = id; + } + } } -
--- a/plugins/vm-memory/common/pom.xml Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/common/pom.xml Fri Jun 23 10:25:42 2017 -0400 @@ -72,7 +72,6 @@ <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> <Bundle-SymbolicName>com.redhat.thermostat.vm.memory.common</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.vm.memory.common.internal.Activator</Bundle-Activator> <Export-Package> com.redhat.thermostat.vm.memory.common, com.redhat.thermostat.vm.memory.common.model @@ -85,6 +84,18 @@ </instructions> </configuration> </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-scrdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> <dependencies>
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/Activator.java Fri Jun 23 15:41:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,118 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.vm.memory.common.internal; - -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.util.tracker.ServiceTracker; - -import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; -import com.redhat.thermostat.common.plugins.PluginConfiguration; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; -import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; - -public class Activator implements BundleActivator { - - private static final Logger logger = LoggingUtils.getLogger(Activator.class); - private static final String PLUGIN_ID = "vm-memory"; - - private final DAOCreator creator; - private ServiceTracker tracker; - private ServiceRegistration memoryReg; - private ServiceRegistration tlabReg; - - public Activator() { - this(new DAOCreator()); - } - - Activator(DAOCreator creator) { - this.creator = creator; - } - - @Override - public void start(BundleContext context) throws Exception { - tracker = new ServiceTracker(context, ConfigurationInfoSource.class.getName(), null) { - @Override - public Object addingService(ServiceReference reference) { - ConfigurationInfoSource source = (ConfigurationInfoSource) super.addingService(reference); - PluginConfiguration config = new PluginConfiguration(source, PLUGIN_ID); - try { - VmMemoryStatDAO vmMemoryStatDao = creator.createMemoryStatDAO(config); - memoryReg = context.registerService(VmMemoryStatDAO.class.getName(), vmMemoryStatDao, null); - VmTlabStatDAO vmTlabStatDao = creator.createTlabStatDAO(config); - tlabReg = context.registerService(VmTlabStatDAO.class.getName(), vmTlabStatDao, null); - } catch (Exception e) { - logger.log(Level.SEVERE, "Failed to create DAOs", e); - } - return source; - } - @Override - public void removedService(ServiceReference reference, Object service) { - if (memoryReg != null) { - memoryReg.unregister(); - } - if (tlabReg != null) { - tlabReg.unregister(); - } - super.removedService(reference, service); - } - }; - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - tracker.close(); - } - - static class DAOCreator { - VmMemoryStatDAO createMemoryStatDAO(PluginConfiguration config) throws Exception { - return new VmMemoryStatDAOImpl(config); - } - VmTlabStatDAO createTlabStatDAO(PluginConfiguration config) throws Exception { - return new VmTlabStatDAOImpl(config); - } - } - -} -
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImpl.java Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImpl.java Fri Jun 23 10:25:42 2017 -0400 @@ -44,10 +44,16 @@ import java.util.logging.Level; import java.util.logging.Logger; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; import com.redhat.thermostat.common.plugins.PluginConfiguration; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; @@ -55,28 +61,44 @@ import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; -class VmMemoryStatDAOImpl implements VmMemoryStatDAO { +@Component +@Service(value = VmMemoryStatDAO.class) +public class VmMemoryStatDAOImpl implements VmMemoryStatDAO { private static final Logger logger = LoggingUtils.getLogger(VmMemoryStatDAOImpl.class); + private static final String PLUGIN_ID = "vm-memory"; private static final String CONTENT_TYPE = "application/json"; - private final String gatewayURL; private final HttpClient client; private final HttpHelper httpHelper; private final JsonHelper jsonHelper; + private final ConfigurationCreator configCreator; - VmMemoryStatDAOImpl(PluginConfiguration config) throws Exception { - this(config, new HttpClient(), new HttpHelper(), new JsonHelper(new VmMemoryStatTypeAdapter())); + private String gatewayURL; + + @Reference + private ConfigurationInfoSource configInfoSource; + + public VmMemoryStatDAOImpl() { + this(new HttpClient(), new JsonHelper(new VmMemoryStatTypeAdapter()), new HttpHelper(), + new ConfigurationCreator(), null); } - VmMemoryStatDAOImpl(PluginConfiguration config, HttpClient client, HttpHelper httpHelper, - JsonHelper jsonHelper) throws Exception { - this.gatewayURL = config.getGatewayURL(); + VmMemoryStatDAOImpl(HttpClient client, JsonHelper jh, HttpHelper hh, ConfigurationCreator creator, + ConfigurationInfoSource source) { this.client = client; - this.httpHelper = httpHelper; - this.jsonHelper = jsonHelper; + this.httpHelper = hh; + this.jsonHelper = jh; + this.configCreator = creator; + this.configInfoSource = source; + } - this.httpHelper.startClient(this.client); + @Activate + void activate() throws Exception { + PluginConfiguration config = configCreator.create(configInfoSource); + this.gatewayURL = config.getGatewayURL(); + + httpHelper.startClient(client); } @Override @@ -133,5 +155,14 @@ } } + + // For Testing purposes + static class ConfigurationCreator { + + PluginConfiguration create(ConfigurationInfoSource source) { + return new PluginConfiguration(source, PLUGIN_ID); + } + + } }
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatDAOImpl.java Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatDAOImpl.java Fri Jun 23 10:25:42 2017 -0400 @@ -37,50 +37,66 @@ package com.redhat.thermostat.vm.memory.common.internal; import java.io.IOException; -import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; -import java.util.logging.Level; import java.util.logging.Logger; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; import com.redhat.thermostat.common.plugins.PluginConfiguration; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; import com.redhat.thermostat.vm.memory.common.model.VmTlabStat; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.util.StringContentProvider; -import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; -class VmTlabStatDAOImpl implements VmTlabStatDAO { +@Component +@Service(value = VmTlabStatDAO.class) +public class VmTlabStatDAOImpl implements VmTlabStatDAO { private static final Logger logger = LoggingUtils.getLogger(VmTlabStatDAOImpl.class); + private static final String PLUGIN_ID = "vm-memory"; private static final String CONTENT_TYPE = "application/json"; - private final String gatewayURL; private final HttpClient client; private final HttpHelper httpHelper; private final JsonHelper jsonHelper; + private final ConfigurationCreator configurationCreator; - VmTlabStatDAOImpl(PluginConfiguration config) throws Exception { - this(config, new HttpClient(), new HttpHelper(), new JsonHelper(new VmTlabStatTypeAdapter())); + private String gatewayURL; + + @Reference + private ConfigurationInfoSource configInfoSource; + + public VmTlabStatDAOImpl() throws Exception { + this(new HttpClient(), new HttpHelper(), new JsonHelper(new VmTlabStatTypeAdapter()), new ConfigurationCreator(), null); } - VmTlabStatDAOImpl(PluginConfiguration config, HttpClient client, HttpHelper httpHelper, - JsonHelper jsonHelper) throws Exception { - this.gatewayURL = config.getGatewayURL(); + VmTlabStatDAOImpl(HttpClient client, HttpHelper httpHelper, JsonHelper jsonHelper, + ConfigurationCreator configurationCreator, ConfigurationInfoSource configInfoSource) throws Exception { this.client = client; this.httpHelper = httpHelper; this.jsonHelper = jsonHelper; + this.configurationCreator = configurationCreator; + this.configInfoSource = configInfoSource; + } + + @Activate + void activate() throws Exception { + PluginConfiguration config = configurationCreator.create(configInfoSource); + this.gatewayURL = config.getGatewayURL(); this.httpHelper.startClient(this.client); } - @Override public void putStat(final VmTlabStat stat) { // TODO: Re-enable when web-gateway service for TLAB stats is available @@ -137,5 +153,13 @@ } } + + // For testing purposes + static class ConfigurationCreator { + + PluginConfiguration create(ConfigurationInfoSource source) { + return new PluginConfiguration(source, PLUGIN_ID); + } + } }
--- a/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/ActivatorTest.java Fri Jun 23 15:41:47 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat 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 2, or (at your - * option) any later version. - * - * Thermostat 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 Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.vm.memory.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.Test; - -import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; -import com.redhat.thermostat.common.plugins.PluginConfiguration; -import com.redhat.thermostat.testutils.StubBundleContext; -import com.redhat.thermostat.vm.memory.common.internal.Activator.DAOCreator; - -public class ActivatorTest { - - @Test - public void verifyActivatorRegistersServices() throws Exception { - DAOCreator creator = mock(DAOCreator.class); - VmMemoryStatDAOImpl memoryDao = mock(VmMemoryStatDAOImpl.class); - VmTlabStatDAOImpl tlabDao = mock(VmTlabStatDAOImpl.class); - when(creator.createMemoryStatDAO(any(PluginConfiguration.class))).thenReturn(memoryDao); - when(creator.createTlabStatDAO(any(PluginConfiguration.class))).thenReturn(tlabDao); - - ConfigurationInfoSource source = mock(ConfigurationInfoSource.class); - StubBundleContext context = new StubBundleContext(); - context.registerService(ConfigurationInfoSource.class.getName(), source, null); - - Activator activator = new Activator(creator); - - activator.start(context); - - assertEquals(3, context.getAllServices().size()); - - activator.stop(context); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(1, context.getAllServices().size()); - } - -} -
--- a/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImplTest.java Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImplTest.java Fri Jun 23 10:25:42 2017 -0400 @@ -36,8 +36,6 @@ package com.redhat.thermostat.vm.memory.common.internal; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyListOf; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; @@ -46,7 +44,6 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; import org.eclipse.jetty.client.HttpClient; @@ -58,9 +55,8 @@ import org.junit.Before; import org.junit.Test; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; import com.redhat.thermostat.common.plugins.PluginConfiguration; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; import com.redhat.thermostat.vm.memory.common.internal.VmMemoryStatDAOImpl.HttpHelper; import com.redhat.thermostat.vm.memory.common.internal.VmMemoryStatDAOImpl.JsonHelper; import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat; @@ -80,6 +76,8 @@ private Request request; private ContentResponse response; private PluginConfiguration config; + VmMemoryStatDAOImpl.ConfigurationCreator creator; + ConfigurationInfoSource source; @Before public void setUp() throws Exception { @@ -98,6 +96,11 @@ config = mock(PluginConfiguration.class); when(config.getGatewayURL()).thenReturn(GATEWAY_URL); + + source = mock(ConfigurationInfoSource.class); + + creator = mock(VmMemoryStatDAOImpl.ConfigurationCreator.class); + when(creator.create(source)).thenReturn(config); } @Test @@ -132,8 +135,9 @@ } VmMemoryStat stat = new VmMemoryStat("foo-agent", 1, "vmId", generations.toArray(new Generation[generations.size()]), 2, 3, 4, 5); - - VmMemoryStatDAO dao = new VmMemoryStatDAOImpl(config, httpClient, httpHelper, jsonHelper); + + VmMemoryStatDAOImpl dao = new VmMemoryStatDAOImpl(httpClient, jsonHelper, httpHelper, creator, source); + dao.activate(); dao.putVmMemoryStat(stat); verify(httpClient).newRequest(GATEWAY_URL);
--- a/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatDAOTest.java Fri Jun 23 15:41:47 2017 +0200 +++ b/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmTlabStatDAOTest.java Fri Jun 23 10:25:42 2017 -0400 @@ -36,6 +36,18 @@ package com.redhat.thermostat.vm.memory.common.internal; +import static com.redhat.thermostat.vm.memory.common.internal.VmTlabStatDAOImpl.HttpHelper; +import static com.redhat.thermostat.vm.memory.common.internal.VmTlabStatDAOImpl.JsonHelper; +import static org.mockito.Matchers.anyListOf; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; + +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; import com.redhat.thermostat.common.plugins.PluginConfiguration; import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; import com.redhat.thermostat.vm.memory.common.model.VmTlabStat; @@ -49,17 +61,6 @@ import org.junit.Ignore; import org.junit.Test; -import java.util.Arrays; - -import static org.mockito.Matchers.anyListOf; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import static com.redhat.thermostat.vm.memory.common.internal.VmTlabStatDAOImpl.HttpHelper; -import static com.redhat.thermostat.vm.memory.common.internal.VmTlabStatDAOImpl.JsonHelper; - public class VmTlabStatDAOTest { private static final String JSON = "{\"this\":\"is\",\"test\":\"JSON\"}"; @@ -96,6 +97,21 @@ } @Test + public void testActivation() throws Exception { + PluginConfiguration pluginConfig = mock(PluginConfiguration.class); + when(pluginConfig.getGatewayURL()).thenReturn("someGatewayURL"); + VmTlabStatDAOImpl.ConfigurationCreator configCreator = mock(VmTlabStatDAOImpl.ConfigurationCreator.class); + ConfigurationInfoSource configInfoSource = mock(ConfigurationInfoSource.class); + when(configCreator.create(configInfoSource)).thenReturn(pluginConfig); + + VmTlabStatDAOImpl dao = new VmTlabStatDAOImpl(httpClient, httpHelper, jsonHelper, configCreator, configInfoSource); + dao.activate(); + + verify(pluginConfig, times(1)).getGatewayURL(); + verify(httpHelper, times(1)).startClient(httpClient); + } + + @Test @Ignore public void verifyPutStat() throws Exception { // TODO: Remove @Ignore when web-gateway service for TLAB stats is available @@ -118,7 +134,9 @@ stat.setTotalFastWaste(678l); stat.setMaxFastWaste(333l); - VmTlabStatDAO dao = new VmTlabStatDAOImpl(config, httpClient, httpHelper, jsonHelper); + VmTlabStatDAOImpl.ConfigurationCreator configurationCreator = new VmTlabStatDAOImpl.ConfigurationCreator(); + ConfigurationInfoSource configInfoSource = mock(ConfigurationInfoSource.class); + VmTlabStatDAO dao = new VmTlabStatDAOImpl(httpClient, httpHelper, jsonHelper, configurationCreator, configInfoSource); dao.putStat(stat); verify(httpClient).newRequest(GATEWAY_URL);