}
public void start(final Environment env) throws IOException {
int exitStatus = 0;
try {
final CommandSession session = commandProcessor.createSession(in, new PrintStream(out), new PrintStream(err));
session.put("SCOPE", "shell:osgi:*");
session.put("APPLICATION", System.getProperty("karaf.name", "root"));
for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
session.put(e.getKey(), e.getValue());
}
try {
Subject subject = this.session != null ? this.session.getAttribute(KarafJaasAuthenticator.SUBJECT_ATTRIBUTE_KEY) : null;
Object result;
if (subject != null) {
try {
String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
executeScript(scriptFileName, session);
result = JaasHelper.doAs(subject, new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
return session.execute(command);
}
});
} catch (PrivilegedActionException e) {
throw e.getException();
}
} else {
String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
executeScript(scriptFileName, session);
result = session.execute(command);
}
if (result != null)
{
session.getConsole().println(session.format(result, Converter.INSPECT));
}
} catch (Throwable t) {
exitStatus = 1;
try {
boolean isCommandNotFound = "org.apache.felix.gogo.runtime.CommandNotFoundException".equals(t.getClass().getName());
if (isCommandNotFound) {
LOGGER.debug("Unknown command entered", t);
} else {
LOGGER.info("Exception caught while executing command", t);
}
session.put(Console.LAST_EXCEPTION, t);
if (t instanceof CommandException) {
session.getConsole().println(((CommandException) t).getNiceHelp());
} else if (isCommandNotFound) {
String str = Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Command not found: ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(t.getClass().getMethod("getCommand").invoke(t))
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.fg(Ansi.Color.DEFAULT).toString();
session.getConsole().println(str);
}
if (getBoolean(session, Console.PRINT_STACK_TRACES)) {
session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString());
t.printStackTrace(session.getConsole());
session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString());
}
else if (!(t instanceof CommandException) && !isCommandNotFound) {
session.getConsole().print(Ansi.ansi().fg(Ansi.Color.RED).toString());
session.getConsole().println("Error executing command: "
+ (t.getMessage() != null ? t.getMessage() : t.getClass().getName()));
session.getConsole().print(Ansi.ansi().fg(Ansi.Color.DEFAULT).toString());
}
session.getConsole().flush();
} catch (Exception ignore) {
// ignore
}
}
} catch (Exception e) {