Mercurial > hg > release > thermostat-1.4
changeset 1528:b6839c766124
Reduce duplication in KillVMCommand and KillVMAction.
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-October/011323.html
line wrap: on
line diff
--- a/killvm/client-swing/pom.xml Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/client-swing/pom.xml Mon Oct 27 12:48:15 2014 -0400 @@ -145,5 +145,10 @@ <version>${project.version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-killvm-common</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> </project>
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/Activator.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/Activator.java Mon Oct 27 12:48:15 2014 -0400 @@ -47,6 +47,7 @@ import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.MultipleServiceTracker.Action; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; @@ -60,6 +61,7 @@ Class<?>[] serviceDeps = new Class<?>[] { AgentInfoDAO.class, VmInfoDAO.class, + KillVMRequest.class, RequestQueue.class, }; @@ -68,8 +70,8 @@ public void dependenciesAvailable(Map<String, Object> services) { AgentInfoDAO agentDao = (AgentInfoDAO) services.get(AgentInfoDAO.class.getName()); VmInfoDAO vmDao = (VmInfoDAO) services.get(VmInfoDAO.class.getName()); - RequestQueue queue = (RequestQueue) services.get(RequestQueue.class.getName()); - KillVMAction service = new KillVMAction(agentDao, vmDao, queue, new SwingVMKilledListener()); + KillVMRequest request = (KillVMRequest) services.get(KillVMRequest.class.getName()); + KillVMAction service = new KillVMAction(agentDao, vmDao, request, new SwingVMKilledListener()); killActionRegistration = context.registerService(ReferenceContextAction.class.getName(), service, null); }
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java Mon Oct 27 12:48:15 2014 -0400 @@ -46,6 +46,7 @@ import com.redhat.thermostat.common.command.Request.RequestType; import com.redhat.thermostat.common.command.RequestResponseListener; import com.redhat.thermostat.killvm.client.locale.LocaleResources; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.shared.locale.LocalizedString; import com.redhat.thermostat.shared.locale.Translate; import com.redhat.thermostat.storage.core.Ref; @@ -65,15 +66,15 @@ private final AgentInfoDAO agentDao; private final VmInfoDAO vmDao; private final Translate<LocaleResources> t; - private final RequestQueue queue; + private final KillVMRequest request; private final RequestResponseListener listener; - public KillVMAction(AgentInfoDAO agentDao, VmInfoDAO vmDao, RequestQueue queue, RequestResponseListener listener) { + public KillVMAction(AgentInfoDAO agentDao, VmInfoDAO vmDao, KillVMRequest queue, RequestResponseListener listener) { Objects.requireNonNull(listener, "Listener can't be null"); this.agentDao = agentDao; this.vmDao = vmDao; this.t = LocaleResources.createLocalizer(); - this.queue = queue; + this.request = queue; this.listener = listener; } @@ -89,29 +90,12 @@ @Override public void execute(Ref ref) { - if (!(ref instanceof VmRef)) { return; } - VmRef reference = (VmRef) ref; - - String address = agentDao.getAgentInformation(reference.getHostRef()).getConfigListenAddress(); - - String [] host = address.split(":"); - InetSocketAddress target = new InetSocketAddress(host[0], Integer.parseInt(host[1])); - Request murderer = getKillRequest(target); - murderer.setParameter(Request.ACTION, CMD_CHANNEL_ACTION_NAME); - murderer.setParameter("vm-pid", String.valueOf(reference.getPid())); - murderer.setReceiver(RECEIVER); - murderer.addListener(listener); - queue.putRequest(murderer); - } - - // testing hook; keep this package private - Request getKillRequest(InetSocketAddress target) { - return new Request(RequestType.RESPONSE_EXPECTED, target); + request.sendKillVMRequestToAgent(reference, agentDao, listener); } @Override
--- a/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/ActivatorTest.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/ActivatorTest.java Mon Oct 27 12:48:15 2014 -0400 @@ -45,6 +45,7 @@ import com.redhat.thermostat.client.command.RequestQueue; import com.redhat.thermostat.client.ui.ReferenceContextAction; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.testutils.StubBundleContext; @@ -71,10 +72,13 @@ AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); RequestQueue queue = mock(RequestQueue.class); + KillVMRequest request = mock(KillVMRequest.class); context.registerService(AgentInfoDAO.class, agentInfoDAO, null); context.registerService(VmInfoDAO.class, vmInfoDAO, null); context.registerService(RequestQueue.class, queue, null); + context.registerService(KillVMRequest.class, request, null); + Activator activator = new Activator(); @@ -85,7 +89,7 @@ activator.stop(context); assertEquals(0, context.getServiceListeners().size()); - assertEquals(3, context.getAllServices().size()); + assertEquals(4, context.getAllServices().size()); } }
--- a/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/KillVMActionTest.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/KillVMActionTest.java Mon Oct 27 12:48:15 2014 -0400 @@ -36,30 +36,24 @@ package com.redhat.thermostat.killvm.client.internal; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.net.InetSocketAddress; - 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.Filter; -import com.redhat.thermostat.common.command.Request; import com.redhat.thermostat.common.command.RequestResponseListener; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.storage.core.HostRef; import com.redhat.thermostat.storage.core.Ref; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; import com.redhat.thermostat.storage.model.VmInfo; public class KillVMActionTest { @@ -75,10 +69,10 @@ VmInfo vmInfo = mock(VmInfo.class); when(vmInfoDao.getVmInfo(matching)).thenReturn(vmInfo); - RequestQueue queue = mock(RequestQueue.class); + KillVMRequest request = mock(KillVMRequest.class); RequestResponseListener listener = mock(RequestResponseListener.class); - KillVMAction action = new KillVMAction(agentDao, vmInfoDao, queue, listener); + KillVMAction action = new KillVMAction(agentDao, vmInfoDao, request, listener); Filter<Ref> filter = action.getFilter(); @@ -101,10 +95,10 @@ VmInfo vmInfo = mock(VmInfo.class); when(vmInfoDao.getVmInfo(matching)).thenReturn(vmInfo); - RequestQueue queue = mock(RequestQueue.class); + KillVMRequest request = mock(KillVMRequest.class); RequestResponseListener listener = mock(RequestResponseListener.class); - KillVMAction action = new KillVMAction(agentDao, vmInfoDao, queue, listener); + KillVMAction action = new KillVMAction(agentDao, vmInfoDao, request, listener); Filter<Ref> filter = action.getFilter(); @@ -118,39 +112,27 @@ @Test public void canQueueKillRequest() { VmRef ref = mock(VmRef.class); - HostRef hostref = mock(HostRef.class); - when(ref.getHostRef()).thenReturn(hostref); - String agentAddress = "127.0.0.1:8888"; - - AgentInformation agentInfo = mock(AgentInformation.class); - when(agentInfo.getConfigListenAddress()).thenReturn(agentAddress); - AgentInfoDAO agentDao = mock(AgentInfoDAO.class); - when(agentDao.getAgentInformation(hostref)).thenReturn(agentInfo); VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); RequestResponseListener agentResponseListener = mock(RequestResponseListener.class); - RequestQueue queue = mock(RequestQueue.class); - final Request req = mock(Request.class); - when(req.getParameter(Request.ACTION)).thenReturn("killvm"); - KillVMAction action = new KillVMAction(agentDao, vmInfoDao, queue, agentResponseListener) { + KillVMRequest request = mock(KillVMRequest.class); + KillVMAction action = new KillVMAction(agentDao, vmInfoDao, request, agentResponseListener); + + final boolean[] complete = {false}; + + doAnswer(new Answer() { @Override - Request getKillRequest(InetSocketAddress target) { - return req; + public Object answer(InvocationOnMock invocation) throws Throwable { + complete[0] = true; + return null; } - }; + }).when(request).sendKillVMRequestToAgent(ref, agentDao, agentResponseListener); + action.execute(ref); - verify(req).setParameter(eq("vm-pid"), any(String.class)); - verify(req).setParameter(eq(Request.ACTION), any(String.class)); - verify(req).addListener(agentResponseListener); - ArgumentCaptor<String> receiverCaptor = ArgumentCaptor - .forClass(String.class); - verify(req).setReceiver(receiverCaptor.capture()); - assertEquals( - "com.redhat.thermostat.killvm.agent.internal.KillVmReceiver", - receiverCaptor.getValue()); - verify(queue).putRequest(req); + + assertTrue(complete[0]); } }
--- a/killvm/command/pom.xml Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/command/pom.xml Mon Oct 27 12:48:15 2014 -0400 @@ -122,6 +122,11 @@ <artifactId>thermostat-shared-config</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-killvm-common</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> </project>
--- a/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/KillVMCommand.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/KillVMCommand.java Mon Oct 27 12:48:15 2014 -0400 @@ -36,20 +36,18 @@ package com.redhat.thermostat.killvm.command; -import java.net.InetSocketAddress; - import com.redhat.thermostat.client.cli.HostVMArguments; -import com.redhat.thermostat.client.command.RequestQueue; import com.redhat.thermostat.common.cli.AbstractCommand; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; -import com.redhat.thermostat.common.command.Request; import com.redhat.thermostat.killvm.command.internal.ShellVMKilledListener; import com.redhat.thermostat.killvm.command.locale.LocaleResources; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.shared.locale.Translate; import com.redhat.thermostat.storage.core.HostRef; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.DAOException; import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.VmInfo; @@ -65,7 +63,7 @@ private HostInfoDAO hostInfoDAO; private VmInfoDAO vmInfoDAO; private AgentInfoDAO agentInfoDAO; - private RequestQueue requestQueue; + private KillVMRequest request; public KillVMCommand(ShellVMKilledListener listener) { this.listener = listener; @@ -76,7 +74,7 @@ requireNonNull(vmInfoDAO, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); requireNonNull(hostInfoDAO, translator.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); requireNonNull(agentInfoDAO, translator.localize(LocaleResources.AGENT_SERVICE_UNAVAILABLE)); - requireNonNull(requestQueue, translator.localize(LocaleResources.QUEUE_SERVICE_UNAVAILABLE)); + requireNonNull(request, translator.localize(LocaleResources.REQUEST_SERVICE_UNAVAILABLE)); listener.setOut(ctx.getConsole().getOutput()); listener.setErr(ctx.getConsole().getError()); @@ -87,43 +85,22 @@ } private void attemptToKillVM(VmRef vmRef) throws CommandException { - VmInfo result = vmInfoDAO.getVmInfo(vmRef); - - if (result == null) { + try { + VmInfo result = vmInfoDAO.getVmInfo(vmRef); + sendKillRequest(vmRef.getHostRef(), result.getVmPid()); + } catch (DAOException e) { + //FIXME: VmInfoDaoImpl currently throws DAOException when VM is not found + //This should be changed when VmInfoDAOImpl gets fixed throw new CommandException(translator.localize(LocaleResources.VM_NOT_FOUND, vmRef.getVmId())); - } else { - sendKillRequest(vmRef.getHostRef(), result.getVmPid()); } - } - private void sendKillRequest(HostRef hostRef, int vmPid) throws CommandException { - InetSocketAddress target = getAddressFromHost(hostRef); - - Request murderer = setupMurderer(target, vmPid); - - requestQueue.putRequest(murderer); - - waitForListenerResponse(); } - private InetSocketAddress getAddressFromHost(HostRef hostRef) throws CommandException { - String [] hostAndPort = getHostAndPort(hostRef); - return new InetSocketAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1])); - } + private void sendKillRequest(HostRef ref, int vmPid) throws CommandException { + VmRef vmRef = new VmRef(ref, "dummy", vmPid, "dummy"); + request.sendKillVMRequestToAgent(vmRef, agentInfoDAO, listener); - private Request setupMurderer(InetSocketAddress target, int vmPid) { - Request murderer = new Request(Request.RequestType.RESPONSE_EXPECTED, target); - murderer.setParameter(Request.ACTION, CMD_CHANNEL_ACTION_NAME); - murderer.setParameter("vm-pid", String.valueOf(vmPid)); - murderer.setReceiver(RECEIVER); - murderer.addListener(listener); - - return murderer; - } - - private String[] getHostAndPort(HostRef hostRef) throws CommandException { - String address = agentInfoDAO.getAgentInformation(hostRef).getConfigListenAddress(); - return address.split(":"); + waitForListenerResponse(); } private void waitForListenerResponse() throws CommandException { @@ -146,7 +123,7 @@ this.agentInfoDAO = agentInfoDAO; } - public void setRequestQueue(RequestQueue requestQueue) { - this.requestQueue = requestQueue; + public void setKillVMRequest(KillVMRequest request) { + this.request = request; } }
--- a/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/internal/Activator.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/internal/Activator.java Mon Oct 27 12:48:15 2014 -0400 @@ -44,6 +44,7 @@ import org.osgi.framework.ServiceRegistration; import com.redhat.thermostat.client.command.RequestQueue; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.cli.Command; import com.redhat.thermostat.killvm.command.KillVMCommand; @@ -63,6 +64,7 @@ HostInfoDAO.class, AgentInfoDAO.class, VmInfoDAO.class, + KillVMRequest.class, RequestQueue.class, }; @@ -72,12 +74,12 @@ HostInfoDAO hostDAO = (HostInfoDAO) services.get(HostInfoDAO.class.getName()); AgentInfoDAO agentDao = (AgentInfoDAO) services.get(AgentInfoDAO.class.getName()); VmInfoDAO vmDao = (VmInfoDAO) services.get(VmInfoDAO.class.getName()); - RequestQueue requestQueue = (RequestQueue) services.get(RequestQueue.class.getName()); + KillVMRequest request= (KillVMRequest) services.get(KillVMRequest.class.getName()); command.setAgentInfoDAO(agentDao); command.setHostInfoDAO(hostDAO); command.setVmInfoDAO(vmDao); - command.setRequestQueue(requestQueue); + command.setKillVMRequest(request); } @Override @@ -85,7 +87,7 @@ command.setAgentInfoDAO(null); command.setHostInfoDAO(null); command.setVmInfoDAO(null); - command.setRequestQueue(null); + command.setKillVMRequest(null); } });
--- a/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/locale/LocaleResources.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/locale/LocaleResources.java Mon Oct 27 12:48:15 2014 -0400 @@ -43,7 +43,7 @@ HOST_SERVICE_UNAVAILABLE, VM_SERVICE_UNAVAILABLE, AGENT_SERVICE_UNAVAILABLE, - QUEUE_SERVICE_UNAVAILABLE, + REQUEST_SERVICE_UNAVAILABLE, KILL_INTERRUPTED, VM_NOT_FOUND;
--- a/killvm/command/src/main/resources/com/redhat/thermostat/killvm/command/locale/strings.properties Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/command/src/main/resources/com/redhat/thermostat/killvm/command/locale/strings.properties Mon Oct 27 12:48:15 2014 -0400 @@ -1,6 +1,6 @@ HOST_SERVICE_UNAVAILABLE = Unable to get host information (HostInfoDAO is unavailable) VM_SERVICE_UNAVAILABLE = Unable to get vm information (VmInfoDAO is unavailable) AGENT_SERVICE_UNAVAILABLE = Unable to get agent information (AgentInfoDAO is unavailable) -QUEUE_SERVICE_UNAVAILABLE = Unable to get queue information (RequestQueue is unavailable) +REQUEST_SERVICE_UNAVAILABLE = Unable to get request information (KillVMRequest is unavailable) KILL_INTERRUPTED = Command interrupted while waiting for response from murderer VM_NOT_FOUND = VM with ID {0} not found. \ No newline at end of file
--- a/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java Mon Oct 27 12:48:15 2014 -0400 @@ -36,29 +36,25 @@ package com.redhat.thermostat.killvm.command; -import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import com.redhat.thermostat.client.command.RequestQueue; import com.redhat.thermostat.common.cli.CommandContext; import com.redhat.thermostat.common.cli.CommandException; import com.redhat.thermostat.common.cli.SimpleArguments; -import com.redhat.thermostat.common.command.Request; +import com.redhat.thermostat.common.command.RequestResponseListener; import com.redhat.thermostat.killvm.command.internal.ShellVMKilledListener; -import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.DAOException; import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; import com.redhat.thermostat.storage.model.VmInfo; import com.redhat.thermostat.test.TestCommandContextFactory; @@ -72,11 +68,7 @@ private AgentInfoDAO agentInfoDAO; private ShellVMKilledListener listener; - private RequestQueue requestQueue; - - private HostRef hostRef; - private VmRef vmRef; - + private KillVMRequest request; @Before public void setup() { @@ -87,59 +79,37 @@ agentInfoDAO = mock(AgentInfoDAO.class); listener = mock(ShellVMKilledListener.class); - requestQueue = mock(RequestQueue.class); + request = mock(KillVMRequest.class); cmd = new KillVMCommand(listener); - cmd.setVmInfoDAO(vmInfoDAO); - cmd.setAgentInfoDAO(agentInfoDAO); - cmd.setHostInfoDAO(hostInfoDAO); - cmd.setRequestQueue(requestQueue); - - hostRef = new HostRef("10", "dummy"); - vmRef = new VmRef(hostRef, "liveVM", -1, "dummy"); - - VmInfo vmInfo = mock(VmInfo.class); - when(vmInfo.getVmPid()).thenReturn(-1); - - when(vmInfoDAO.getVmInfo(vmRef)).thenReturn(vmInfo); } @Test public void testKillLiveVM() throws CommandException, InterruptedException { String vmId = "liveVM"; - - AgentInformation agent = mock(AgentInformation.class); - when(agent.getConfigListenAddress()).thenReturn("addr:10"); - when(agentInfoDAO.getAgentInformation(hostRef)).thenReturn(agent); - - CommandContext ctx = createContext(vmId, hostRef.getAgentId()); - - final boolean[] complete = {false}; + String hostId = "hostId"; - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - complete[0] = true; - return null; - } - }).when(requestQueue).putRequest(any(Request.class)); + VmInfo vmInfo = mock(VmInfo.class); + when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenReturn(vmInfo); + CommandContext ctx = createContext(vmId, hostId); + + setServices(); cmd.run(ctx); - assertTrue(complete[0]); + verify(request).sendKillVMRequestToAgent(any(VmRef.class), any(AgentInfoDAO.class), any(RequestResponseListener.class)); } @Test(expected = CommandException.class) public void testKillNonexistentVM() throws CommandException { String vmId = "nonexistentVM"; - CommandContext ctx = createContext(vmId, hostRef.getAgentId()); - cmd.run(ctx); - } + String hostId = "hostId"; + + when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenThrow(DAOException.class); - @Test(expected = CommandException.class) - public void testKillNonexistentHost() throws CommandException { - String vmId = "liveVM"; - CommandContext ctx = createContext(vmId, "nonexistentHost"); + CommandContext ctx = createContext(vmId, hostId); + setServices(); + cmd.run(ctx); } @@ -148,7 +118,7 @@ SimpleArguments args = new SimpleArguments(); args.addArgument("hostId", "hostId"); CommandContext ctx = cmdCtxFactory.createContext(args); - + setServices(); cmd.run(ctx); } @@ -167,4 +137,12 @@ args.addArgument("hostId", hostId); return cmdCtxFactory.createContext(args); } + + private void setServices() { + cmd.setVmInfoDAO(vmInfoDAO); + cmd.setAgentInfoDAO(agentInfoDAO); + cmd.setHostInfoDAO(hostInfoDAO); + cmd.setKillVMRequest(request); + + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/pom.xml Mon Oct 27 12:48:15 2014 -0400 @@ -0,0 +1,123 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright 2012-2014 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. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>thermostat-killvm</artifactId> + <groupId>com.redhat.thermostat</groupId> + <version>1.1.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>thermostat-killvm-common</artifactId> + <packaging>bundle</packaging> + + <name>Thermostat KillVM Common</name> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> + <Bundle-Activator>com.redhat.thermostat.killvm.common.internal.Activator</Bundle-Activator> + <Bundle-SymbolicName>com.redhat.thermostat.killvm.common</Bundle-SymbolicName> + <Export-Package> + com.redhat.thermostat.killvm.common, + </Export-Package> + <Private-Package> + com.redhat.thermostat.killvm.common.internal, + </Private-Package> + <!-- Do not autogenerate uses clauses in Manifests --> + <_nouses>true</_nouses> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.framework</artifactId> + <version>4.2.0</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-client-command</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-storage-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-common-test</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/main/java/com/redhat/thermostat/killvm/common/KillVMRequest.java Mon Oct 27 12:48:15 2014 -0400 @@ -0,0 +1,75 @@ +/* + * Copyright 2012-2014 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.killvm.common; + +import java.net.InetSocketAddress; + +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.storage.core.VmRef; +import com.redhat.thermostat.storage.dao.AgentInfoDAO; + +public class KillVMRequest { + private static final String RECEIVER = "com.redhat.thermostat.killvm.agent.internal.KillVmReceiver"; + private static final String CMD_CHANNEL_ACTION_NAME = "killvm"; + + private RequestQueue queue; + + public KillVMRequest(RequestQueue queue) { + this.queue = queue; + } + + public void sendKillVMRequestToAgent(VmRef vmRef, AgentInfoDAO agentInfoDAO, RequestResponseListener listener) { + String address = agentInfoDAO.getAgentInformation(vmRef.getHostRef()).getConfigListenAddress(); + + String [] host = address.split(":"); + InetSocketAddress target = new InetSocketAddress(host[0], Integer.parseInt(host[1])); + Request murderer = getKillRequest(target); + murderer.setParameter(Request.ACTION, CMD_CHANNEL_ACTION_NAME); + murderer.setParameter("vm-pid", String.valueOf(vmRef.getPid())); + murderer.setReceiver(RECEIVER); + murderer.addListener(listener); + + queue.putRequest(murderer); + } + + // for testing + Request getKillRequest(InetSocketAddress target) { + return new Request(Request.RequestType.NO_RESPONSE_EXPECTED, target); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/main/java/com/redhat/thermostat/killvm/common/internal/Activator.java Mon Oct 27 12:48:15 2014 -0400 @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2014 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.killvm.common.internal; + +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.client.command.RequestQueue; +import com.redhat.thermostat.killvm.common.KillVMRequest; + +public class Activator implements BundleActivator { + + + private ServiceTracker tracker; + + @Override + public void start(final BundleContext context) throws Exception { + tracker = new ServiceTracker(context, RequestQueue.class, null) { + @Override + public Object addingService(ServiceReference reference) { + + RequestQueue requestqueue = (RequestQueue) context.getService(reference); + + KillVMRequest gcRequest = new KillVMRequest(requestqueue); + context.registerService(KillVMRequest.class, gcRequest, null); + return super.addingService(reference); + } + + @Override + public void removedService(ServiceReference reference, Object service) { + + context.ungetService(reference); + super.removedService(reference, service); + } + }; + + tracker.open(); + } + + @Override + public void stop(BundleContext context) throws Exception { + tracker.close(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/test/java/com/redhat/thermostat/killvm/common/KillVMRequestTest.java Mon Oct 27 12:48:15 2014 -0400 @@ -0,0 +1,122 @@ +/* + * Copyright 2012-2014 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.killvm.common; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.InetSocketAddress; + +import org.junit.Before; +import org.junit.Test; + +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.storage.core.HostRef; +import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.model.AgentInformation; + +public class KillVMRequestTest { + + private AgentInfoDAO agentDAO; + private RequestQueue queue; + private VmRef vm; + + private KillVMRequest killVMRequest; + private Request request; + private RequestResponseListener listener; + + @Before + public void setup() { + agentDAO = mock(AgentInfoDAO.class); + vm = mock(VmRef.class); + + request = mock(Request.class); + + listener = mock(RequestResponseListener.class); + + HostRef ref = mock(HostRef.class); + when(vm.getHostRef()).thenReturn(ref); + when(vm.getPid()).thenReturn(123456); + + AgentInformation info = mock(AgentInformation.class); + when(info.getConfigListenAddress()).thenReturn("0.0.42.42:42"); + + when(agentDAO.getAgentInformation(ref)).thenReturn(info); + + queue = mock(RequestQueue.class); + } + + @Test + public void testSendKillVMRequestToAgent() { + final boolean [] results = new boolean [3]; + killVMRequest = new KillVMRequest(queue) { + @Override + Request getKillRequest(InetSocketAddress target) { + results[0] = true; + if (target.getHostString().equals("0.0.42.42")) { + results[1] = true; + } + if (target.getPort() == 42) { + results[2] = true; + } + + return request; + } + }; + + killVMRequest.sendKillVMRequestToAgent(vm, agentDAO, listener); + verify(vm).getHostRef(); + verify(vm).getPid(); + + assertTrue(results[0]); + assertTrue(results[1]); + assertTrue(results[2]); + + verify(request).setReceiver("com.redhat.thermostat.killvm.agent.internal.KillVmReceiver"); + verify(request).setParameter(Request.ACTION, "killvm"); + verify(request).setParameter("vm-pid", "123456"); + verify(request).addListener(listener); + + verify(queue).putRequest(request); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/test/java/com/redhat/thermostat/killvm/common/internal/ActivatorTest.java Mon Oct 27 12:48:15 2014 -0400 @@ -0,0 +1,63 @@ +/* + * Copyright 2012-2014 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.killvm.common.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import org.junit.Test; + +import com.redhat.thermostat.client.command.RequestQueue; +import com.redhat.thermostat.killvm.common.KillVMRequest; +import com.redhat.thermostat.testutils.StubBundleContext; + +public class ActivatorTest { + @Test + public void verifyActivatorRegistersServices() throws Exception { + StubBundleContext context = new StubBundleContext(); + context.registerService(RequestQueue.class.getName(), mock(RequestQueue.class), null); + + Activator activator = new Activator(); + activator.start(context); + + assertTrue(context.isServiceRegistered(KillVMRequest.class.getName(), KillVMRequest.class)); + activator.stop(context); + + assertEquals(0, context.getServiceListeners().size()); + } +}
--- a/killvm/distribution/pom.xml Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/distribution/pom.xml Mon Oct 27 12:48:15 2014 -0400 @@ -93,6 +93,11 @@ </dependency> <dependency> <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-killvm-common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-killvm-agent</artifactId> <version>${project.version}</version> </dependency>
--- a/killvm/distribution/thermostat-plugin.xml Tue Oct 21 14:02:57 2014 -0400 +++ b/killvm/distribution/thermostat-plugin.xml Mon Oct 27 12:48:15 2014 -0400 @@ -65,6 +65,7 @@ </environments> <bundles> <bundle><symbolic-name>com.redhat.thermostat.killvm.command</symbolic-name><version>${project.version}</version></bundle> + <bundle><symbolic-name>com.redhat.thermostat.killvm.common</symbolic-name><version>${project.version}</version></bundle> <bundle><symbolic-name>com.redhat.thermostat.common.command</symbolic-name><version>${project.version}</version></bundle> <bundle><symbolic-name>com.redhat.thermostat.client.core</symbolic-name><version>${project.version}</version></bundle> <bundle><symbolic-name>com.redhat.thermostat.client.command</symbolic-name><version>${project.version}</version></bundle> @@ -90,6 +91,7 @@ <name>gui</name> <bundles> <bundle><symbolic-name>com.redhat.thermostat.killvm.client</symbolic-name><version>${project.version}</version></bundle> + <bundle><symbolic-name>com.redhat.thermostat.killvm.common</symbolic-name><version>${project.version}</version></bundle> </bundles> </extension> <extension>