Search in sources :

Example 1 with Context

use of org.apache.felix.gogo.jline.Shell.Context in project felix by apache.

the class Posix method runShell.

private void runShell(CommandSession session, Terminal terminal) {
    InputStream in = terminal.input();
    OutputStream out = terminal.output();
    CommandSession newSession = processor.createSession(in, out, out);
    newSession.put(Shell.VAR_TERMINAL, terminal);
    newSession.put(".tmux", session.get(".tmux"));
    Context context = new Context() {

        public String getProperty(String name) {
            return System.getProperty(name);
        }

        public void exit() throws Exception {
            terminal.close();
        }
    };
    try {
        new Shell(context, processor).gosh(newSession, new String[] { "--login" });
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            terminal.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Context(org.apache.felix.gogo.jline.Shell.Context) CommandSession(org.apache.felix.service.command.CommandSession) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) AttributedString(org.jline.utils.AttributedString) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with Context

use of org.apache.felix.gogo.jline.Shell.Context in project felix by apache.

the class Telnet method start.

private void start(CommandSession session) throws IOException {
    ConnectionManager connectionManager = new ConnectionManager(1000, 5 * 60 * 1000, 5 * 60 * 1000, 60 * 1000, null, null, false) {

        @Override
        protected Connection createConnection(ThreadGroup threadGroup, ConnectionData newCD) {
            return new Connection(threadGroup, newCD) {

                TelnetIO telnetIO;

                @Override
                protected void doRun() throws Exception {
                    telnetIO = new TelnetIO();
                    telnetIO.setConnection(this);
                    telnetIO.initIO();
                    InputStream in = new InputStream() {

                        @Override
                        public int read() throws IOException {
                            return telnetIO.read();
                        }

                        @Override
                        public int read(byte[] b, int off, int len) throws IOException {
                            int r = read();
                            if (r >= 0) {
                                b[off] = (byte) r;
                                return 1;
                            } else {
                                return -1;
                            }
                        }
                    };
                    PrintStream out = new PrintStream(new OutputStream() {

                        @Override
                        public void write(int b) throws IOException {
                            telnetIO.write(b);
                        }

                        @Override
                        public void flush() throws IOException {
                            telnetIO.flush();
                        }
                    });
                    Terminal terminal = TerminalBuilder.builder().type(getConnectionData().getNegotiatedTerminalType().toLowerCase()).streams(in, out).system(false).name("telnet").build();
                    terminal.setSize(new Size(getConnectionData().getTerminalColumns(), getConnectionData().getTerminalRows()));
                    terminal.setAttributes(Shell.getTerminal(session).getAttributes());
                    addConnectionListener(new ConnectionListener() {

                        @Override
                        public void connectionIdle(ConnectionEvent ce) {
                        }

                        @Override
                        public void connectionTimedOut(ConnectionEvent ce) {
                        }

                        @Override
                        public void connectionLogoutRequest(ConnectionEvent ce) {
                        }

                        @Override
                        public void connectionSentBreak(ConnectionEvent ce) {
                        }

                        @Override
                        public void connectionTerminalGeometryChanged(ConnectionEvent ce) {
                            terminal.setSize(new Size(getConnectionData().getTerminalColumns(), getConnectionData().getTerminalRows()));
                            terminal.raise(Signal.WINCH);
                        }
                    });
                    PrintStream pout = new PrintStream(terminal.output());
                    CommandSession session = processor.createSession(terminal.input(), pout, pout);
                    session.put(Shell.VAR_TERMINAL, terminal);
                    Context context = new Context() {

                        @Override
                        public String getProperty(String name) {
                            return System.getProperty(name);
                        }

                        @Override
                        public void exit() throws Exception {
                            close();
                        }
                    };
                    new Shell(context, processor).gosh(session, new String[] { "--login" });
                }

                @Override
                protected void doClose() throws Exception {
                    telnetIO.closeOutput();
                    telnetIO.closeInput();
                }
            };
        }
    };
    portListener = new PortListener("gogo", port, 10);
    portListener.setConnectionManager(connectionManager);
    portListener.start();
}
Also used : Context(org.apache.felix.gogo.jline.Shell.Context) PrintStream(java.io.PrintStream) InputStream(java.io.InputStream) Size(org.jline.terminal.Size) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal) CommandSession(org.apache.felix.service.command.CommandSession) Shell(org.apache.felix.gogo.jline.Shell)

Example 3 with Context

use of org.apache.felix.gogo.jline.Shell.Context in project felix by apache.

the class Main method main.

public static void main(String[] args) throws IOException {
    try (Terminal terminal = TerminalBuilder.builder().name("gogo").system(true).nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build()) {
        ThreadIOImpl tio = new ThreadIOImpl();
        tio.start();
        try {
            CommandProcessorImpl processor = new CommandProcessorImpl(tio);
            Context context = new MyContext();
            Shell shell = new Shell(context, processor, tio, null);
            processor.addCommand("gogo", processor, "addCommand");
            processor.addCommand("gogo", processor, "removeCommand");
            processor.addCommand("gogo", processor, "eval");
            processor.addConverter(new BaseConverters());
            register(processor, new Builtin(), Builtin.functions);
            register(processor, new Procedural(), Procedural.functions);
            register(processor, new Posix(processor), Posix.functions);
            register(processor, shell, Shell.functions);
            InputStream in = new FilterInputStream(terminal.input()) {

                @Override
                public void close() throws IOException {
                }
            };
            OutputStream out = new FilterOutputStream(terminal.output()) {

                @Override
                public void close() throws IOException {
                }
            };
            CommandSession session = processor.createSession(in, out, out);
            session.put(Shell.VAR_CONTEXT, context);
            session.put(Shell.VAR_TERMINAL, terminal);
            try {
                String[] argv = new String[args.length + 1];
                argv[0] = "--login";
                System.arraycopy(args, 0, argv, 1, args.length);
                shell.gosh(session, argv);
            } catch (Exception e) {
                Object loc = session.get(".location");
                if (null == loc || !loc.toString().contains(":")) {
                    loc = "gogo";
                }
                System.err.println(loc + ": " + e.getClass().getSimpleName() + ": " + e.getMessage());
                e.printStackTrace();
            } finally {
                session.close();
            }
        } finally {
            tio.stop();
        }
    }
}
Also used : Context(org.apache.felix.gogo.jline.Shell.Context) FilterInputStream(java.io.FilterInputStream) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) CommandProcessorImpl(org.apache.felix.gogo.runtime.CommandProcessorImpl) OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) Terminal(org.jline.terminal.Terminal) IOException(java.io.IOException) CommandSession(org.apache.felix.service.command.CommandSession) FilterOutputStream(java.io.FilterOutputStream)

Aggregations

IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Context (org.apache.felix.gogo.jline.Shell.Context)3 CommandSession (org.apache.felix.service.command.CommandSession)3 FilterInputStream (java.io.FilterInputStream)2 Terminal (org.jline.terminal.Terminal)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FilterOutputStream (java.io.FilterOutputStream)1 PrintStream (java.io.PrintStream)1 Shell (org.apache.felix.gogo.jline.Shell)1 CommandProcessorImpl (org.apache.felix.gogo.runtime.CommandProcessorImpl)1 ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)1 Size (org.jline.terminal.Size)1 AttributedString (org.jline.utils.AttributedString)1