changeset 2312:5b2cb89debda

Provide filename tab completion for byteman --rules option Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-May/018926.html
author Andrew Azores <aazores@redhat.com>
date Tue, 24 May 2016 12:46:14 -0400
parents 72f214409817
children f85f3a94a8ce
files vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/Activator.java vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanCompleterService.java vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/ActivatorTest.java vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanCompleterServiceTest.java
diffstat 4 files changed, 306 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/Activator.java	Tue May 24 11:42:32 2016 -0400
+++ b/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/Activator.java	Tue May 24 12:46:14 2016 -0400
@@ -39,8 +39,11 @@
 import java.util.Hashtable;
 import java.util.Map;
 
+import com.redhat.thermostat.common.cli.CompleterService;
+import com.redhat.thermostat.common.cli.FileNameTabCompleter;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
 import com.redhat.thermostat.client.command.RequestQueue;
@@ -50,14 +53,20 @@
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
 import com.redhat.thermostat.vm.byteman.common.VmBytemanDAO;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 public class Activator implements BundleActivator {
 
     private ServiceRegistration<Command> commandReg;
-    private MultipleServiceTracker depsTracker;
-    
+    private ServiceRegistration completerRegistration;
+    private MultipleServiceTracker commandDepsTracker;
+    private ServiceTracker fileNameTabCompleterTracker;
+    private BytemanCompleterService completerService;
+
     @Override
-    public void start(BundleContext context) throws Exception {
+    @SuppressWarnings("unchecked")
+    public void start(final BundleContext context) throws Exception {
         Hashtable<String, String> properties = new Hashtable<>();
         properties.put(Command.NAME, BytemanControlCommand.COMMAND_NAME);
         final BytemanControlCommand bytemanCommand = new BytemanControlCommand();
@@ -68,7 +77,7 @@
                 VmBytemanDAO.class,
                 RequestQueue.class,
         };
-        depsTracker = new MultipleServiceTracker(context, deps, new Action() {
+        commandDepsTracker = new MultipleServiceTracker(context, deps, new Action() {
             
             @Override
             public void dependenciesUnavailable() {
@@ -90,13 +99,48 @@
                 bytemanCommand.setRequestQueue(queue);
             }
         });
-        depsTracker.open();
+        commandDepsTracker.open();
+
+        completerService = new BytemanCompleterService();
+
+        fileNameTabCompleterTracker = new ServiceTracker(context, FileNameTabCompleter.class, new ServiceTrackerCustomizer() {
+            @Override
+            public Object addingService(ServiceReference serviceReference) {
+                FileNameTabCompleter fileNameTabCompleter = (FileNameTabCompleter) context.getService(serviceReference);
+                completerService.setFileNameTabCompleter(fileNameTabCompleter);
+                completerRegistration = context.registerService(CompleterService.class.getName(), completerService, null);
+                return context.getService(serviceReference);
+            }
+
+            @Override
+            public void modifiedService(ServiceReference serviceReference, Object o) {
+            }
+
+            @Override
+            public void removedService(ServiceReference serviceReference, Object o) {
+                completerService.setFileNameTabCompleter(null);
+            }
+        });
+        fileNameTabCompleterTracker.open();
     }
 
     @Override
     public void stop(BundleContext context) throws Exception {
-        depsTracker.close();
-        commandReg.unregister();
+        if (commandDepsTracker != null) {
+            commandDepsTracker.close();
+        }
+        if (commandReg != null) {
+            commandReg.unregister();
+        }
+        if (fileNameTabCompleterTracker != null) {
+            fileNameTabCompleterTracker.close();
+        }
+        if (completerRegistration != null) {
+            completerRegistration.unregister();
+        }
+        if (completerService != null) {
+            completerService.setFileNameTabCompleter(null);
+        }
     }
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanCompleterService.java	Tue May 24 12:46:14 2016 -0400
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2012-2016 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.byteman.client.cli.internal;
+
+import com.redhat.thermostat.common.cli.AbstractCompleterService;
+import com.redhat.thermostat.common.cli.CliCommandOption;
+import com.redhat.thermostat.common.cli.FileNameTabCompleter;
+import com.redhat.thermostat.common.cli.TabCompleter;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+public class BytemanCompleterService extends AbstractCompleterService {
+
+    static final CliCommandOption RULES_OPTION = new CliCommandOption("r", "rules", true,
+            "a file with Byteman rules to load into a VM", false);
+
+    @Override
+    public Set<String> getCommands() {
+        return Collections.singleton(BytemanControlCommand.COMMAND_NAME);
+    }
+
+    @Override
+    public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() {
+        if (!dependencyServices.hasService(FileNameTabCompleter.class)) {
+            return Collections.emptyMap();
+        }
+        return Collections.singletonMap(RULES_OPTION, dependencyServices.getService(FileNameTabCompleter.class));
+    }
+
+    public void setFileNameTabCompleter(FileNameTabCompleter tabCompleter) {
+        setService(FileNameTabCompleter.class, tabCompleter);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/ActivatorTest.java	Tue May 24 12:46:14 2016 -0400
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2012-2016 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.byteman.client.cli.internal;
+
+import com.redhat.thermostat.common.cli.Command;
+import com.redhat.thermostat.common.cli.CompleterService;
+import com.redhat.thermostat.common.cli.FileNameTabCompleter;
+import com.redhat.thermostat.testutils.StubBundleContext;
+import org.junit.Test;
+
+import java.util.List;
+
+import static com.redhat.thermostat.testutils.Asserts.assertCommandIsRegistered;
+import static com.redhat.thermostat.testutils.Asserts.assertServiceIsRegistered;
+import static org.junit.Assert.assertEquals;
+
+public class ActivatorTest {
+
+    @Test
+    public void testCommandsRegistered() throws Exception {
+        StubBundleContext ctx = new StubBundleContext();
+
+        Activator activator = new Activator();
+
+        activator.start(ctx);
+
+        assertCommandIsRegistered(ctx, "byteman", BytemanControlCommand.class);
+
+        activator.stop(ctx);
+
+        assertEquals(0, ctx.getAllServices().size());
+    }
+
+    @Test
+    public void testCompleterBecomesAvailableWhenFileNameCompleterAppears() throws Exception {
+        StubBundleContext ctx = new StubBundleContext();
+        ctx.registerService(FileNameTabCompleter.class, new StubFileNameTabCompleter(), null);
+        Activator activator = new Activator();
+        activator.start(ctx);
+        assertEquals(3, ctx.getAllServices().size());
+        assertServiceIsRegistered(ctx, FileNameTabCompleter.class, StubFileNameTabCompleter.class);
+        assertServiceIsRegistered(ctx, CompleterService.class, BytemanCompleterService.class);
+        assertServiceIsRegistered(ctx, Command.class, BytemanControlCommand.class);
+    }
+
+    private static class StubFileNameTabCompleter implements FileNameTabCompleter {
+        @Override
+        public int complete(String buffer, int cursor, List<CharSequence> candidates) {
+            return 0;
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanCompleterServiceTest.java	Tue May 24 12:46:14 2016 -0400
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2012-2016 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.vm.byteman.client.cli.internal;
+
+import com.redhat.thermostat.common.cli.CliCommandOption;
+import com.redhat.thermostat.common.cli.FileNameTabCompleter;
+import com.redhat.thermostat.common.cli.TabCompleter;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static com.redhat.thermostat.vm.byteman.client.cli.internal.BytemanCompleterService.RULES_OPTION;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class BytemanCompleterServiceTest {
+
+    private BytemanCompleterService completerService;
+
+    @Before
+    public void setup() {
+        completerService = new BytemanCompleterService();
+        completerService.setFileNameTabCompleter(mock(FileNameTabCompleter.class));
+    }
+
+    @Test
+    public void testOnlyProvidesCompletionForBytemanCommand() {
+        assertThat(completerService.getCommands(), is(equalTo(Collections.singleton(BytemanControlCommand.COMMAND_NAME))));
+    }
+
+    @Test
+    public void testProvidesOnlyOneCompleter() {
+        assertThat(completerService.getOptionCompleters().size(), is(1));
+    }
+
+    @Test
+    public void testProvidesCompletionForRulesArgument() {
+        Map<CliCommandOption, ? extends TabCompleter> map = completerService.getOptionCompleters();
+        assertThat(map.keySet(), is(equalTo(Collections.singleton(RULES_OPTION))));
+        assertThat(RULES_OPTION.getLongOpt(), is("rules"));
+        assertThat(RULES_OPTION.getOpt(), is("r"));
+        assertThat(RULES_OPTION.isRequired(), is(false));
+        assertThat(RULES_OPTION.hasArg(), is(true));
+    }
+
+    @Test
+    public void testCompleterIsNotNull() {
+        Map<CliCommandOption, ? extends TabCompleter> map = completerService.getOptionCompleters();
+        assertThat(map.get(BytemanCompleterService.RULES_OPTION), is(not(equalTo(null))));
+    }
+
+    @Test
+    public void testProvidesNoCompletionWhenFileNameTabCompleterNotAvailable() {
+        completerService.setFileNameTabCompleter(null);
+        Map<CliCommandOption, ? extends TabCompleter> map = completerService.getOptionCompleters();
+        assertThat(map.isEmpty(), is(true));
+    }
+
+}