Mercurial > hg > release > thermostat-1.0
changeset 1312:4ae82c4623a1
"generify" Actions
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008691.html
reviewed-by: omajid
line wrap: on
line diff
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/HostContextAction.java Fri Nov 08 14:23:57 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* - * Copyright 2012, 2013 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.client.ui; - -import com.redhat.thermostat.annotations.ExtensionPoint; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.shared.locale.LocalizedString; -import com.redhat.thermostat.storage.core.HostRef; - -/** - * {@code HostContextAction}s provide actions that are associated with hosts and - * can be invoked by users. The exact position and appearance of these - * {@code HostContextAction}s varies based on the implementation. - * <p> - * Plugins can register implementations of this interface as OSGi services to - * provide additional {@code HostContextAction}s. - * <p> - * <h2>Implementation Note</h2> - * <p> - * The following information is specific to the current release and may change - * in a future release. - * <p> - * The swing client uses instances of this interface to provide menu items for - * the Host/VM tree. The menu is shown when a user right clicks a host in the - * Host/VM tree. A menu item for every {@code HostContextAction} is added, if - * the {@link Filter} matches, to this menu. Selecting a menu item invokes the - * appropriate {@code HostContextAction}. - * - * @see VMContextAction - */ -@ExtensionPoint -public interface HostContextAction extends ReferenceContextAction<HostRef> { - - /** - * A user-visible name for this {@code HostContextAction}. This should be - * localized. - */ - @Override - LocalizedString getName(); - - /** - * A user-visible description for this {@code HostContextAction}. This - * should be localized. - */ - @Override - LocalizedString getDescription(); -} -
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/ReferenceContextAction.java Fri Nov 08 14:23:57 2013 +0100 +++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/ReferenceContextAction.java Fri Nov 08 14:44:28 2013 +0100 @@ -36,6 +36,7 @@ package com.redhat.thermostat.client.ui; +import com.redhat.thermostat.annotations.ExtensionPoint; import com.redhat.thermostat.common.Filter; import com.redhat.thermostat.storage.core.Ref; @@ -43,7 +44,8 @@ * A common interface for {@link ContextAction} that can execute commands * based on {@link Ref}erences. */ -public interface ReferenceContextAction<R extends Ref> extends ContextAction { +@ExtensionPoint +public interface ReferenceContextAction extends ContextAction { /** * Invoked when the user selects this {@code ReferenceAction}. @@ -51,11 +53,11 @@ * @param reference the host on which this {@code ReferenceAction} was * invoked on. */ - void execute(R reference); + void execute(Ref reference); /** * The {@link Filter} returned by this method is used to select what * {@link Ref} this {@code ReferenceAction} is applicable to. */ - Filter<R> getFilter(); + Filter<Ref> getFilter(); }
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/VMContextAction.java Fri Nov 08 14:23:57 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -/* - * Copyright 2012, 2013 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.client.ui; - -import com.redhat.thermostat.annotations.ExtensionPoint; -import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.shared.locale.LocalizedString; -import com.redhat.thermostat.storage.core.VmRef; - -/** - * {@code VMContextAction}s provide actions that are associated with Java - * Virtual Machines and can be invoked by users. The exact position and - * appearance of these {@code VMContextAction}s varies based on the client - * implementation. - * <p> - * Plugins can register implementation of this interface as OSGi services to - * provide additional {@code VMContextAction}s. - * <p> - * <h2>Implementation Note</h2> - * <p> - * The following information is specific to the current release and may change - * in a future release. - * <p> - * The swing client uses instances of this class to provide menu items in the - * Host/VM tree. The menu is shown when a user right-clicks a VM in the Host/VM - * tree. A menu item for every {@link VMContextAction} is added, if the - * {@code Filter} matches, to this menu. Selecting a menu item invokes the - * corresponding {@code VMContextAction}. - * - * @see HostContextAction - */ -@ExtensionPoint -public interface VMContextAction extends ReferenceContextAction<VmRef> { - - /** - * A user-visible name for this {@code VMContextAction}. Should be - * localized. - */ - @Override - public LocalizedString getName(); - - /** - * A user-visible description for {@code VMContextAction}. Should be - * localized. - */ - @Override - public LocalizedString getDescription(); -} -
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImpl.java Fri Nov 08 14:23:57 2013 +0100 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImpl.java Fri Nov 08 14:44:28 2013 +0100 @@ -56,9 +56,8 @@ import com.redhat.thermostat.client.core.views.HostInformationViewProvider; import com.redhat.thermostat.client.core.views.SummaryViewProvider; import com.redhat.thermostat.client.core.views.VmInformationViewProvider; -import com.redhat.thermostat.client.swing.internal.osgi.HostContextActionServiceTracker; +import com.redhat.thermostat.client.swing.internal.osgi.ContextActionServiceTracker; import com.redhat.thermostat.client.swing.internal.osgi.InformationServiceTracker; -import com.redhat.thermostat.client.swing.internal.osgi.VMContextActionServiceTracker; import com.redhat.thermostat.client.swing.internal.registry.decorator.DecoratorRegistryController; import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextActionController; import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextHandler; @@ -120,8 +119,7 @@ private ClientConfigViewProvider clientConfigViewProvider; private InformationServiceTracker infoServiceTracker; - private HostContextActionServiceTracker hostContextActionTracker; - private VMContextActionServiceTracker vmContextActionTracker; + private ContextActionServiceTracker contextActionTracker; private MultipleServiceTracker depTracker; private CountDownLatch shutdown; @@ -206,11 +204,8 @@ this.infoServiceTracker = new InformationServiceTracker(context); this.infoServiceTracker.open(); - this.hostContextActionTracker = new HostContextActionServiceTracker(context); - this.hostContextActionTracker.open(); - - this.vmContextActionTracker = new VMContextActionServiceTracker(context); - this.vmContextActionTracker.open(); + this.contextActionTracker = new ContextActionServiceTracker(context); + this.contextActionTracker.open(); this.shutdown = shutdown; @@ -367,8 +362,7 @@ depTracker.close(); infoServiceTracker.close(); - hostContextActionTracker.close(); - vmContextActionTracker.close(); + contextActionTracker.close(); } private void installListenersAndStartRegistries() { @@ -394,9 +388,7 @@ private void setUpActionControllers() { ContextActionController contextController = view.getContextActionController(); - ContextHandler handler = - new ContextHandler(hostContextActionTracker, - vmContextActionTracker); + ContextHandler handler = new ContextHandler(contextActionTracker); contextController.addContextActionListener(handler); handler.addContextHandlerActionListener(contextController); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/osgi/ContextActionServiceTracker.java Fri Nov 08 14:44:28 2013 +0100 @@ -0,0 +1,79 @@ +/* + * Copyright 2012, 2013 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.client.swing.internal.osgi; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; + +import com.redhat.thermostat.client.ui.ReferenceContextAction; + +@SuppressWarnings("rawtypes") +public class ContextActionServiceTracker extends ServiceTracker { + + private List<ReferenceContextAction> hostContextActions; + + @SuppressWarnings("unchecked") + public ContextActionServiceTracker(BundleContext context) { + super(context, ReferenceContextAction.class.getName(), null); + this.hostContextActions = new CopyOnWriteArrayList<>(); + } + + @Override + public Object addingService(ServiceReference reference) { + @SuppressWarnings("unchecked") + ReferenceContextAction service = (ReferenceContextAction) super.addingService(reference); + hostContextActions.add(service); + return service; + } + + @SuppressWarnings("unchecked") + @Override + public void removedService(ServiceReference reference, Object service) { + hostContextActions.remove((ReferenceContextAction)service); + super.removedService(reference, service); + } + + public List<ReferenceContextAction> getActions() { + return new ArrayList<>(hostContextActions); + } +} +
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/osgi/HostContextActionServiceTracker.java Fri Nov 08 14:23:57 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * Copyright 2012, 2013 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.client.swing.internal.osgi; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; - -import com.redhat.thermostat.client.ui.HostContextAction; - -@SuppressWarnings("rawtypes") -public class HostContextActionServiceTracker extends ServiceTracker implements ReferenceContextActionProvider { - - private List<HostContextAction> hostContextActions; - - @SuppressWarnings("unchecked") - public HostContextActionServiceTracker(BundleContext context) { - super(context, HostContextAction.class.getName(), null); - this.hostContextActions = new CopyOnWriteArrayList<>(); - } - - @Override - public Object addingService(ServiceReference reference) { - @SuppressWarnings("unchecked") - HostContextAction service = (HostContextAction) super.addingService(reference); - hostContextActions.add(service); - return service; - } - - @SuppressWarnings("unchecked") - @Override - public void removedService(ServiceReference reference, Object service) { - hostContextActions.remove((HostContextAction)service); - super.removedService(reference, service); - } - - @Override - public List<HostContextAction> getActions() { - return new ArrayList<>(hostContextActions); - } -} -
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/osgi/ReferenceContextActionProvider.java Fri Nov 08 14:23:57 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -/* - * Copyright 2012, 2013 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.client.swing.internal.osgi; - -import java.util.List; - -import com.redhat.thermostat.client.ui.ReferenceContextAction; - -public interface ReferenceContextActionProvider { - - public List<? extends ReferenceContextAction> getActions(); -}
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/osgi/VMContextActionServiceTracker.java Fri Nov 08 14:23:57 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * Copyright 2012, 2013 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.client.swing.internal.osgi; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; - -import com.redhat.thermostat.client.ui.VMContextAction; - -@SuppressWarnings("rawtypes") -public class VMContextActionServiceTracker extends ServiceTracker implements ReferenceContextActionProvider { - - private List<VMContextAction> vmContextActions; - - @SuppressWarnings("unchecked") - public VMContextActionServiceTracker(BundleContext context) { - super(context, VMContextAction.class.getName(), null); - this.vmContextActions = new CopyOnWriteArrayList<>(); - } - - @Override - public Object addingService(ServiceReference reference) { - @SuppressWarnings("unchecked") - VMContextAction service = (VMContextAction) super.addingService(reference); - vmContextActions.add(service); - return service; - } - - @SuppressWarnings("unchecked") - @Override - public void removedService(ServiceReference reference, Object service) { - vmContextActions.remove((VMContextAction) service); - super.removedService(reference, service); - } - - @Override - public List<VMContextAction> getActions() { - return new ArrayList<>(vmContextActions); - } -} -
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandler.java Fri Nov 08 14:23:57 2013 +0100 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandler.java Fri Nov 08 14:44:28 2013 +0100 @@ -42,26 +42,24 @@ import javax.swing.SwingUtilities; import com.redhat.thermostat.client.swing.components.ThermostatPopupMenu; -import com.redhat.thermostat.client.swing.internal.osgi.HostContextActionServiceTracker; -import com.redhat.thermostat.client.swing.internal.osgi.VMContextActionServiceTracker; +import com.redhat.thermostat.client.swing.internal.osgi.ContextActionServiceTracker; import com.redhat.thermostat.client.ui.ReferenceContextAction; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ActionNotifier; -import com.redhat.thermostat.storage.core.HostRef; import com.redhat.thermostat.storage.core.Ref; /** * */ -public class ContextHandler implements ActionListener<ContextActionController.ContextAction>{ +public class ContextHandler implements ActionListener<ContextActionController.ContextAction> { public enum ContextHandlerAction { ACTION_PERFORMED, } - public static class Payload<R extends Ref> { - ReferenceContextAction<R> action; + public static class Payload { + ReferenceContextAction action; Ref reference; } @@ -69,14 +67,10 @@ private ThermostatPopupMenu contextMenu; - private HostContextActionServiceTracker hostContextActionTracker; - private VMContextActionServiceTracker vmContextActionTracker; + private ContextActionServiceTracker contextActionTracker; - public ContextHandler(HostContextActionServiceTracker hostContextActionTracker, - VMContextActionServiceTracker vmContextActionTracker) - { - this.hostContextActionTracker = hostContextActionTracker; - this.vmContextActionTracker = vmContextActionTracker; + public ContextHandler(ContextActionServiceTracker contextActionTracker) { + this.contextActionTracker = contextActionTracker; notifier = new ActionNotifier<>(this); } @@ -84,7 +78,6 @@ public void actionPerformed(final ActionEvent<ContextActionController.ContextAction> actionEvent) { SwingUtilities.invokeLater(new Runnable() { - @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void run() { if (contextMenu == null) { @@ -94,13 +87,8 @@ final ContextActionController.Payload payload = (ContextActionController.Payload) actionEvent.getPayload(); - List<? extends ReferenceContextAction> actions = null; - if (payload.ref instanceof HostRef) { - actions = hostContextActionTracker.getActions(); - } else { - actions = vmContextActionTracker.getActions(); - } - + List<? extends ReferenceContextAction> actions = + contextActionTracker.getActions(); contextMenu.removeAll(); boolean showPopup = false;
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImplTest.java Fri Nov 08 14:23:57 2013 +0100 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImplTest.java Fri Nov 08 14:44:28 2013 +0100 @@ -36,7 +36,6 @@ package com.redhat.thermostat.client.swing.internal; -import static org.mockito.Matchers.isA; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; @@ -66,21 +65,17 @@ import com.redhat.thermostat.client.swing.internal.registry.decorator.DecoratorRegistryController; import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextActionController; import com.redhat.thermostat.client.swing.internal.vmlist.controller.HostTreeController; -import com.redhat.thermostat.client.ui.HostContextAction; import com.redhat.thermostat.client.ui.MenuAction; import com.redhat.thermostat.client.ui.MenuRegistry; -import com.redhat.thermostat.client.ui.VMContextAction; +import com.redhat.thermostat.client.ui.ReferenceContextAction; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ApplicationService; -import com.redhat.thermostat.common.Filter; import com.redhat.thermostat.common.ThermostatExtensionRegistry; import com.redhat.thermostat.common.ThermostatExtensionRegistry.Action; import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.TimerFactory; import com.redhat.thermostat.shared.locale.LocalizedString; -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.BackendInfoDAO; import com.redhat.thermostat.storage.dao.HostInfoDAO; @@ -103,10 +98,6 @@ private HostInfoDAO mockHostsDAO; private VmInfoDAO mockVmsDAO; - private HostContextAction hostContextAction1; - private VMContextAction vmContextAction1; - private VMContextAction vmContextAction2; - private HostFilterRegistry hostFilterRegistry; private VmFilterRegistry vmFilterRegistry; @@ -223,50 +214,11 @@ ArgumentCaptor<ActionListener> grabInfoRegistry = ArgumentCaptor.forClass(ActionListener.class); doNothing().when(vmInfoRegistry).addActionListener(grabInfoRegistry.capture()); - setUpHostContextActions(); - setUpVMContextActions(); - controller = new MainWindowControllerImpl(context, appSvc, view, registryFactory, shutdown); l = grabListener.getValue(); } - private void setUpHostContextActions() { - hostContextAction1 = mock(HostContextAction.class); - @SuppressWarnings("unchecked") - Filter<HostRef> hostFilter1 = mock(Filter.class); - when(hostFilter1.matches(isA(HostRef.class))).thenReturn(true); - - when(hostContextAction1.getName()).thenReturn(new LocalizedString("action1")); - when(hostContextAction1.getDescription()).thenReturn(new LocalizedString("action1desc")); - when(hostContextAction1.getFilter()).thenReturn(hostFilter1); - - context.registerService(HostContextAction.class, hostContextAction1, null); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - private void setUpVMContextActions() { - vmContextAction1 = mock(VMContextAction.class); - Filter action1Filter = mock(Filter.class); - when(action1Filter.matches(isA(VmRef.class))).thenReturn(true); - - when(vmContextAction1.getName()).thenReturn(new LocalizedString("action1")); - when(vmContextAction1.getDescription()).thenReturn(new LocalizedString("action1desc")); - when(vmContextAction1.getFilter()).thenReturn(action1Filter); - - context.registerService(VMContextAction.class, vmContextAction1, null); - - vmContextAction2 = mock(VMContextAction.class); - Filter action2Filter = mock(Filter.class); - when(action2Filter.matches(isA(VmRef.class))).thenReturn(false); - - when(vmContextAction2.getName()).thenReturn(new LocalizedString("action2")); - when(vmContextAction2.getDescription()).thenReturn(new LocalizedString("action2desc")); - when(vmContextAction2.getFilter()).thenReturn(action2Filter); - - context.registerService(VMContextAction.class, vmContextAction2, null); - } - @After public void tearDown() { view = null;
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/osgi/HostContextActionServiceTrackerTest.java Fri Nov 08 14:23:57 2013 +0100 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/osgi/HostContextActionServiceTrackerTest.java Fri Nov 08 14:44:28 2013 +0100 @@ -43,7 +43,7 @@ import org.junit.Test; import org.osgi.framework.ServiceRegistration; -import com.redhat.thermostat.client.ui.HostContextAction; +import com.redhat.thermostat.client.ui.ReferenceContextAction; import com.redhat.thermostat.testutils.StubBundleContext; public class HostContextActionServiceTrackerTest { @@ -52,10 +52,10 @@ public void verifyHostActionIsAddedToAndRemovedFromUiModel() { StubBundleContext bundleContext = new StubBundleContext(); - HostContextAction hostAction = mock(HostContextAction.class); - ServiceRegistration registration = bundleContext.registerService(HostContextAction.class, hostAction, null); + ReferenceContextAction hostAction = mock(ReferenceContextAction.class); + ServiceRegistration registration = bundleContext.registerService(ReferenceContextAction.class, hostAction, null); - HostContextActionServiceTracker tracker = new HostContextActionServiceTracker(bundleContext); + ContextActionServiceTracker tracker = new ContextActionServiceTracker(bundleContext); tracker.open(); assertTrue(tracker.getActions().contains(hostAction));
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/osgi/VMContextActionServiceTrackerTest.java Fri Nov 08 14:23:57 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -/* - * Copyright 2012, 2013 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.client.swing.internal.osgi; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; - -import org.junit.Test; -import org.osgi.framework.ServiceRegistration; - -import com.redhat.thermostat.client.ui.VMContextAction; -import com.redhat.thermostat.testutils.StubBundleContext; - -public class VMContextActionServiceTrackerTest { - - @Test - public void verifyHostActionIsAddedToAndRemovedFromUiModel() { - StubBundleContext bundleContext = new StubBundleContext(); - - VMContextAction vmAction = mock(VMContextAction.class); - ServiceRegistration registration = bundleContext.registerService(VMContextAction.class, vmAction, null); - - VMContextActionServiceTracker tracker = new VMContextActionServiceTracker(bundleContext); - tracker.open(); - - assertTrue(tracker.getActions().contains(vmAction)); - - registration.unregister(); - - tracker.close(); - - assertFalse(tracker.getActions().contains(vmAction)); - } - -} -
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandlerTest.java Fri Nov 08 14:23:57 2013 +0100 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandlerTest.java Fri Nov 08 14:44:28 2013 +0100 @@ -44,7 +44,6 @@ import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.doNothing; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -65,23 +64,22 @@ import com.redhat.thermostat.client.swing.components.ThermostatPopupMenu; import com.redhat.thermostat.client.swing.internal.accordion.AccordionComponent; -import com.redhat.thermostat.client.swing.internal.osgi.HostContextActionServiceTracker; -import com.redhat.thermostat.client.swing.internal.osgi.VMContextActionServiceTracker; +import com.redhat.thermostat.client.swing.internal.osgi.ContextActionServiceTracker; import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextHandler.ContextHandlerAction; import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextHandler.Payload; -import com.redhat.thermostat.client.ui.HostContextAction; +import com.redhat.thermostat.client.ui.ReferenceContextAction; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.Filter; import com.redhat.thermostat.shared.locale.LocalizedString; import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.storage.core.Ref; /** * */ public class ContextHandlerTest { - private HostContextActionServiceTracker hostContextActionTracker; - private VMContextActionServiceTracker vmContextActionTracker; + private ContextActionServiceTracker contextActionTracker; private ActionEvent actionEvent; @BeforeClass @@ -116,8 +114,7 @@ @Before public void setUp() { - hostContextActionTracker = mock(HostContextActionServiceTracker.class); - vmContextActionTracker = mock(VMContextActionServiceTracker.class); + contextActionTracker = mock(ContextActionServiceTracker.class); actionEvent = mock(ActionEvent.class); } @@ -155,7 +152,7 @@ TestActionListener testListener = new TestActionListener(); - ContextHandler handler = new ContextHandler(hostContextActionTracker, vmContextActionTracker) { + ContextHandler handler = new ContextHandler(contextActionTracker) { @Override ThermostatPopupMenu createContextPopMenu() { invocations[0]++; @@ -177,8 +174,7 @@ assertEquals(1, invocations[0]); assertEquals(0, invocations[1]); - verify(hostContextActionTracker).getActions(); - verify(vmContextActionTracker, times(0)).getActions(); + verify(contextActionTracker).getActions(); // verify the popup is built from scratch verify(popup).removeAll(); @@ -188,13 +184,13 @@ // *** test two actions, no filter // no reason to change the event, but add actions - Filter<HostRef> hostFilter = mock(Filter.class); + Filter<Ref> hostFilter = mock(Filter.class); when(hostFilter.matches(host0)).thenReturn(true).thenReturn(true); LocalizedString name0 = new LocalizedString("actionName0"); LocalizedString des0 = new LocalizedString("actionDesc0"); - HostContextAction hostAction0 = mock(HostContextAction.class); + ReferenceContextAction hostAction0 = mock(ReferenceContextAction.class); when(hostAction0.getFilter()).thenReturn(hostFilter); when(hostAction0.getName()).thenReturn(name0); when(hostAction0.getDescription()).thenReturn(des0); @@ -202,16 +198,16 @@ LocalizedString name1 = new LocalizedString("actionName1"); LocalizedString des1 = new LocalizedString("actionDesc1"); - HostContextAction hostAction1 = mock(HostContextAction.class); + ReferenceContextAction hostAction1 = mock(ReferenceContextAction.class); when(hostAction1.getFilter()).thenReturn(hostFilter); when(hostAction1.getName()).thenReturn(name1); when(hostAction1.getDescription()).thenReturn(des1); - List<HostContextAction> hostActions = new ArrayList<>(); + List<ReferenceContextAction> hostActions = new ArrayList<>(); hostActions.add(hostAction0); hostActions.add(hostAction1); - when(hostContextActionTracker.getActions()).thenReturn(hostActions); + when(contextActionTracker.getActions()).thenReturn(hostActions); handler.actionPerformed(actionEvent); waitForSwing();
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/Activator.java Fri Nov 08 14:23:57 2013 +0100 +++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/Activator.java Fri Nov 08 14:44:28 2013 +0100 @@ -43,7 +43,8 @@ import org.osgi.framework.ServiceRegistration; import com.redhat.thermostat.client.command.RequestQueue; -import com.redhat.thermostat.client.ui.VMContextAction; +import com.redhat.thermostat.client.ui.ReferenceContextAction; + import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.MultipleServiceTracker.Action; import com.redhat.thermostat.storage.dao.AgentInfoDAO; @@ -69,7 +70,7 @@ 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()); - killActionRegistration = context.registerService(VMContextAction.class.getName(), service, null); + killActionRegistration = context.registerService(ReferenceContextAction.class.getName(), service, null); } @Override
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java Fri Nov 08 14:23:57 2013 +0100 +++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java Fri Nov 08 14:44:28 2013 +0100 @@ -41,13 +41,14 @@ import com.redhat.thermostat.client.command.RequestQueue; import com.redhat.thermostat.common.Filter; -import com.redhat.thermostat.client.ui.VMContextAction; +import com.redhat.thermostat.client.ui.ReferenceContextAction; import com.redhat.thermostat.common.command.Request; 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.shared.locale.LocalizedString; import com.redhat.thermostat.shared.locale.Translate; +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; @@ -57,7 +58,7 @@ * Implements the {@link VMContextAction} entry point to provide a kill switch * for the currently selected Virtual Machine. */ -public class KillVMAction implements VMContextAction { +public class KillVMAction implements ReferenceContextAction { private static final String RECEIVER = "com.redhat.thermostat.killvm.agent.internal.KillVmReceiver"; private static final String CMD_CHANNEL_ACTION_NAME = "killvm"; @@ -87,7 +88,14 @@ } @Override - public void execute(VmRef reference) { + 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(":"); @@ -107,16 +115,22 @@ } @Override - public Filter<VmRef> getFilter() { + public Filter<Ref> getFilter() { return new LocalAndAliveFilter(); } - private class LocalAndAliveFilter extends Filter<VmRef> { - + private class LocalAndAliveFilter extends Filter<Ref> { @Override - public boolean matches(VmRef ref) { - VmInfo vmInfo = vmDao.getVmInfo(ref); - return vmInfo.isAlive(); + public boolean matches(Ref ref) { + boolean match = false; + + if (ref instanceof VmRef) { + VmRef reference = (VmRef) ref; + VmInfo vmInfo = vmDao.getVmInfo(reference); + match = vmInfo.isAlive(); + } + + return match; } }
--- a/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/ActivatorTest.java Fri Nov 08 14:23:57 2013 +0100 +++ b/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/ActivatorTest.java Fri Nov 08 14:44:28 2013 +0100 @@ -44,7 +44,7 @@ import org.junit.Test; import com.redhat.thermostat.client.command.RequestQueue; -import com.redhat.thermostat.client.ui.VMContextAction; +import com.redhat.thermostat.client.ui.ReferenceContextAction; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.testutils.StubBundleContext; @@ -80,7 +80,7 @@ activator.start(context); - assertTrue(context.isServiceRegistered(VMContextAction.class.getName(), KillVMAction.class)); + assertTrue(context.isServiceRegistered(ReferenceContextAction.class.getName(), KillVMAction.class)); activator.stop(context);
--- a/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/KillVMActionTest.java Fri Nov 08 14:23:57 2013 +0100 +++ b/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/KillVMActionTest.java Fri Nov 08 14:44:28 2013 +0100 @@ -55,6 +55,7 @@ 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.Ref; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; @@ -64,6 +65,33 @@ public class KillVMActionTest { @Test + public void killVMFilterDiscardHost() { + AgentInfoDAO agentDao = mock(AgentInfoDAO.class); + VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); + + VmRef matching = mock(VmRef.class); + HostRef notMatching = mock(HostRef.class); + + VmInfo vmInfo = mock(VmInfo.class); + when(vmInfoDao.getVmInfo(matching)).thenReturn(vmInfo); + + RequestQueue queue = mock(RequestQueue.class); + RequestResponseListener listener = mock(RequestResponseListener.class); + + KillVMAction action = new KillVMAction(agentDao, vmInfoDao, queue, listener); + + Filter<Ref> filter = action.getFilter(); + + assertFalse(filter.matches(notMatching)); + + when(vmInfo.isAlive()).thenReturn(true); + assertTrue(filter.matches(matching)); + + when(vmInfo.isAlive()).thenReturn(false); + assertFalse(filter.matches(matching)); + } + + @Test public void killVMFilterOnlyMatchesLiveVMs() { AgentInfoDAO agentDao = mock(AgentInfoDAO.class); VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); @@ -78,7 +106,7 @@ KillVMAction action = new KillVMAction(agentDao, vmInfoDao, queue, listener); - Filter<VmRef> filter = action.getFilter(); + Filter<Ref> filter = action.getFilter(); when(vmInfo.isAlive()).thenReturn(true); assertTrue(filter.matches(matching));