This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
664 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/uk/gov/justice/framework/command/client/CommandIdProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package uk.gov.justice.framework.command.client; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
import static java.util.UUID.fromString; | ||
|
||
import uk.gov.justice.framework.command.client.io.ToConsolePrinter; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
|
||
public class CommandIdProvider { | ||
|
||
@Inject | ||
private ToConsolePrinter toConsolePrinter; | ||
|
||
public Optional<UUID> getCommandId(final CommandLine commandLine) { | ||
final String commandId = commandLine.getOptionValue("attach"); | ||
|
||
if (commandId == null) { | ||
toConsolePrinter.println("Please specify a command id to attach to"); | ||
} else { | ||
try { | ||
return of(fromString(commandId)); | ||
} catch (final IllegalArgumentException e) { | ||
toConsolePrinter.printf("Command id '%s' is not a UUID", commandId); | ||
} | ||
} | ||
|
||
return empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/uk/gov/justice/framework/command/client/jmx/RunContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package uk.gov.justice.framework.command.client.jmx; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
public class RunContext { | ||
|
||
private final UUID commandId; | ||
private final String commandName; | ||
private final ZonedDateTime startTime; | ||
|
||
public RunContext(final UUID commandId, final String commandName, final ZonedDateTime startTime) { | ||
this.commandId = commandId; | ||
this.commandName = commandName; | ||
this.startTime = startTime; | ||
} | ||
|
||
public UUID getCommandId() { | ||
return commandId; | ||
} | ||
|
||
public String getCommandName() { | ||
return commandName; | ||
} | ||
|
||
public ZonedDateTime getStartTime() { | ||
return startTime; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof RunContext)) return false; | ||
final RunContext that = (RunContext) o; | ||
return Objects.equals(commandId, that.commandId) && | ||
Objects.equals(commandName, that.commandName) && | ||
Objects.equals(startTime, that.startTime); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(commandId, commandName, startTime); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RunContext{" + | ||
"commandId=" + commandId + | ||
", commandName='" + commandName + '\'' + | ||
", startTime=" + startTime + | ||
'}'; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/uk/gov/justice/framework/command/client/jmx/SystemCommandAttachRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package uk.gov.justice.framework.command.client.jmx; | ||
|
||
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_IN_PROGRESS; | ||
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_RECEIVED; | ||
|
||
import uk.gov.justice.framework.command.client.io.ToConsolePrinter; | ||
import uk.gov.justice.services.jmx.api.CommandNotFoundException; | ||
import uk.gov.justice.services.jmx.api.domain.CommandState; | ||
import uk.gov.justice.services.jmx.api.domain.SystemCommandStatus; | ||
import uk.gov.justice.services.jmx.api.mbean.SystemCommanderMBean; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.UUID; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class SystemCommandAttachRunner { | ||
|
||
@Inject | ||
private CommandPoller commandPoller; | ||
|
||
@Inject | ||
private ToConsolePrinter toConsolePrinter; | ||
|
||
public void attach(final UUID commandId, final SystemCommanderMBean systemCommanderMBean) { | ||
try { | ||
final SystemCommandStatus commandStatus = systemCommanderMBean.getCommandStatus(commandId); | ||
final String commandName = commandStatus.getSystemCommandName(); | ||
final CommandState commandState = commandStatus.getCommandState(); | ||
|
||
if (commandState == COMMAND_IN_PROGRESS || commandState == COMMAND_RECEIVED) { | ||
final ZonedDateTime startTime = commandStatus.getStatusChangedAt(); | ||
final RunContext runContext = new RunContext( | ||
commandId, | ||
commandName, | ||
startTime | ||
); | ||
|
||
commandPoller.runUntilComplete(systemCommanderMBean, runContext); | ||
} else { | ||
toConsolePrinter.printf("Cannot attach to Command with id '%s'. Command is not running. Current command status: %s", commandId, commandState); | ||
} | ||
} catch (final CommandNotFoundException e) { | ||
toConsolePrinter.printf("No system command exists with id '%s", commandId); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/uk/gov/justice/framework/command/client/jmx/SystemCommandAttacher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package uk.gov.justice.framework.command.client.jmx; | ||
|
||
import uk.gov.justice.framework.command.client.io.ToConsolePrinter; | ||
import uk.gov.justice.services.jmx.api.SystemCommandInvocationFailedException; | ||
import uk.gov.justice.services.jmx.api.mbean.SystemCommanderMBean; | ||
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClient; | ||
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClientFactory; | ||
import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; | ||
|
||
import java.util.UUID; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
@ApplicationScoped | ||
public class SystemCommandAttacher { | ||
|
||
@Inject | ||
private SystemCommanderClientFactory systemCommanderClientFactory; | ||
|
||
@Inject | ||
private SystemCommandAttachRunner systemCommandAttachRunner; | ||
|
||
@Inject | ||
private ToConsolePrinter toConsolePrinter; | ||
|
||
public void attachToRunningCommand(final UUID commandId, final JmxParameters jmxParameters) { | ||
|
||
final String contextName = jmxParameters.getContextName(); | ||
toConsolePrinter.printf("Attaching to system command with id'%s'", commandId); | ||
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 systemCommanderMBean = systemCommanderClient.getRemote(contextName); | ||
systemCommandAttachRunner.attach(commandId, systemCommanderMBean); | ||
} catch (final SystemCommandInvocationFailedException e) { | ||
toConsolePrinter.printf("The command with id '%s' failed: %s", commandId, e.getMessage()); | ||
toConsolePrinter.println(e.getServerStackTrace()); | ||
throw e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.