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() + "'");
}
}