changeset 2322:52d7d3d03fa7

Add tab completion for storage-populator -c Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-May/019019.html
author Andrew Azores <aazores@redhat.com>
date Tue, 31 May 2016 12:43:36 -0400
parents ae02dcfdaab6
children 48fd6f717c4f
files dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/StoragePopulatorCommand.java dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/internal/Activator.java dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/internal/StoragePopulatorCompleterService.java
diffstat 3 files changed, 123 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/StoragePopulatorCommand.java	Mon May 30 18:46:07 2016 +0200
+++ b/dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/StoragePopulatorCommand.java	Tue May 31 12:43:36 2016 -0400
@@ -70,6 +70,8 @@
 
 public class StoragePopulatorCommand extends AbstractCommand {
 
+    public static final String COMMAND_NAME = "storage-populator";
+
     private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
     private static final Logger logger = LoggingUtils.getLogger(StoragePopulatorCommand.class);
 
@@ -191,8 +193,12 @@
      * Package-private to allow overriding for testing.
      */
     File getConfigFile(Arguments args) {
-        return new File(paths.getSystemPluginConfigurationDirectory().getAbsolutePath() +
-                    "/storage-populator/" + args.getArgument(CONFIG_FILE_NAME));
+        return new File(getConfigFileDirectoryPath(paths) + args.getArgument(CONFIG_FILE_NAME));
+    }
+
+    public static String getConfigFileDirectoryPath(CommonPaths paths) {
+        return paths.getSystemPluginConfigurationDirectory().getAbsolutePath() +
+                "/storage-populator/";
     }
 
     /**
--- a/dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/internal/Activator.java	Mon May 30 18:46:07 2016 +0200
+++ b/dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/internal/Activator.java	Tue May 31 12:43:36 2016 -0400
@@ -39,6 +39,7 @@
 import java.util.Hashtable;
 import java.util.Map;
 
+import com.redhat.thermostat.common.cli.CompleterService;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 
@@ -51,13 +52,20 @@
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
 import com.redhat.thermostat.storage.populator.StoragePopulatorCommand;
 import com.redhat.thermostat.thread.dao.ThreadDao;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 public class Activator implements BundleActivator {
 
-    private MultipleServiceTracker tracker;
+    private MultipleServiceTracker cmdDepsTracker;
+    private ServiceTracker completerDepsTracker;
+    private ServiceRegistration completerRegistration;
 
     @Override
-    public void start(BundleContext context) throws Exception {
+    @SuppressWarnings("unchecked")
+    public void start(final BundleContext context) throws Exception {
 
         final StoragePopulatorCommand command = new StoragePopulatorCommand();
         Hashtable<String,String> properties = new Hashtable<>();
@@ -73,7 +81,7 @@
                 ThreadDao.class,
         };
 
-        tracker = new MultipleServiceTracker(context, deps, new MultipleServiceTracker.Action() {
+        cmdDepsTracker = new MultipleServiceTracker(context, deps, new MultipleServiceTracker.Action() {
 
             @Override
             public void dependenciesAvailable(Map<String, Object> services) {
@@ -98,12 +106,37 @@
                 command.setServicesUnavailable();
             }
         });
-        tracker.open();
+        cmdDepsTracker.open();
+
+        final StoragePopulatorCompleterService completerService = new StoragePopulatorCompleterService();
+        completerDepsTracker = new ServiceTracker(context, CommonPaths.class, new ServiceTrackerCustomizer() {
+            @Override
+            public Object addingService(ServiceReference serviceReference) {
+                CommonPaths paths = (CommonPaths) context.getService(serviceReference);
+                completerService.setCommonPaths(paths);
+                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.setCommonPaths(null);
+            }
+        });
+        completerDepsTracker.open();
     }
 
     @Override
     public void stop(BundleContext context) throws Exception {
-        tracker.close();
+        cmdDepsTracker.close();
+        completerDepsTracker.close();
+        if (completerRegistration != null) {
+            completerRegistration.unregister();
+        }
     }
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dev/storage-populator/command/src/main/java/com/redhat/thermostat/storage/populator/internal/StoragePopulatorCompleterService.java	Tue May 31 12:43:36 2016 -0400
@@ -0,0 +1,77 @@
+/*
+ * 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.storage.populator.internal;
+
+import com.redhat.thermostat.common.cli.AbstractCompleterService;
+import com.redhat.thermostat.common.cli.CliCommandOption;
+import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter;
+import com.redhat.thermostat.common.cli.DirectoryContentsCompletionFinder;
+import com.redhat.thermostat.common.cli.TabCompleter;
+import com.redhat.thermostat.shared.config.CommonPaths;
+import com.redhat.thermostat.storage.populator.StoragePopulatorCommand;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+public class StoragePopulatorCompleterService extends AbstractCompleterService {
+
+    static final CliCommandOption CONFIG_OPTION = new CliCommandOption("c", "config", true, "the json config file to use", true);
+
+    @Override
+    public Set<String> getCommands() {
+        return Collections.singleton(StoragePopulatorCommand.COMMAND_NAME);
+    }
+
+    @Override
+    public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() {
+        if (!dependencyServices.hasService(CommonPaths.class)) {
+            return Collections.emptyMap();
+        }
+        CommonPaths paths = dependencyServices.getService(CommonPaths.class);
+        String directoryPath = StoragePopulatorCommand.getConfigFileDirectoryPath(paths);
+        File directory = new File(directoryPath);
+        CompletionFinderTabCompleter completer = new CompletionFinderTabCompleter(new DirectoryContentsCompletionFinder(directory));
+        return Collections.singletonMap(CONFIG_OPTION, completer);
+    }
+
+    public void setCommonPaths(CommonPaths paths) {
+        setService(CommonPaths.class, paths);
+    }
+
+}