Search in sources :

Example 1 with Tokenizer

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();
        }
    };
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Token(org.apache.felix.gogo.runtime.Token) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandSession(org.apache.felix.service.command.CommandSession) Tokenizer(org.apache.felix.gogo.runtime.Tokenizer)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Token (org.apache.felix.gogo.runtime.Token)1 Tokenizer (org.apache.felix.gogo.runtime.Tokenizer)1 CommandSession (org.apache.felix.service.command.CommandSession)1 Terminal (org.jline.terminal.Terminal)1