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();
}
}
}
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();
}
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();
}
}
}
Aggregations