run(sessionFactory, sb.toString(), in, out, err, cl);
}
private void run(final SessionFactory sessionFactory, String command, final InputStream in, final PrintStream out, final PrintStream err, ClassLoader cl) throws Exception {
final TerminalFactory terminalFactory = new TerminalFactory();
try {
final Terminal terminal = new JLineTerminal(terminalFactory.getTerminal());
Session session = createSession(sessionFactory, command.length() > 0 ? null : in, out, err, terminal);
session.put("USER", user);
session.put("APPLICATION", application);
discoverCommands(session, cl, getDiscoveryResource());
if (command.length() > 0) {
// Shell is directly executing a sub/command, we don't setup a console
// in this case, this avoids us reading from stdin un-necessarily.
session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
session.put(Session.PRINT_STACK_TRACES, "execution");
try {
session.execute(command);
} catch (Throwable t) {
ShellUtil.logException(session, t);
}
} else {
// We are going into full blown interactive shell mode.
session.run();
}
} finally {
terminalFactory.destroy();
}
}