Search in sources :

Example 1 with CliEventListener

use of org.jboss.as.cli.CliEventListener in project quickstarts by jboss-switchyard.

the class JBossCliUtil method executeCliScript.

/**
     * Executes CLI script file.
     * @param path file path
     * @throws Exception exception
     */
public static void executeCliScript(String path) throws Exception {
    File file = new File(path);
    if (!file.exists() || !file.isFile()) {
        throw new IllegalArgumentException(path + " is not a valid CLI script file");
    }
    final CommandContext cmdCtx = CommandContextFactory.getInstance().newCommandContext(HOST, PORT, USER, PASSWORD != null ? PASSWORD.toCharArray() : null, NO_LOCAL_AUTH, INIT_CONSOLE, CONNECTION_TIMEOUT);
    if (_logger.isDebugEnabled()) {
        cmdCtx.addEventListener(new CliEventListener() {

            @Override
            public void cliEvent(CliEvent event, CommandContext ctx) {
                _logger.debug("CLI Event: " + event.toString());
            }
        });
    }
    cmdCtx.connectController();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        _logger.info("Executing CLI script file: " + path);
        String line = reader.readLine();
        while (cmdCtx.getExitCode() == 0 && !cmdCtx.isTerminated() && line != null) {
            if (_logger.isDebugEnabled()) {
                _logger.debug(">>> " + line.trim());
            }
            cmdCtx.handleSafe(line.trim());
            line = reader.readLine();
        }
    } finally {
        StreamUtils.safeClose(reader);
        cmdCtx.terminateSession();
        _logger.info("CLI session is terminated with exit code '" + cmdCtx.getExitCode() + "'");
    }
}
Also used : CommandContext(org.jboss.as.cli.CommandContext) CliEventListener(org.jboss.as.cli.CliEventListener) BufferedReader(java.io.BufferedReader) CliEvent(org.jboss.as.cli.CliEvent) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 CliEvent (org.jboss.as.cli.CliEvent)1 CliEventListener (org.jboss.as.cli.CliEventListener)1 CommandContext (org.jboss.as.cli.CommandContext)1