Mercurial > hg > thermostat
changeset 1583:17e344385eff
Show connectedness in shell prompt
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-December/011860.html
line wrap: on
line diff
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/Activator.java Fri Nov 28 17:09:31 2014 -0500 +++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/Activator.java Mon Dec 01 08:51:43 2014 -0500 @@ -40,6 +40,9 @@ import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.MultipleServiceTracker.Action; @@ -47,6 +50,7 @@ import com.redhat.thermostat.common.cli.CommandRegistryImpl; import com.redhat.thermostat.common.config.ClientPreferences; import com.redhat.thermostat.shared.config.CommonPaths; +import com.redhat.thermostat.storage.core.DbService; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.BackendInfoDAO; import com.redhat.thermostat.utils.keyring.Keyring; @@ -62,6 +66,9 @@ private MultipleServiceTracker agentInfoTracker; private final AgentInfoCommand agentInfoCommand = new AgentInfoCommand(); + private ServiceTracker dbServiceTracker; + private ShellCommand shellCommand; + @Override public void start(final BundleContext context) throws Exception { reg = new CommandRegistryImpl(context); @@ -84,7 +91,8 @@ CommonPaths paths = (CommonPaths) services.get(CommonPaths.class.getName()); ClientPreferences prefs = new ClientPreferences(paths); reg.registerCommand("connect", new ConnectCommand(prefs, keyring)); - reg.registerCommand("shell", new ShellCommand(context, paths)); + shellCommand = new ShellCommand(context, paths); + reg.registerCommand("shell", shellCommand); } @Override @@ -135,6 +143,25 @@ agentInfoTracker.open(); reg.registerCommand("agent-info", agentInfoCommand); + + dbServiceTracker = new ServiceTracker(context, DbService.class.getName(), new ServiceTrackerCustomizer() { + @Override + public Object addingService(ServiceReference serviceReference) { + shellCommand.dbServiceAvailable(); + return context.getService(serviceReference); + } + + @Override + public void modifiedService(ServiceReference serviceReference, Object o) { + //Do nothing + } + + @Override + public void removedService(ServiceReference serviceReference, Object o) { + shellCommand.dbServiceUnavailable(); + } + }); + dbServiceTracker.open(); } @Override @@ -142,6 +169,7 @@ tracker.close(); listAgentTracker.close(); agentInfoTracker.close(); + dbServiceTracker.close(); reg.unregisterCommands(); }
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java Fri Nov 28 17:09:31 2014 -0500 +++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellCommand.java Mon Dec 01 08:51:43 2014 -0500 @@ -49,7 +49,6 @@ import jline.console.history.PersistentHistory; import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import com.redhat.thermostat.common.Version; @@ -70,13 +69,13 @@ private static final String[] exitKeywords = { "exit", "quit", "q" }; - private static final String PROMPT = "Thermostat > "; - private HistoryProvider historyProvider; private Version version; private BundleContext bundleContext; - + + private final ShellPrompt shellPrompt; + static class HistoryProvider { private CommonPaths paths; @@ -104,6 +103,8 @@ this.historyProvider = provider; this.bundleContext = context; this.version = version; + + this.shellPrompt = new ShellPrompt(); } @Override @@ -150,7 +151,7 @@ private boolean handleConsoleInput(ConsoleReader reader, Console console) throws IOException, CommandException { String line; try { - line = reader.readLine(PROMPT); + line = reader.readLine(shellPrompt.getPrompt()); } catch (IllegalArgumentException iae) { if (iae.getMessage().endsWith(": event not found")) { console.getError().println(iae.getMessage()); @@ -189,5 +190,13 @@ return false; } + public void dbServiceAvailable() { + this.shellPrompt.storageConnected(); + } + + public void dbServiceUnavailable() { + this.shellPrompt.storageDisconnected(); + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/ShellPrompt.java Mon Dec 01 08:51:43 2014 -0500 @@ -0,0 +1,60 @@ +/* + * 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.client.cli.internal; + +public class ShellPrompt { + + private static final String PROMPT_FORMAT = "Thermostat (%s) > "; + + private String connectedToken = "D"; + + public ShellPrompt() { + } + + public String getPrompt() { + return String.format(PROMPT_FORMAT, connectedToken); + } + + public void storageConnected() { + connectedToken = "C"; + } + + public void storageDisconnected() { + connectedToken = "D"; + } + +}
--- a/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ShellCommandTest.java Fri Nov 28 17:09:31 2014 -0500 +++ b/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ShellCommandTest.java Mon Dec 01 08:51:43 2014 -0500 @@ -70,6 +70,8 @@ static private final String VERSION = "Thermostat some version"; static private final String VERSION_OUTPUT = VERSION + "\n"; + static private final String PROMPT = "Thermostat (D) > "; + private ShellCommand cmd; private BundleContext bundleContext; @@ -130,7 +132,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals(VERSION_OUTPUT + "Thermostat > quit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + PROMPT + "quit\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } @@ -141,7 +143,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals(VERSION_OUTPUT + "Thermostat > q\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + PROMPT + "q\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } @@ -152,7 +154,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals(VERSION_OUTPUT + "Thermostat > ", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + PROMPT, ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); } @@ -163,7 +165,7 @@ Arguments args = new SimpleArguments(); CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals(VERSION_OUTPUT + "Thermostat > \nThermostat > exit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + PROMPT + "\n" + PROMPT + "exit\n", ctxFactory.getOutput()); } @Test @@ -187,7 +189,7 @@ CommandContext ctx = ctxFactory.createContext(args); cmd.run(ctx); - assertEquals(VERSION_OUTPUT + "Thermostat > old-history-value\nThermostat > exit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + PROMPT + "old-history-value\n" + PROMPT + "exit\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); verify(launcher).run(new String[] {"old-history-value"}, true); @@ -213,7 +215,7 @@ verify(mockHistory).add("add-to-history"); verify(mockHistory).flush(); - assertEquals(VERSION_OUTPUT + "Thermostat > add-to-history\nThermostat > exit\n", ctxFactory.getOutput()); + assertEquals(VERSION_OUTPUT + PROMPT + "add-to-history\n" + PROMPT + "exit\n", ctxFactory.getOutput()); assertEquals("", ctxFactory.getError()); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/ShellPromptTest.java Mon Dec 01 08:51:43 2014 -0500 @@ -0,0 +1,68 @@ +/* + * 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.client.cli.internal; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class ShellPromptTest { + + ShellPrompt shellPrompt; + + @Before + public void setup() { + shellPrompt = new ShellPrompt(); + } + + @Test + public void testConnectedPrompt() { + shellPrompt.storageConnected(); + + String expected = "Thermostat (C) > "; + assertEquals(expected, shellPrompt.getPrompt()); + } + + @Test + public void testDisconnectedPrompt() { + shellPrompt.storageDisconnected(); + + String expected = "Thermostat (D) > "; + assertEquals(expected, shellPrompt.getPrompt()); + } +}
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/CliTest.java Fri Nov 28 17:09:31 2014 -0500 +++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/CliTest.java Mon Dec 01 08:51:43 2014 -0500 @@ -132,10 +132,10 @@ public void testShell() throws Exception { Spawn shell = spawnThermostat("shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("help\n"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); assertMatchesShellHelpCommandList(shell.getCurrentStandardOutContents()); @@ -148,7 +148,7 @@ public void testShellPrintsVersionOnStartup() throws Exception { Spawn shell = spawnThermostat("shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); String stdOut = shell.getCurrentStandardOutContents(); assertTrue(stdOut.contains("Thermostat version ")); @@ -158,17 +158,17 @@ public void versionArgumentInShellIsNotAllowed() throws Exception { Spawn shell = spawnThermostat("shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("--version\n"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); String stdOut = shell.getCurrentStandardOutContents(); String stdErr = shell.getCurrentStandardErrContents(); assertMatchesShellHelpCommandList(shell.getCurrentStandardOutContents()); // use the Pattern.DOTALL flag (?s) so that line terminators match with - // ".*". stdOut contains the SHELL_PROMPT too. + // ".*". stdOut contains the SHELL_DISCONNECT_PROMPT too. assertTrue(stdOut.matches("(?s)^.*\nunknown command '--version'\n.*$")); assertEquals(stdErr, ""); @@ -226,9 +226,9 @@ // test '!' events Spawn shell = spawnThermostat("shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("what!?!\n"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("exit\n"); shell.expectClose();
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java Fri Nov 28 17:09:31 2014 -0500 +++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java Mon Dec 01 08:51:43 2014 -0500 @@ -78,7 +78,8 @@ public static final long TIMEOUT_IN_SECONDS = 30; - public static final String SHELL_PROMPT = "Thermostat >"; + public static final String SHELL_DISCONNECT_PROMPT = "Thermostat (D) >"; + public static final String SHELL_CONNECT_PROMPT = "Thermostat (C) >"; private static final String THERMOSTAT_HOME = "THERMOSTAT_HOME"; private static final String USER_THERMOSTAT_HOME = "USER_THERMOSTAT_HOME";
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/StorageConnectionTest.java Fri Nov 28 17:09:31 2014 -0500 +++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/StorageConnectionTest.java Mon Dec 01 08:51:43 2014 -0500 @@ -74,10 +74,10 @@ public void testConnect() throws ExpectJException, TimeoutException, IOException { Spawn shell = spawnThermostat(true, "shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("connect -d mongodb://127.0.0.1:27518\n"); handleAuthPrompt(shell, "mongodb://127.0.0.1:27518", "", ""); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_CONNECT_PROMPT); shell.send("exit\n"); shell.expectClose(); @@ -89,9 +89,9 @@ public void testDisconnectWithoutConnecting() throws ExpectJException, TimeoutException, IOException { Spawn shell = spawnThermostat("shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("disconnect\n"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("exit\n"); shell.expectClose(); @@ -104,10 +104,10 @@ public void testConnectAndDisconnectInShell() throws IOException, TimeoutException, ExpectJException { Spawn shell = spawnThermostat(true, "shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("connect -d mongodb://127.0.0.1:27518\n"); handleAuthPrompt(shell, "mongodb://127.0.0.1:27518", "", ""); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_CONNECT_PROMPT); shell.send("disconnect\n"); shell.send("exit\n"); shell.expectClose();
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java Fri Nov 28 17:09:31 2014 -0500 +++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java Mon Dec 01 08:51:43 2014 -0500 @@ -130,15 +130,15 @@ String storageURL = "mongodb://127.0.0.1:27518"; Spawn shell = spawnThermostat("shell"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_DISCONNECT_PROMPT); shell.send("list-vms -d " + storageURL + "\n"); handleAuthPrompt(shell, storageURL, "", ""); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_CONNECT_PROMPT); shell.send("dump-heap\n"); - shell.expect(SHELL_PROMPT); + shell.expect(SHELL_CONNECT_PROMPT); shell.send("exit\n"); shell.expectClose();