All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.common.script.debug.DisconnectDebugCommand Maven / Gradle / Ivy

package org.bidib.wizard.common.script.debug;

import java.util.Scanner;

import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DisconnectDebugCommand extends AbstractScriptCommand {

    private static final Logger LOGGER = LoggerFactory.getLogger(DisconnectDebugCommand.class);

    public static final String KEY = "disconnectDebugInterface";

    private String connectionId;

    public static final String HELP =
        CODE_BLOCK_START + KEY + prepareHelpHtml(" --id=") + DIV_END + prepareHelpHtml(
            "\nDisconnect from debug interface. The connectionId is the connection identifier. The standard value is 'DebugMain'.\n\nExample:\n")
            + CODE_BLOCK_START + prepareHelpHtml("disconnectDebugInterface --id=DebugMain") + DIV_END;

    public DisconnectDebugCommand(ConsoleService consoleService) {
        super(consoleService, KEY, HELP);
    }

    @Override
    public void parse(String commandLine) {

        try (Scanner scanner = new Scanner(commandLine)) {
            if (!getKey().equals(scanner.next())) {
                LOGGER.warn("Invalid command is scanned, key does not match.");
                throw new IllegalArgumentException("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("Parsed command, connectionId: {}", connectionId);
    }

    public String getConnectionId() {
        return connectionId;
    }

    @Override
    protected void internalExecute(DebugInterfaceScripting scripting, ApplicationContext context) {
        LOGGER.info("Disconnect from debug interface with connectionId: {}", connectionId);

        scripting.disconnectDebug(connectionId);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy