org.bidib.wizard.common.script.common.DisconnectCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.common;
import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.script.ScriptOptionAwareCommand;
import org.bidib.wizard.api.script.Scripting;
import org.bidib.wizard.api.service.console.ConsoleColor;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.ScriptExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DisconnectCommand extends AbstractScriptCommand implements ScriptOptionAwareCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(DisconnectCommand.class);
public static final String KEY = "disconnect";
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(" --id=") + DIV_END + prepareHelpHtml(
"\nDisconnect from BiDiB system. The connectionId is the connection identifier. The standard value is 'main'.\n\nExample:\n")
+ CODE_BLOCK_START + prepareHelpHtml("disconnect --id=main") + DIV_END;
private String connectionId;
public DisconnectCommand(ConsoleService consoleService) {
super(consoleService, KEY, HELP);
}
protected String getConnectionId() {
return connectionId;
}
@Override
public String getOptionKey() {
return Scripting.OPTION_DISCONNECT_ON_ERROR;
}
@Override
public void parse(String commandLine) {
setLine(commandLine.trim());
try (Scanner scanner = new Scanner(commandLine)) {
if (!getKey().equals(scanner.next())) {
LOGGER.info("Invalid command is scanned, key does not match.");
}
line = commandLine.trim();
try {
connectionId = scanner.next("--id=(.+)").substring(5);
}
catch (Exception ex) {
LOGGER.warn("No connectionId found.", ex);
throw new RuntimeException("disconnect: No connectionId found.");
}
}
LOGGER.info("Current connectionId: {}", connectionId);
}
@Override
protected void internalExecute(CommonScripting scripting, ApplicationContext context) {
if (StringUtils.isBlank(connectionId)) {
throw new ScriptExecutionException("No connectionId provided.", this);
}
try {
scripting.disconnect(connectionId);
getConsoleService().addConsoleLine(ConsoleColor.gray, "Disconnect passed.");
}
catch (Exception ex) {
LOGGER.warn("Connect failed.", ex);
throw new ScriptExecutionException("Disconnect from BiDiB system failed: " + ex.getMessage(), this);
}
}
}