use of org.apache.felix.gogo.runtime.Tokenizer in project felix by apache.
the class Activator method startShell.
private Runnable startShell(BundleContext context, CommandProcessor processor) throws Exception {
Dictionary<String, Object> dict = new Hashtable<>();
dict.put(CommandProcessor.COMMAND_SCOPE, "gogo");
// register converters
regs.add(context.registerService(Converter.class.getName(), new Converters(context.getBundle(0).getBundleContext()), null));
// register commands
dict.put(CommandProcessor.COMMAND_FUNCTION, Builtin.functions);
regs.add(context.registerService(Builtin.class.getName(), new Builtin(), dict));
dict.put(CommandProcessor.COMMAND_FUNCTION, Procedural.functions);
regs.add(context.registerService(Procedural.class.getName(), new Procedural(), dict));
dict.put(CommandProcessor.COMMAND_FUNCTION, Posix.functions);
regs.add(context.registerService(Posix.class.getName(), new Posix(processor), dict));
Shell shell = new Shell(new ShellContext(), processor);
dict.put(CommandProcessor.COMMAND_FUNCTION, Shell.functions);
regs.add(context.registerService(Shell.class.getName(), shell, dict));
Terminal terminal = TerminalBuilder.builder().name("gogo").system(true).nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build();
CommandSession session = processor.createSession(terminal.input(), terminal.output(), terminal.output());
AtomicBoolean closing = new AtomicBoolean();
Thread thread = new Thread(() -> {
String errorMessage = "gogo: unable to create console";
try {
session.put(Shell.VAR_TERMINAL, terminal);
try {
List<String> args = new ArrayList<>();
args.add("--login");
String argstr = shell.getContext().getProperty("gosh.args");
if (argstr != null) {
Tokenizer tokenizer = new Tokenizer(argstr);
Token token;
while ((token = tokenizer.next()) != null) {
args.add(token.toString());
}
}
shell.gosh(session, args.toArray(new String[args.size()]));
} catch (Throwable e) {
Object loc = session.get(".location");
if (null == loc || !loc.toString().contains(":")) {
loc = "gogo";
}
errorMessage = loc.toString();
throw e;
}
} catch (Throwable e) {
if (!closing.get()) {
System.err.println(errorMessage + e.getClass().getSimpleName() + ": " + e.getMessage());
e.printStackTrace();
}
}
}, "Gogo shell");
// start shell on a separate thread...
thread.start();
return () -> {
closing.set(true);
shell.stop();
try {
terminal.close();
} catch (IOException e) {
// Ignore
}
try {
long t0 = System.currentTimeMillis();
while (thread.isAlive()) {
thread.interrupt();
thread.join(10);
if (System.currentTimeMillis() - t0 > 5000) {
System.err.println("!!! FAILED TO STOP EXECUTOR !!!");
break;
}
}
} catch (InterruptedException e) {
// Restore administration...
Thread.currentThread().interrupt();
}
};
}
Aggregations