Mercurial > hg > release > thermostat-0.4
changeset 572:6f6d09025f91
Move client-specific thread-fluff into client-specific bundle
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002986.html
line wrap: on
line diff
--- a/thread/client-common/pom.xml Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-common/pom.xml Wed Aug 29 19:43:25 2012 -0400 @@ -102,11 +102,17 @@ <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> <Bundle-SymbolicName>com.redhat.thermostat.thread.client.common</Bundle-SymbolicName> + <Bundle-Activator>com.redhat.thermostat.thread.client.common.osgi.Activator</Bundle-Activator> <Export-Package> com.redhat.thermostat.thread.client.common, com.redhat.thermostat.thread.client.common.locale, com.redhat.thermostat.thread.client.common.chart, + com.redhat.thermostat.thread.client.common.collector, </Export-Package> + <Private-Package> + com.redhat.thermostat.thread.client.common.collector.impl, + com.redhat.thermostat.thread.client.common.osgi, + </Private-Package> <!-- Do not autogenerate uses clauses in Manifests --> <_nouses>true</_nouses> </instructions>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/ThreadCollector.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,68 @@ +/* + * Copyright 2012 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.thread.client.common.collector; + +import java.util.List; + +import com.redhat.thermostat.thread.model.ThreadInfoData; +import com.redhat.thermostat.thread.model.ThreadSummary; +import com.redhat.thermostat.thread.model.VMThreadCapabilities; + +public interface ThreadCollector { + + VMThreadCapabilities getVMThreadCapabilities(); + + void startHarvester(); + void stopHarvester(); + + ThreadSummary getLatestThreadSummary(); + List<ThreadSummary> getThreadSummary(long since); + List<ThreadSummary> getThreadSummary(); + + /** + * Return a list of {@link ThreadInfoData}, sorted in descending order their by + * {@link ThreadInfoData#getTimeStamp()}, whose elements are at most + * "{@code since}" old. + */ + List<ThreadInfoData> getThreadInfo(long since); + + /** + * Return a list of all the {@link ThreadInfoData} collected, sorted in + * descending order their by {@link ThreadInfoData#getTimeStamp()}. + */ + List<ThreadInfoData> getThreadInfo(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/ThreadCollectorFactory.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,44 @@ +/* + * Copyright 2012 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.thread.client.common.collector; + +import com.redhat.thermostat.common.dao.VmRef; + +public interface ThreadCollectorFactory { + + ThreadCollector getCollector(VmRef reference); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadCollectorFactoryImpl.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,56 @@ +/* + * Copyright 2012 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.thread.client.common.collector.impl; + +import com.redhat.thermostat.common.dao.VmRef; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; +import com.redhat.thermostat.thread.dao.ThreadDao; + +public class ThreadCollectorFactoryImpl implements ThreadCollectorFactory { + + private ThreadDao threadDao; + + public ThreadCollectorFactoryImpl(ThreadDao threadDao) { + this.threadDao = threadDao; + } + + @Override + public synchronized ThreadCollector getCollector(VmRef reference) { + return new ThreadMXBeanCollector(threadDao, reference); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadMXBeanCollector.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,171 @@ +/* + * Copyright 2012 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.thread.client.common.collector.impl; + +import java.net.InetSocketAddress; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import com.redhat.thermostat.client.command.RequestQueue; +import com.redhat.thermostat.common.command.Request; +import com.redhat.thermostat.common.command.RequestResponseListener; +import com.redhat.thermostat.common.command.Response; +import com.redhat.thermostat.common.command.Request.RequestType; +import com.redhat.thermostat.common.dao.HostRef; +import com.redhat.thermostat.common.dao.VmRef; +import com.redhat.thermostat.common.utils.OSGIUtils; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; +import com.redhat.thermostat.thread.collector.HarvesterCommand; +import com.redhat.thermostat.thread.dao.ThreadDao; +import com.redhat.thermostat.thread.model.ThreadInfoData; +import com.redhat.thermostat.thread.model.ThreadSummary; +import com.redhat.thermostat.thread.model.VMThreadCapabilities; + +public class ThreadMXBeanCollector implements ThreadCollector { + + private ThreadDao threadDao; + private VmRef ref; + + public ThreadMXBeanCollector(ThreadDao threadDao, VmRef ref) { + this.threadDao = threadDao; + this.ref = ref; + } + + Request createRequest() { + HostRef targetHostRef = ref.getAgent(); + + // todo + String address = threadDao.getStorage().getConfigListenAddress(targetHostRef); + String [] host = address.split(":"); + + InetSocketAddress target = new InetSocketAddress(host[0], Integer.parseInt(host[1])); + Request harvester = new Request(RequestType.RESPONSE_EXPECTED, target); + + harvester.setReceiver(HarvesterCommand.RECEIVER); + + return harvester; + } + + @Override + public void startHarvester() { + + Request harvester = createRequest(); + harvester.setParameter(HarvesterCommand.class.getName(), HarvesterCommand.START.name()); + harvester.setParameter(HarvesterCommand.VM_ID.name(), ref.getIdString()); + harvester.setParameter(HarvesterCommand.AGENT_ID.name(), ref.getAgent().getAgentId()); + + RequestQueue queue = getRequestQueue(); + queue.putRequest(harvester); + } + + @Override + public void stopHarvester() { + + Request harvester = createRequest(); + harvester.setParameter(HarvesterCommand.class.getName(), HarvesterCommand.STOP.name()); + harvester.setParameter(HarvesterCommand.VM_ID.name(), ref.getIdString()); + + RequestQueue queue = getRequestQueue(); + queue.putRequest(harvester); + } + + @Override + public ThreadSummary getLatestThreadSummary() { + ThreadSummary summary = threadDao.loadLastestSummary(ref); + if (summary == null) { + // default to all 0 + summary = new ThreadSummary(); + } + return summary; + } + + @Override + public List<ThreadSummary> getThreadSummary(long since) { + List<ThreadSummary> summary = threadDao.loadSummary(ref, since); + return summary; + } + + @Override + public List<ThreadSummary> getThreadSummary() { + return getThreadSummary(0); + } + + @Override + public List<ThreadInfoData> getThreadInfo() { + return getThreadInfo(0); + } + + @Override + public List<ThreadInfoData> getThreadInfo(long since) { + return threadDao.loadThreadInfo(ref, since); + } + + @Override + public VMThreadCapabilities getVMThreadCapabilities() { + + VMThreadCapabilities caps = threadDao.loadCapabilities(ref); + if (caps == null) { + Request harvester = createRequest(); + harvester.setParameter(HarvesterCommand.class.getName(), HarvesterCommand.VM_CAPS.name()); + harvester.setParameter(HarvesterCommand.VM_ID.name(), ref.getIdString()); + harvester.setParameter(HarvesterCommand.AGENT_ID.name(), ref.getAgent().getAgentId()); + + final CountDownLatch latch = new CountDownLatch(1); + harvester.addListener(new RequestResponseListener() { + @Override + public void fireComplete(Request request, Response response) { + latch.countDown(); + } + }); + + RequestQueue queue = getRequestQueue(); + queue.putRequest(harvester); + + try { + latch.await(); + caps = threadDao.loadCapabilities(ref); + } catch (InterruptedException ignore) { + caps = new VMThreadCapabilities(); + } + } + return caps; + } + + RequestQueue getRequestQueue() { + return OSGIUtils.getInstance().getService(RequestQueue.class); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/osgi/Activator.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,77 @@ +/* + * Copyright 2012 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.thread.client.common.osgi; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; + +import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; +import com.redhat.thermostat.thread.client.common.collector.impl.ThreadCollectorFactoryImpl; +import com.redhat.thermostat.thread.dao.ThreadDao; +import com.redhat.thermostat.thread.dao.impl.ThreadDaoImpl; + +public class Activator implements BundleActivator { + + private ThreadCollectorFactoryImpl collectorFactory; + + @Override + public void start(final BundleContext context) throws Exception { + + @SuppressWarnings({ "rawtypes", "unchecked" }) + ServiceTracker tracker = new ServiceTracker(context, ThreadDao.class.getName(), null) { + @Override + public Object addingService(ServiceReference reference) { + + ThreadDao threadDao = (ThreadDao) context.getService(reference); + + collectorFactory = new ThreadCollectorFactoryImpl(threadDao); + + context.registerService(ThreadCollectorFactory.class.getName(), collectorFactory, null); + + return super.addingService(reference); + } + }; + tracker.open(); + } + + @Override + public void stop(BundleContext context) throws Exception { + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/test/java/com/redhat/thermostat/thread/client/common/collector/ThreadCollectorFactoryTest.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,63 @@ +/* + * Copyright 2012 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.thread.client.common.collector; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; + +import java.util.concurrent.ScheduledExecutorService; + +import org.junit.Test; + +import com.redhat.thermostat.common.dao.VmRef; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; +import com.redhat.thermostat.thread.client.common.collector.impl.ThreadCollectorFactoryImpl; +import com.redhat.thermostat.thread.dao.ThreadDao; + +public class ThreadCollectorFactoryTest { + + @Test + public void testThreadCollectorFactory() { + ThreadDao threadDao = mock(ThreadDao.class); + VmRef reference = mock(VmRef.class); + + ThreadCollectorFactory factory = new ThreadCollectorFactoryImpl(threadDao); + ThreadCollector collector = factory.getCollector(reference); + assertNotNull(collector); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/client-common/src/test/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadCollectorTest.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,213 @@ +/* + * Copyright 2012 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.thread.client.common.collector.impl; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.times; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import com.redhat.thermostat.client.command.RequestQueue; +import com.redhat.thermostat.common.command.Request; +import com.redhat.thermostat.common.command.RequestResponseListener; +import com.redhat.thermostat.common.dao.HostRef; +import com.redhat.thermostat.common.dao.VmRef; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.impl.ThreadMXBeanCollector; +import com.redhat.thermostat.thread.collector.HarvesterCommand; +import com.redhat.thermostat.thread.dao.ThreadDao; +import com.redhat.thermostat.thread.model.VMThreadCapabilities; + +public class ThreadCollectorTest { + + @Test + public void testVMCapabilitiesNotInDAO() throws Exception { + + HostRef agent = mock(HostRef.class); + when(agent.getAgentId()).thenReturn("42"); + + VmRef reference = mock(VmRef.class); + when(reference.getIdString()).thenReturn("00101010"); + when(reference.getAgent()).thenReturn(agent); + ThreadDao threadDao = mock(ThreadDao.class); + + VMThreadCapabilities resCaps = mock(VMThreadCapabilities.class); + when(threadDao.loadCapabilities(reference)).thenReturn(null).thenReturn(resCaps); + + final Request request = mock(Request.class); + final RequestQueue requestQueue = mock(RequestQueue.class); + + final ArgumentCaptor<RequestResponseListener> captor = ArgumentCaptor.forClass(RequestResponseListener.class); + doNothing().when(request).addListener(captor.capture()); + + doAnswer(new Answer<Void>() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + Request req = (Request) invocation.getArguments()[0]; + assertSame(request, req); + + RequestResponseListener listener = captor.getValue(); + listener.fireComplete(null, null); + + return null; + } + + }).when(requestQueue).putRequest(request); + + /* ************* */ + + ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { + @Override + Request createRequest() { + return request; + } + @Override + RequestQueue getRequestQueue() { + return requestQueue; + } + }; + + VMThreadCapabilities caps = collector.getVMThreadCapabilities(); + + verify(request).setParameter(HarvesterCommand.class.getName(), HarvesterCommand.VM_CAPS.name()); + verify(request).setParameter(HarvesterCommand.VM_ID.name(), "00101010"); + verify(request).setParameter(HarvesterCommand.AGENT_ID.name(), "42"); + + verify(requestQueue).putRequest(request); + + verify(threadDao, times(2)).loadCapabilities(reference); + assertSame(resCaps, caps); + } + + @Test + public void testVMCapabilitiesInDAO() throws Exception { + + VmRef reference = mock(VmRef.class); + ThreadDao threadDao = mock(ThreadDao.class); + + VMThreadCapabilities resCaps = mock(VMThreadCapabilities.class); + when(threadDao.loadCapabilities(reference)).thenReturn(resCaps); + + ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { + @Override + Request createRequest() { + fail(); + return null; + } + @Override + RequestQueue getRequestQueue() { + fail(); + return null; + } + }; + + VMThreadCapabilities caps = collector.getVMThreadCapabilities(); + + verify(threadDao, times(1)).loadCapabilities(reference); + assertSame(resCaps, caps); + } + + @Test + public void testStart() { + + HostRef agent = mock(HostRef.class); + when(agent.getAgentId()).thenReturn("42"); + + final Request request = mock(Request.class); + final RequestQueue requestQueue = mock(RequestQueue.class); + ThreadDao threadDao = mock(ThreadDao.class); + VmRef reference = mock(VmRef.class); + when(reference.getIdString()).thenReturn("00101010"); + when(reference.getAgent()).thenReturn(agent); + + ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { + @Override + Request createRequest() { + return request; + } + @Override + RequestQueue getRequestQueue() { + return requestQueue; + } + }; + collector.startHarvester(); + + verify(request).setParameter(HarvesterCommand.class.getName(), HarvesterCommand.START.name()); + verify(request).setParameter(HarvesterCommand.VM_ID.name(), "00101010"); + verify(request).setParameter(HarvesterCommand.AGENT_ID.name(), "42"); + + verify(requestQueue).putRequest(request); + } + + + @Test + public void testStop() { + + final Request request = mock(Request.class); + final RequestQueue requestQueue = mock(RequestQueue.class); + ThreadDao threadDao = mock(ThreadDao.class); + VmRef reference = mock(VmRef.class); + when(reference.getIdString()).thenReturn("00101010"); + + ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { + @Override + Request createRequest() { + return request; + } + @Override + RequestQueue getRequestQueue() { + return requestQueue; + } + }; + collector.stopHarvester(); + + verify(request).setParameter(HarvesterCommand.class.getName(), HarvesterCommand.STOP.name()); + verify(request).setParameter(HarvesterCommand.VM_ID.name(), "00101010"); + + verify(requestQueue).putRequest(request); + } +}
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationController.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationController.java Wed Aug 29 19:43:25 2012 -0400 @@ -58,9 +58,9 @@ import com.redhat.thermostat.thread.client.common.VMThreadCapabilitiesView; import com.redhat.thermostat.thread.client.common.ThreadView.ThreadAction; import com.redhat.thermostat.thread.client.common.chart.LivingDaemonThreadDifferenceChart; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; -import com.redhat.thermostat.thread.collector.ThreadCollector; -import com.redhat.thermostat.thread.collector.ThreadCollectorFactory; import com.redhat.thermostat.thread.model.ThreadSummary; import com.redhat.thermostat.thread.model.VMThreadCapabilities;
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationService.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationService.java Wed Aug 29 19:43:25 2012 -0400 @@ -43,7 +43,7 @@ import com.redhat.thermostat.client.osgi.service.VmInformationServiceController; import com.redhat.thermostat.common.dao.VmRef; import com.redhat.thermostat.thread.client.common.ThreadViewProvider; -import com.redhat.thermostat.thread.collector.ThreadCollectorFactory; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; public class ThreadInformationService implements VmInformationService {
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableController.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableController.java Wed Aug 29 19:43:25 2012 -0400 @@ -50,7 +50,7 @@ import com.redhat.thermostat.common.Timer.SchedulingType; import com.redhat.thermostat.thread.client.common.ThreadTableBean; import com.redhat.thermostat.thread.client.common.ThreadTableView; -import com.redhat.thermostat.thread.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; import com.redhat.thermostat.thread.model.ThreadInfoData; public class ThreadTableController implements CommonController {
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/VMThreadCapabilitiesController.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/VMThreadCapabilitiesController.java Wed Aug 29 19:43:25 2012 -0400 @@ -40,7 +40,7 @@ import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.thread.client.common.VMThreadCapabilitiesView; -import com.redhat.thermostat.thread.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; import com.redhat.thermostat.thread.model.VMThreadCapabilities; public class VMThreadCapabilitiesController implements CommonController {
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/osgi/Activator.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/osgi/Activator.java Wed Aug 29 19:43:25 2012 -0400 @@ -46,8 +46,8 @@ import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.MultipleServiceTracker.Action; import com.redhat.thermostat.thread.client.common.ThreadViewProvider; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; import com.redhat.thermostat.thread.client.controller.impl.ThreadInformationService; -import com.redhat.thermostat.thread.collector.ThreadCollectorFactory; public class Activator implements BundleActivator {
--- a/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationControllerTest.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationControllerTest.java Wed Aug 29 19:43:25 2012 -0400 @@ -61,8 +61,8 @@ import com.redhat.thermostat.thread.client.common.ThreadView; import com.redhat.thermostat.thread.client.common.ThreadViewProvider; import com.redhat.thermostat.thread.client.common.VMThreadCapabilitiesView; -import com.redhat.thermostat.thread.collector.ThreadCollector; -import com.redhat.thermostat.thread.collector.ThreadCollectorFactory; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollectorFactory; public class ThreadInformationControllerTest {
--- a/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableControllerTest.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableControllerTest.java Wed Aug 29 19:43:25 2012 -0400 @@ -54,7 +54,7 @@ import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.appctx.ApplicationContextUtil; import com.redhat.thermostat.thread.client.common.ThreadTableView; -import com.redhat.thermostat.thread.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; import com.redhat.thermostat.thread.model.ThreadInfoData; public class ThreadTableControllerTest {
--- a/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/VMThreadCapabilitiesControllerTest.java Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/VMThreadCapabilitiesControllerTest.java Wed Aug 29 19:43:25 2012 -0400 @@ -50,7 +50,7 @@ import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.appctx.ApplicationContextUtil; import com.redhat.thermostat.thread.client.common.VMThreadCapabilitiesView; -import com.redhat.thermostat.thread.collector.ThreadCollector; +import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; import com.redhat.thermostat.thread.model.VMThreadCapabilities; public class VMThreadCapabilitiesControllerTest {
--- a/thread/collector/pom.xml Wed Aug 29 19:42:32 2012 -0400 +++ b/thread/collector/pom.xml Wed Aug 29 19:43:25 2012 -0400 @@ -105,7 +105,7 @@ <configuration> <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> - <Bundle-Activator>com.redhat.thermostat.thread.collector.osgi.Activator</Bundle-Activator> + <Bundle-Activator>com.redhat.thermostat.thread.common.osgi.Activator</Bundle-Activator> <Bundle-SymbolicName>com.redhat.thermostat.thread.collector</Bundle-SymbolicName> <Export-Package> com.redhat.thermostat.thread.collector, @@ -113,8 +113,7 @@ com.redhat.thermostat.thread.model, </Export-Package> <Private-Package> - com.redhat.thermostat.thread.collector.osgi, - com.redhat.thermostat.thread.collector.impl, + com.redhat.thermostat.thread.common.osgi, com.redhat.thermostat.thread.dao.impl, </Private-Package> <!-- Do not autogenerate uses clauses in Manifests -->
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/collector/ThreadCollector.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * Copyright 2012 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.thread.collector; - -import java.util.List; - -import com.redhat.thermostat.thread.model.ThreadInfoData; -import com.redhat.thermostat.thread.model.ThreadSummary; -import com.redhat.thermostat.thread.model.VMThreadCapabilities; - -public interface ThreadCollector { - - VMThreadCapabilities getVMThreadCapabilities(); - - void startHarvester(); - void stopHarvester(); - - ThreadSummary getLatestThreadSummary(); - List<ThreadSummary> getThreadSummary(long since); - List<ThreadSummary> getThreadSummary(); - - /** - * Return a list of {@link ThreadInfoData}, sorted in descending order their by - * {@link ThreadInfoData#getTimeStamp()}, whose elements are at most - * "{@code since}" old. - */ - List<ThreadInfoData> getThreadInfo(long since); - - /** - * Return a list of all the {@link ThreadInfoData} collected, sorted in - * descending order their by {@link ThreadInfoData#getTimeStamp()}. - */ - List<ThreadInfoData> getThreadInfo(); -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/collector/ThreadCollectorFactory.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* - * Copyright 2012 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.thread.collector; - -import com.redhat.thermostat.common.dao.VmRef; - -public interface ThreadCollectorFactory { - - ThreadCollector getCollector(VmRef reference); -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/collector/impl/ThreadCollectorFactoryImpl.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * Copyright 2012 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.thread.collector.impl; - -import com.redhat.thermostat.common.dao.VmRef; -import com.redhat.thermostat.thread.collector.ThreadCollector; -import com.redhat.thermostat.thread.collector.ThreadCollectorFactory; -import com.redhat.thermostat.thread.dao.ThreadDao; - -public class ThreadCollectorFactoryImpl implements ThreadCollectorFactory { - - private ThreadDao threadDao; - - public ThreadCollectorFactoryImpl(ThreadDao threadDao) { - this.threadDao = threadDao; - } - - @Override - public synchronized ThreadCollector getCollector(VmRef reference) { - return new ThreadMXBeanCollector(threadDao, reference); - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/collector/impl/ThreadMXBeanCollector.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,171 +0,0 @@ -/* - * Copyright 2012 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.thread.collector.impl; - -import java.net.InetSocketAddress; -import java.util.List; -import java.util.concurrent.CountDownLatch; - -import com.redhat.thermostat.client.command.RequestQueue; -import com.redhat.thermostat.common.command.Request; -import com.redhat.thermostat.common.command.RequestResponseListener; -import com.redhat.thermostat.common.command.Response; -import com.redhat.thermostat.common.command.Request.RequestType; -import com.redhat.thermostat.common.dao.HostRef; -import com.redhat.thermostat.common.dao.VmRef; -import com.redhat.thermostat.common.utils.OSGIUtils; -import com.redhat.thermostat.thread.collector.HarvesterCommand; -import com.redhat.thermostat.thread.collector.ThreadCollector; -import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.ThreadInfoData; -import com.redhat.thermostat.thread.model.ThreadSummary; -import com.redhat.thermostat.thread.model.VMThreadCapabilities; - -public class ThreadMXBeanCollector implements ThreadCollector { - - private ThreadDao threadDao; - private VmRef ref; - - public ThreadMXBeanCollector(ThreadDao threadDao, VmRef ref) { - this.threadDao = threadDao; - this.ref = ref; - } - - Request createRequest() { - HostRef targetHostRef = ref.getAgent(); - - // todo - String address = threadDao.getStorage().getConfigListenAddress(targetHostRef); - String [] host = address.split(":"); - - InetSocketAddress target = new InetSocketAddress(host[0], Integer.parseInt(host[1])); - Request harvester = new Request(RequestType.RESPONSE_EXPECTED, target); - - harvester.setReceiver(HarvesterCommand.RECEIVER); - - return harvester; - } - - @Override - public void startHarvester() { - - Request harvester = createRequest(); - harvester.setParameter(HarvesterCommand.class.getName(), HarvesterCommand.START.name()); - harvester.setParameter(HarvesterCommand.VM_ID.name(), ref.getIdString()); - harvester.setParameter(HarvesterCommand.AGENT_ID.name(), ref.getAgent().getAgentId()); - - RequestQueue queue = getRequestQueue(); - queue.putRequest(harvester); - } - - @Override - public void stopHarvester() { - - Request harvester = createRequest(); - harvester.setParameter(HarvesterCommand.class.getName(), HarvesterCommand.STOP.name()); - harvester.setParameter(HarvesterCommand.VM_ID.name(), ref.getIdString()); - - RequestQueue queue = getRequestQueue(); - queue.putRequest(harvester); - } - - @Override - public ThreadSummary getLatestThreadSummary() { - ThreadSummary summary = threadDao.loadLastestSummary(ref); - if (summary == null) { - // default to all 0 - summary = new ThreadSummary(); - } - return summary; - } - - @Override - public List<ThreadSummary> getThreadSummary(long since) { - List<ThreadSummary> summary = threadDao.loadSummary(ref, since); - return summary; - } - - @Override - public List<ThreadSummary> getThreadSummary() { - return getThreadSummary(0); - } - - @Override - public List<ThreadInfoData> getThreadInfo() { - return getThreadInfo(0); - } - - @Override - public List<ThreadInfoData> getThreadInfo(long since) { - return threadDao.loadThreadInfo(ref, since); - } - - @Override - public VMThreadCapabilities getVMThreadCapabilities() { - - VMThreadCapabilities caps = threadDao.loadCapabilities(ref); - if (caps == null) { - Request harvester = createRequest(); - harvester.setParameter(HarvesterCommand.class.getName(), HarvesterCommand.VM_CAPS.name()); - harvester.setParameter(HarvesterCommand.VM_ID.name(), ref.getIdString()); - harvester.setParameter(HarvesterCommand.AGENT_ID.name(), ref.getAgent().getAgentId()); - - final CountDownLatch latch = new CountDownLatch(1); - harvester.addListener(new RequestResponseListener() { - @Override - public void fireComplete(Request request, Response response) { - latch.countDown(); - } - }); - - RequestQueue queue = getRequestQueue(); - queue.putRequest(harvester); - - try { - latch.await(); - caps = threadDao.loadCapabilities(ref); - } catch (InterruptedException ignore) { - caps = new VMThreadCapabilities(); - } - } - return caps; - } - - RequestQueue getRequestQueue() { - return OSGIUtils.getInstance().getService(RequestQueue.class); - } -}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/collector/osgi/Activator.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -/* - * Copyright 2012 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.thread.collector.osgi; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; - -import com.redhat.thermostat.common.storage.Storage; -import com.redhat.thermostat.thread.collector.ThreadCollectorFactory; -import com.redhat.thermostat.thread.collector.impl.ThreadCollectorFactoryImpl; -import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.dao.impl.ThreadDaoImpl; - -public class Activator implements BundleActivator { - - private ThreadCollectorFactoryImpl collectorFactory; - - @Override - public void start(final BundleContext context) throws Exception { - - @SuppressWarnings({ "rawtypes", "unchecked" }) - ServiceTracker tracker = new ServiceTracker(context, Storage.class.getName(), null) { - @Override - public Object addingService(ServiceReference reference) { - - Storage storage = (Storage) context.getService(reference); - - ThreadDao threadDao = new ThreadDaoImpl(storage); - collectorFactory = new ThreadCollectorFactoryImpl(threadDao); - - context.registerService(ThreadCollectorFactory.class.getName(), collectorFactory, null); - context.registerService(ThreadDao.class.getName(), threadDao, null); - - return super.addingService(reference); - } - }; - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/common/osgi/Activator.java Wed Aug 29 19:43:25 2012 -0400 @@ -0,0 +1,78 @@ +/* + * Copyright 2012 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.thread.common.osgi; + +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.storage.Storage; +import com.redhat.thermostat.thread.dao.ThreadDao; +import com.redhat.thermostat.thread.dao.impl.ThreadDaoImpl; + +public class Activator implements BundleActivator { + + @SuppressWarnings("rawtypes") + private ServiceRegistration reg; + + @Override + public void start(BundleContext context) throws Exception { + + + @SuppressWarnings({ "rawtypes", "unchecked" }) + ServiceTracker tracker = new ServiceTracker(context, Storage.class.getName(), null) { + @Override + public Object addingService(ServiceReference reference) { + Storage storage = (Storage) context.getService(reference); + ThreadDao threadDao = new ThreadDaoImpl(storage); + reg = context.registerService(ThreadDao.class.getName(), threadDao, null); + return super.addingService(reference); + } + }; + tracker.open(); + } + + @Override + public void stop(BundleContext context) throws Exception { + if (reg != null) { + reg.unregister(); + } + } + +}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/collector/ThreadCollectorFactoryTest.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* - * Copyright 2012 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.thread.collector; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; - -import java.util.concurrent.ScheduledExecutorService; - -import org.junit.Test; - -import com.redhat.thermostat.common.dao.VmRef; -import com.redhat.thermostat.thread.collector.impl.ThreadCollectorFactoryImpl; -import com.redhat.thermostat.thread.dao.ThreadDao; - -public class ThreadCollectorFactoryTest { - - @Test - public void testThreadCollectorFactory() { - ThreadDao threadDao = mock(ThreadDao.class); - VmRef reference = mock(VmRef.class); - - ThreadCollectorFactory factory = new ThreadCollectorFactoryImpl(threadDao); - ThreadCollector collector = factory.getCollector(reference); - assertNotNull(collector); - } -}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/collector/impl/ThreadCollectorTest.java Wed Aug 29 19:42:32 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,212 +0,0 @@ -/* - * Copyright 2012 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.thread.collector.impl; - -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; - -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.times; - -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -import com.redhat.thermostat.client.command.RequestQueue; -import com.redhat.thermostat.common.command.Request; -import com.redhat.thermostat.common.command.RequestResponseListener; -import com.redhat.thermostat.common.dao.HostRef; -import com.redhat.thermostat.common.dao.VmRef; -import com.redhat.thermostat.thread.collector.HarvesterCommand; -import com.redhat.thermostat.thread.collector.ThreadCollector; -import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.VMThreadCapabilities; - -public class ThreadCollectorTest { - - @Test - public void testVMCapabilitiesNotInDAO() throws Exception { - - HostRef agent = mock(HostRef.class); - when(agent.getAgentId()).thenReturn("42"); - - VmRef reference = mock(VmRef.class); - when(reference.getIdString()).thenReturn("00101010"); - when(reference.getAgent()).thenReturn(agent); - ThreadDao threadDao = mock(ThreadDao.class); - - VMThreadCapabilities resCaps = mock(VMThreadCapabilities.class); - when(threadDao.loadCapabilities(reference)).thenReturn(null).thenReturn(resCaps); - - final Request request = mock(Request.class); - final RequestQueue requestQueue = mock(RequestQueue.class); - - final ArgumentCaptor<RequestResponseListener> captor = ArgumentCaptor.forClass(RequestResponseListener.class); - doNothing().when(request).addListener(captor.capture()); - - doAnswer(new Answer<Void>() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - Request req = (Request) invocation.getArguments()[0]; - assertSame(request, req); - - RequestResponseListener listener = captor.getValue(); - listener.fireComplete(null, null); - - return null; - } - - }).when(requestQueue).putRequest(request); - - /* ************* */ - - ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { - @Override - Request createRequest() { - return request; - } - @Override - RequestQueue getRequestQueue() { - return requestQueue; - } - }; - - VMThreadCapabilities caps = collector.getVMThreadCapabilities(); - - verify(request).setParameter(HarvesterCommand.class.getName(), HarvesterCommand.VM_CAPS.name()); - verify(request).setParameter(HarvesterCommand.VM_ID.name(), "00101010"); - verify(request).setParameter(HarvesterCommand.AGENT_ID.name(), "42"); - - verify(requestQueue).putRequest(request); - - verify(threadDao, times(2)).loadCapabilities(reference); - assertSame(resCaps, caps); - } - - @Test - public void testVMCapabilitiesInDAO() throws Exception { - - VmRef reference = mock(VmRef.class); - ThreadDao threadDao = mock(ThreadDao.class); - - VMThreadCapabilities resCaps = mock(VMThreadCapabilities.class); - when(threadDao.loadCapabilities(reference)).thenReturn(resCaps); - - ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { - @Override - Request createRequest() { - fail(); - return null; - } - @Override - RequestQueue getRequestQueue() { - fail(); - return null; - } - }; - - VMThreadCapabilities caps = collector.getVMThreadCapabilities(); - - verify(threadDao, times(1)).loadCapabilities(reference); - assertSame(resCaps, caps); - } - - @Test - public void testStart() { - - HostRef agent = mock(HostRef.class); - when(agent.getAgentId()).thenReturn("42"); - - final Request request = mock(Request.class); - final RequestQueue requestQueue = mock(RequestQueue.class); - ThreadDao threadDao = mock(ThreadDao.class); - VmRef reference = mock(VmRef.class); - when(reference.getIdString()).thenReturn("00101010"); - when(reference.getAgent()).thenReturn(agent); - - ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { - @Override - Request createRequest() { - return request; - } - @Override - RequestQueue getRequestQueue() { - return requestQueue; - } - }; - collector.startHarvester(); - - verify(request).setParameter(HarvesterCommand.class.getName(), HarvesterCommand.START.name()); - verify(request).setParameter(HarvesterCommand.VM_ID.name(), "00101010"); - verify(request).setParameter(HarvesterCommand.AGENT_ID.name(), "42"); - - verify(requestQueue).putRequest(request); - } - - - @Test - public void testStop() { - - final Request request = mock(Request.class); - final RequestQueue requestQueue = mock(RequestQueue.class); - ThreadDao threadDao = mock(ThreadDao.class); - VmRef reference = mock(VmRef.class); - when(reference.getIdString()).thenReturn("00101010"); - - ThreadCollector collector = new ThreadMXBeanCollector(threadDao, reference) { - @Override - Request createRequest() { - return request; - } - @Override - RequestQueue getRequestQueue() { - return requestQueue; - } - }; - collector.stopHarvester(); - - verify(request).setParameter(HarvesterCommand.class.getName(), HarvesterCommand.STOP.name()); - verify(request).setParameter(HarvesterCommand.VM_ID.name(), "00101010"); - - verify(requestQueue).putRequest(request); - } -}