Search in sources :

Example 1 with Shell

use of org.apache.felix.gogo.jline.Shell 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)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Shell (org.apache.felix.gogo.jline.Shell)1 Context (org.apache.felix.gogo.jline.Shell.Context)1 CommandSession (org.apache.felix.service.command.CommandSession)1 Size (org.jline.terminal.Size)1 Terminal (org.jline.terminal.Terminal)1