Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Merge 7bc431d into 1d3e3d4
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Jul 18, 2019
2 parents 1d3e3d4 + 7bc431d commit b96874e
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Added a mandatory command line switch '--context-name' to allow to connect to a specific context


## [1.0.1] 2019-07-17
### Fixed
- Removed all dependencies on server side code to allow fat jar to be run
Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
<cpp.repo.name>framework-jmx-command-client</cpp.repo.name>

<framework-api.version>4.0.1</framework-api.version>
<framework.version>6.0.0-RC7</framework.version>
<event-store.version>2.0.0-RC7</event-store.version>
<framework.version>6.0.0-RC9</framework.version>
<maven-common-bom.version>2.4.0</maven-common-bom.version>
</properties>

Expand Down
6 changes: 3 additions & 3 deletions runSystemCommand.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash

FRAMEWORK_JMX_COMMAND_CLIENT_VERSION=1.1.0-SNAPSHOT
FRAMEWORK_JMX_COMMAND_CLIENT_VERSION=1.2.0-SNAPSHOT

#fail script on error
set -e
echo "Running Framework System Command Client $1 $2 $3 $4 $5"
echo "Running Framework System Command Client $1 $2 $3 $4 $5 $6"
echo
java -jar target/framework-jmx-command-client-${FRAMEWORK_JMX_COMMAND_CLIENT_VERSION}.jar $1 $2 $3 $4 $5
java -jar target/framework-jmx-command-client-${FRAMEWORK_JMX_COMMAND_CLIENT_VERSION}.jar $1 $2 $3 $4 $5 $6
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ public class JmxParametersFactory {

public JmxParameters createFrom(final CommandLine commandLine) {

if (!commandLine.hasOption("context-name")) {
throw new CommandLineException("No context name provided. Please run using --context-name <name> or -cn <name> .");
}

final String contextName = commandLine.getOptionValue("context-name");
final String host = commandLine.getOptionValue("host", DEFAULT_HOST);
final int port = getPort(commandLine);

final JmxParametersBuilder jmxParameters = jmxParameters()
.withHost(host)
.withPort(port);
.withContextName(contextName)
.withHost(host)
.withPort(port);

if (commandLine.hasOption("username")) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class OptionsFactory {
public Options createOptions() {
final Options options = new Options();

options.addOption("cn", "context-name", true, "The name of the context on which to run the command. Required");
options.addOption("c", "command", true, "Framework command to execute. Run with --list for a list of all commands");
options.addOption("h", "host", true, "Hostname or IP address of the Wildfly server. Defaults to localhost");
options.addOption("p", "port", true, "Wildfly management port. Defaults to 9990");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public class ListCommandsInvoker {

public Optional<List<SystemCommand>> listSystemCommands(final JmxParameters jmxParameters) {

toConsolePrinter.printf("Connecting to Wildfly instance at '%s' on port %d", jmxParameters.getHost(), jmxParameters.getPort());
final String contextName = jmxParameters.getContextName();

toConsolePrinter.printf("Connecting to %s context at '%s' on port %d", contextName, jmxParameters.getHost(), jmxParameters.getPort());
jmxParameters.getCredentials().ifPresent(credentials -> toConsolePrinter.printf("Connecting with credentials for user '%s'", credentials.getUsername()));

try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(jmxParameters)) {
final SystemCommanderMBean remote = systemCommanderClient.getRemote();
final SystemCommanderMBean remote = systemCommanderClient.getRemote(contextName);

toConsolePrinter.println("Connected to remote server");
toConsolePrinter.printf("Connected to %s context", contextName);

return of(remote.listCommands());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ public class SystemCommandInvoker {

public void runSystemCommand(final SystemCommand systemCommand, final JmxParameters jmxParameters) {

toConsolePrinter.printf("Running system command '%s'", systemCommand.getName());
final String commandName = systemCommand.getName();
final String contextName = jmxParameters.getContextName();

toConsolePrinter.printf("Running system command '%s'", commandName);
toConsolePrinter.printf("Connecting to %s context at '%s' on port %d", contextName, jmxParameters.getHost(), jmxParameters.getPort());

toConsolePrinter.printf("Connecting to Wildfly instance at '%s' on port %d", jmxParameters.getHost(), jmxParameters.getPort());
jmxParameters.getCredentials().ifPresent(credentials -> toConsolePrinter.printf("Connecting with credentials for user '%s'", credentials.getUsername()));

try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(jmxParameters)) {
final SystemCommanderMBean systemCommanderMBean = systemCommanderClient.getRemote();
final SystemCommanderMBean systemCommanderMBean = systemCommanderClient.getRemote(contextName);

toConsolePrinter.println("Connected to remote server");
toConsolePrinter.printf("Connected to %s context", contextName);

systemCommanderMBean.call(systemCommand);

toConsolePrinter.printf("System command '%s' successfully sent", systemCommand.getName());
toConsolePrinter.printf("System command '%s' successfully sent to %s", commandName, contextName);

} catch (final JmxAuthenticationException e) {
toConsolePrinter.println("Authentication failed. Please ensure your username and password are correct");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ public class JmxParametersFactoryTest {
@Test
public void shouldCreateJmxParametersFromTheHostAndPort() throws Exception {

final String contextName = "my-context";
final String host = "host";
final String port = "9990";

final CommandLine commandLine = mock(CommandLine.class);

when(commandLine.hasOption("context-name")).thenReturn(true);
when(commandLine.getOptionValue("context-name")).thenReturn(contextName);
when(commandLine.getOptionValue("host", "localhost")).thenReturn(host);
when(commandLine.getOptionValue("port", "9990")).thenReturn(port);
when(commandLine.hasOption("username")).thenReturn(false);

final JmxParameters jmxParameters = jmxParametersFactory.createFrom(commandLine);

assertThat(jmxParameters.getContextName(), is(contextName));
assertThat(jmxParameters.getHost(), is(host));
assertThat(jmxParameters.getPort(), is(9990));
assertThat(jmxParameters.getCredentials().isPresent(), is(false));
Expand All @@ -45,13 +49,16 @@ public void shouldCreateJmxParametersFromTheHostAndPort() throws Exception {
@Test
public void shouldAlsoAddCredentialsIfTheUsernameExists() throws Exception {

final String contextName = "my-context";
final String host = "host";
final String port = "9990";
final String username = "Fred";
final String password = "Password123";

final CommandLine commandLine = mock(CommandLine.class);

when(commandLine.hasOption("context-name")).thenReturn(true);
when(commandLine.getOptionValue("context-name")).thenReturn(contextName);
when(commandLine.getOptionValue("host", "localhost")).thenReturn(host);
when(commandLine.getOptionValue("port", "9990")).thenReturn(port);
when(commandLine.hasOption("username")).thenReturn(true);
Expand All @@ -73,14 +80,32 @@ public void shouldAlsoAddCredentialsIfTheUsernameExists() throws Exception {
}
}

@Test
public void shouldThrowExceptionIfTheContextNameIsMissing() throws Exception {

final CommandLine commandLine = mock(CommandLine.class);

when(commandLine.hasOption("context-name")).thenReturn(false);

try {
jmxParametersFactory.createFrom(commandLine);
fail();
} catch (final CommandLineException expected) {
assertThat(expected.getMessage(), is("No context name provided. Please run using --context-name <name> or -cn <name> ."));
}
}

@Test
public void shouldThrowExceptionIfThePortIsNotANumber() throws Exception {

final String contextName = "my-context";
final String host = "host";
final String port = "not-a-number";

final CommandLine commandLine = mock(CommandLine.class);

when(commandLine.hasOption("context-name")).thenReturn(true);
when(commandLine.getOptionValue("context-name")).thenReturn(contextName);
when(commandLine.getOptionValue("host", "localhost")).thenReturn(host);
when(commandLine.getOptionValue("port", "9990")).thenReturn(port);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public void shouldCreateNewOptionsObjectConfiguredWithTheCorrectCommandLineParam

final Collection<Option> allOptions = options.getOptions();

assertThat(allOptions.size(), is(7));
assertThat(allOptions.size(), is(8));

assertThat(allOptions, hasItem(new Option("help", false, "Show help.")));
assertThat(allOptions, hasItem(new Option("cn", "context-name", true, "The name of the context on which to run the command. Required")));
assertThat(allOptions, hasItem(new Option("c", "command", true, "Framework command to execute. Run with --list for a list of all commands")));
assertThat(allOptions, hasItem(new Option("h", "host", true, "Hostname or IP address of the Wildfly server. Defaults to localhost")));
assertThat(allOptions, hasItem(new Option("p", "port", true, "Wildfly management port. Defaults to 9990")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ListCommandsInvokerTest {
@Test
public void shouldMakeAJmxCallToRetrieveTheListOfCommands() throws Exception {

final String contextName = "my-context";
final String host = "localhost";
final int port = 92834;

Expand All @@ -52,11 +53,12 @@ public void shouldMakeAJmxCallToRetrieveTheListOfCommands() throws Exception {
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);
final SystemCommanderMBean commanderMBean = mock(SystemCommanderMBean.class);

when(jmxParameters.getContextName()).thenReturn(contextName);
when(jmxParameters.getHost()).thenReturn(host);
when(jmxParameters.getPort()).thenReturn(port);
when(jmxParameters.getCredentials()).thenReturn(empty());
when(systemCommanderClientFactory.create(jmxParameters)).thenReturn(systemCommanderClient);
when(systemCommanderClient.getRemote()).thenReturn(commanderMBean);
when(systemCommanderClient.getRemote(contextName)).thenReturn(commanderMBean);
when(commanderMBean.listCommands()).thenReturn(systemCommands);

assertThat(listCommandsInvoker.listSystemCommands(jmxParameters), is(of(systemCommands)));
Expand All @@ -66,15 +68,16 @@ public void shouldMakeAJmxCallToRetrieveTheListOfCommands() throws Exception {
systemCommanderClientFactory,
systemCommanderClient);

inOrder.verify(toConsolePrinter).printf("Connecting to Wildfly instance at '%s' on port %d", host, port);
inOrder.verify(toConsolePrinter).printf("Connecting to %s context at '%s' on port %d", contextName, host, port);
inOrder.verify(systemCommanderClientFactory).create(jmxParameters);
inOrder.verify(systemCommanderClient).getRemote();
inOrder.verify(toConsolePrinter).println("Connected to remote server");
inOrder.verify(systemCommanderClient).getRemote(contextName);
inOrder.verify(toConsolePrinter).printf("Connected to %s context", contextName);
}

@Test
public void shoulLogIfUsingCredentials() throws Exception {

final String contextName = "my-context";
final String host = "localhost";
final int port = 92834;
final String username = "Fred";
Expand All @@ -86,12 +89,13 @@ public void shoulLogIfUsingCredentials() throws Exception {
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);
final SystemCommanderMBean commanderMBean = mock(SystemCommanderMBean.class);

when(jmxParameters.getContextName()).thenReturn(contextName);
when(jmxParameters.getHost()).thenReturn(host);
when(jmxParameters.getPort()).thenReturn(port);
when(jmxParameters.getCredentials()).thenReturn(of(credentials));
when(credentials.getUsername()).thenReturn(username);
when(systemCommanderClientFactory.create(jmxParameters)).thenReturn(systemCommanderClient);
when(systemCommanderClient.getRemote()).thenReturn(commanderMBean);
when(systemCommanderClient.getRemote(contextName)).thenReturn(commanderMBean);
when(commanderMBean.listCommands()).thenReturn(systemCommands);

assertThat(listCommandsInvoker.listSystemCommands(jmxParameters), is(of(systemCommands)));
Expand All @@ -101,16 +105,17 @@ public void shoulLogIfUsingCredentials() throws Exception {
systemCommanderClientFactory,
systemCommanderClient);

inOrder.verify(toConsolePrinter).printf("Connecting to Wildfly instance at '%s' on port %d", host, port);
inOrder.verify(toConsolePrinter).printf("Connecting to %s context at '%s' on port %d", contextName, host, port);
inOrder.verify(toConsolePrinter).printf("Connecting with credentials for user '%s'", username);
inOrder.verify(systemCommanderClientFactory).create(jmxParameters);
inOrder.verify(systemCommanderClient).getRemote();
inOrder.verify(toConsolePrinter).println("Connected to remote server");
inOrder.verify(systemCommanderClient).getRemote(contextName);
inOrder.verify(toConsolePrinter).printf("Connected to %s context", contextName);
}

@Test
public void shoulLogAndReturnEmptyIfAuthenticationFails() throws Exception {

final String contextName = "my-context";
final String host = "localhost";
final int port = 92834;
final String username = "Fred";
Expand All @@ -121,6 +126,7 @@ public void shoulLogAndReturnEmptyIfAuthenticationFails() throws Exception {
final JmxParameters jmxParameters = mock(JmxParameters.class);
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);

when(jmxParameters.getContextName()).thenReturn(contextName);
when(jmxParameters.getHost()).thenReturn(host);
when(jmxParameters.getPort()).thenReturn(port);
when(jmxParameters.getCredentials()).thenReturn(of(credentials));
Expand All @@ -134,7 +140,7 @@ public void shoulLogAndReturnEmptyIfAuthenticationFails() throws Exception {
systemCommanderClientFactory,
systemCommanderClient);

inOrder.verify(toConsolePrinter).printf("Connecting to Wildfly instance at '%s' on port %d", host, port);
inOrder.verify(toConsolePrinter).printf("Connecting to %s context at '%s' on port %d", contextName, host, port);
inOrder.verify(toConsolePrinter).printf("Connecting with credentials for user '%s'", username);
inOrder.verify(toConsolePrinter).println("Authentication failed. Please ensure your username and password are correct");
}
Expand Down
Loading

0 comments on commit b96874e

Please sign in to comment.