Mercurial > hg > release > thermostat-1.4
changeset 1819:2b7c06300955
Add VmIdCompleter
Reviewed-by:omajid
Review-thread:http://icedtea.classpath.org/pipermail/thermostat/2015-June/014225.html
PR2646
author | Lukasz Dracz <ldracz@redhat.com> |
---|---|
date | Mon, 29 Jun 2015 15:36:20 -0400 |
parents | 912b35b5bedd |
children | 7ec7c131b3db |
files | launcher/src/main/java/com/redhat/thermostat/launcher/internal/IdCompleter.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/IdFinder.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/StorageState.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/TreeCompleter.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java |
diffstat | 7 files changed, 295 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/IdCompleter.java Mon Jun 29 15:36:20 2015 -0400 @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2015 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.launcher.internal; + +import java.util.ArrayList; +import java.util.List; + +import jline.console.completer.Completer; +import jline.console.completer.StringsCompleter; + +public class IdCompleter implements Completer { + + private IdFinder finder; + private final StorageState storageState; + + public IdCompleter(IdFinder finder, StorageState storageState) { + this.finder = finder; + this.storageState = storageState; + } + + @Override + public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) { + List<String> ids = new ArrayList<>(); + + if (storageState.isStorageConnected()) { + ids = finder.findIds(); + } + + StringsCompleter idsCompleter = new StringsCompleter(ids); + return idsCompleter.complete(buffer, cursor, candidates); + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/IdFinder.java Mon Jun 29 15:36:20 2015 -0400 @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2015 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.launcher.internal; + +import java.util.List; + +public interface IdFinder { + + public List<String> findIds(); +}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java Wed Jun 24 10:51:15 2015 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java Mon Jun 29 15:36:20 2015 -0400 @@ -42,6 +42,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import com.redhat.thermostat.storage.core.Storage; import jline.Terminal; import jline.TerminalFactory; import jline.console.ConsoleReader; @@ -80,6 +81,7 @@ private final ShellPrompt shellPrompt; private CommandInfoSource commandInfoSource; + private final StorageState storageState = new StorageState(); static class HistoryProvider { @@ -155,7 +157,7 @@ private void shellMainLoop(CommandContext ctx, History history, Terminal term) throws IOException, CommandException { ConsoleReader reader = new ConsoleReader(ctx.getConsole().getInput(), ctx.getConsole().getOutput(), term); if (reader.getCompleters().isEmpty() && commandInfoSource != null) { - TabCompletion.setupTabCompletion(reader, commandInfoSource); + TabCompletion.setupTabCompletion(reader, commandInfoSource, bundleContext, storageState); } if (history != null) { reader.setHistory(history); @@ -224,10 +226,12 @@ public void dbServiceAvailable(DbService dbService) { this.shellPrompt.storageConnected(dbService); + storageState.setStorageConnected(true); } public void dbServiceUnavailable() { this.shellPrompt.storageDisconnected(); + storageState.setStorageConnected(false); } public void setCommandInfoSource(final CommandInfoSource source) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/StorageState.java Mon Jun 29 15:36:20 2015 -0400 @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2015 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.launcher.internal; + +public class StorageState { + + private boolean storageConnected = false; + + public void setStorageConnected(boolean isStorageConnected) { + storageConnected = isStorageConnected; + } + + public boolean isStorageConnected() { + return storageConnected; + } + +}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java Wed Jun 24 10:51:15 2015 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TabCompletion.java Mon Jun 29 15:36:20 2015 -0400 @@ -42,8 +42,11 @@ import com.redhat.thermostat.common.utils.LoggingUtils; import jline.console.ConsoleReader; +import jline.console.completer.Completer; import jline.console.completer.FileNameCompleter; +import jline.console.completer.StringsCompleter; import org.apache.commons.cli.Option; +import org.osgi.framework.BundleContext; import static com.redhat.thermostat.launcher.internal.TreeCompleter.createStringNode; @@ -52,7 +55,7 @@ private static final String LONG_OPTION_PREFIX = "--"; private static final String SHORT_OPTION_PREFIX = "-"; - public static void setupTabCompletion(ConsoleReader reader, CommandInfoSource commandInfoSource) { + public static void setupTabCompletion(ConsoleReader reader, CommandInfoSource commandInfoSource, BundleContext context, StorageState storageState) { List<String> logLevels = new ArrayList<>(); for (LoggingUtils.LogLevel level : LoggingUtils.LogLevel.values()) { @@ -70,7 +73,9 @@ for (Option option : (Collection<Option>) info.getOptions().getOptions()) { if (option.getLongOpt().equals("logLevel")) { - setupLogLevelCompletion(logLevels, command, option); + setupCompletion(command, option, new StringsCompleter(logLevels)); + } else if (option.getLongOpt().equals("vmId")) { + setupCompletion(command, option, new IdCompleter(new VmIdsFinder(context), storageState)); } else { setupDefaultCompletion(command, option); } @@ -90,37 +95,39 @@ } private static void setupDefaultCompletion(final TreeCompleter.Node command, final Option option) { - if (option.getLongOpt() != null) { - String optionLongName = LONG_OPTION_PREFIX + option.getLongOpt(); - TreeCompleter.Node defaultNode = createStringNode(optionLongName); - defaultNode.setRestartNode(command); - command.addBranch(defaultNode); - } - if (option.getOpt() != null) { - String optionShortName = SHORT_OPTION_PREFIX + option.getOpt(); + setupDefaultCompletion(command, option.getLongOpt(), LONG_OPTION_PREFIX); + setupDefaultCompletion(command, option.getOpt(), SHORT_OPTION_PREFIX); + } + + private static void setupDefaultCompletion(final TreeCompleter.Node command, final String option, final String prefix) { + if (option != null) { + String optionShortName = prefix + option; TreeCompleter.Node defaultNode = createStringNode(optionShortName); defaultNode.setRestartNode(command); command.addBranch(defaultNode); } } - private static void setupLogLevelCompletion( final List<String> logLevels, final TreeCompleter.Node command, final Option option) { - if (option.getLongOpt() != null) { - String optionLongName = LONG_OPTION_PREFIX + option.getLongOpt(); - TreeCompleter.Node logLevelOption = createStringNode(optionLongName); - TreeCompleter.Node logLevelChoices = createStringNode(logLevels); - logLevelChoices.setRestartNode(command); - logLevelOption.addBranch(logLevelChoices); - command.addBranch(logLevelOption); - } - if (option.getOpt() != null) { - String optionShortName = SHORT_OPTION_PREFIX + option.getOpt(); - TreeCompleter.Node logLevelOption = createStringNode(optionShortName); - TreeCompleter.Node logLevelChoices = createStringNode(logLevels); - logLevelChoices.setRestartNode(command); - logLevelOption.addBranch(logLevelChoices); - command.addBranch(logLevelOption); + private static void setupCompletion(final TreeCompleter.Node command, final Option option, Completer completer) { + setupCompletion(command, completer, option.getLongOpt(), LONG_OPTION_PREFIX); + setupCompletion(command, completer, option.getOpt(), SHORT_OPTION_PREFIX); + } + + private static void setupCompletion(final TreeCompleter.Node command, final Completer completer, final String option, final String prefix) { + if (option != null) { + final String optionName = prefix + option; + TreeCompleter.Node nodeOption = setupCompletionNode(command, optionName, completer); + command.addBranch(nodeOption); } } + private static TreeCompleter.Node setupCompletionNode(final TreeCompleter.Node command, final String optionName, Completer completer) { + TreeCompleter.Node option = createStringNode(optionName); + TreeCompleter.Node choices = new TreeCompleter.Node(completer); + option.addBranch(choices); + option.setRestartNode(command); + choices.setRestartNode(command); + return option; + } + }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TreeCompleter.java Wed Jun 24 10:51:15 2015 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/TreeCompleter.java Mon Jun 29 15:36:20 2015 -0400 @@ -261,10 +261,22 @@ if (node.getBranches().isEmpty()) { return getChildNodesFromRestartNode(node); } + List<CharSequence> candidates = getAllCandidates(node.getBranches()); + if (candidates.isEmpty()) { + return getChildNodesFromRestartNode(node); + } return node.getBranches(); } } + private List<CharSequence> getAllCandidates(final List<Node> nodes) { + List<CharSequence> candidates = new LinkedList<>(); + for (Node node : nodes) { + node.completer.complete("", 0, candidates); + } + return candidates; + } + /** * A class to be used with the TreeCompleter * Each node contains a completer that will be used by the TreeCompleter
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java Mon Jun 29 15:36:20 2015 -0400 @@ -0,0 +1,82 @@ +/* + * Copyright 2012-2015 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.launcher.internal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.storage.dao.HostInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.VmInfo; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +public class VmIdsFinder implements IdFinder { + + private BundleContext context; + + public VmIdsFinder(BundleContext context) { + this.context = context; + } + + @Override + public List<String> findIds() { + List<String> vmIds = new ArrayList<>(); + ServiceReference hostsDAORef = context.getServiceReference(HostInfoDAO.class.getName()); + HostInfoDAO hostsDAO = (HostInfoDAO) context.getService(hostsDAORef); + + Collection<HostRef> hosts = hostsDAO.getHosts(); + context.ungetService(hostsDAORef); + + ServiceReference vmsDAORef = context.getServiceReference(VmInfoDAO.class.getName()); + VmInfoDAO vmsDAO = (VmInfoDAO) context.getService(vmsDAORef); + + for (HostRef host : hosts) { + Collection<VmRef> vms = vmsDAO.getVMs(host); + for (VmRef vm : vms) { + VmInfo info = vmsDAO.getVmInfo(vm); + vmIds.add(info.getVmId()); + } + } + + context.ungetService(vmsDAORef); + return vmIds; + } +}