/** * Allow sub classes of main to change the ConsoleImpl implementation used. * * @param sessionFactory the session factory. * @param in the input stream (console std in). * @param out the output stream (console std out). * @param err the error stream (console std err). * @param terminal the terminal. * @return the created session. * @throws Exception if something goes wrong during session creation. */ protected Session createSession(SessionFactory sessionFactory, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception { return sessionFactory.create(in, out, err, terminal, null, null); }
/** * Allow sub classes of main to change the ConsoleImpl implementation used. * * @param sessionFactory the session factory. * @param in the input stream (console std in). * @param out the output stream (console std out). * @param err the error stream (console std err). * @param terminal the terminal. * @return the created session. * @throws Exception if something goes wrong during session creation. */ protected Session createSession(SessionFactory sessionFactory, InputStream in, PrintStream out, PrintStream err, Terminal terminal) throws Exception { return sessionFactory.create(in, out, err, terminal, null, null); }
@Override public String run() throws Exception { final Session session = sessionFactory.create(System.in, printStream, errStream); LOG.info("Command: {}", command); FutureTask<String> commandFuture = new FutureTask<String>(new Callable<String>() { public String call() { try { session.execute(command); } catch (Exception e) { e.printStackTrace(System.err); } printStream.flush(); errStream.flush(); return byteArrayOutputStream.toString(); } }); try { executor.submit(commandFuture); String response = commandFuture.get(10, TimeUnit.SECONDS); LOG.info("Response: {}", response); return response; } catch (Exception e) { e.printStackTrace(System.err); return "SHELL COMMAND TIMED OUT: " + command; } } });
@Override public void execute(JobContext context) { try (Session s = sessionFactory.create(new ByteArrayInputStream(new byte[0]), session.getConsole(), session.getConsole(), session)) { script.execute(s, Collections.singletonList(context)); } catch (Exception e) { LOGGER.warn("Error executing script", e); } }
@Override public void execute(JobContext context) { try (Session s = sessionFactory.create(new ByteArrayInputStream(new byte[0]), session.getConsole(), session.getConsole(), session)) { script.execute(s, Collections.singletonList(context)); } catch (Exception e) { LOGGER.warn("Error executing script", e); } }
public void run() { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(byteArrayOutputStream); try (Session session = sessionFactory.create(new ByteArrayInputStream(new byte[0]), printStream, printStream, WatchAction.this.session)) { executing = Thread.currentThread(); session.execute(arguments.trim()); } catch (InterruptedException e) { abort(null); } catch (Exception e) { ShellUtil.logException(session, e); } finally { executing = null; } printStream.flush(); if (!abort) { if (!append) { System.out.print("\33[2J"); System.out.print("\33[1;1H"); } System.out.print(byteArrayOutputStream.toString()); System.out.flush(); } } catch (Exception e) { //Ingore } }
public void run() { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(byteArrayOutputStream); try (Session session = sessionFactory.create(new ByteArrayInputStream(new byte[0]), printStream, printStream, WatchAction.this.session)) { executing = Thread.currentThread(); session.execute(arguments.trim()); } catch (InterruptedException e) { abort(null); } catch (Exception e) { ShellUtil.logException(session, e); } finally { executing = null; } printStream.flush(); if (!abort) { if (!append) { System.out.print("\33[2J"); System.out.print("\33[1;1H"); } System.out.print(byteArrayOutputStream.toString()); System.out.flush(); } } catch (Exception e) { //Ingore } }
public SessionTerminal() throws IOException { try { this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT); terminal.write("\u001b\u005B20\u0068"); // set newline mode on in = new PipedOutputStream(); out = new PipedInputStream(); InputStream input = new PipedInputStream(in); OutputStream output = new PipedOutputStream(out); PrintStream pipedOut = new PrintStream(output, true); Session session = sessionFactory.create( input, pipedOut, pipedOut, new WebTerminal(TERM_WIDTH, TERM_HEIGHT, input, pipedOut), null, null); new Thread(session, "Karaf web console user " + getCurrentUserName()).start(); } catch (IOException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw new IOException(e); } new Thread(this).start(); }
public SessionTerminal() throws IOException { try { this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT); terminal.write("\u001b\u005B20\u0068"); // set newline mode on in = new PipedOutputStream(); out = new PipedInputStream(); InputStream input = new PipedInputStream(in); OutputStream output = new PipedOutputStream(out); PrintStream pipedOut = new PrintStream(output, true); Session session = sessionFactory.create( input, pipedOut, pipedOut, new WebTerminal(TERM_WIDTH, TERM_HEIGHT, input, pipedOut), null, null); new Thread(session, "Karaf web console user " + getCurrentUserName()).start(); } catch (IOException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw new IOException(e); } new Thread(this).start(); }
public String getHelp(Session session, String path) { if (path.indexOf('|') > 0) { if (path.startsWith("command|")) { path = path.substring("command|".length()); } else { return null; } } ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos, true); Session s = session.getFactory().create(bais, ps, ps, session); s.put(Session.SCOPE, session.get(Session.SCOPE)); s.put(Session.SUBSHELL, session.get(Session.SUBSHELL)); try { s.execute(path + " --help"); } catch (Throwable t) { return null; } finally { s.close(); } return baos.toString(); }
public String getHelp(Session session, String path) { if (path.indexOf('|') > 0) { if (path.startsWith("command|")) { path = path.substring("command|".length()); } else { return null; } } ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos, true); Session s = session.getFactory().create(bais, ps, ps, session); s.put(Session.SCOPE, session.get(Session.SCOPE)); s.put(Session.SUBSHELL, session.get(Session.SUBSHELL)); try { s.execute(path + " --help"); } catch (Throwable t) { return null; } finally { s.close(); } return baos.toString(); }
public void start(final Environment env) throws IOException { try { final Subject subject = ShellImpl.this.session != null ? ShellImpl.this.session .getAttribute(KarafJaasAuthenticator.SUBJECT_ATTRIBUTE_KEY) : null; String encoding = getEncoding(env); terminal = new SshTerminal(env, in, out); final PrintStream pout = new PrintStream(terminal.output(), true, encoding); final PrintStream perr = err instanceof PrintStream ? (PrintStream) err : out == err ? pout : new PrintStream(err, true, encoding); shell = sessionFactory.create(in, pout, perr, terminal, encoding, this::destroy); for (Map.Entry<String, String> e : env.getEnv().entrySet()) { shell.put(e.getKey(), e.getValue()); } JaasHelper.runAs(subject, () -> new Thread(shell, "Karaf ssh console user " + ShellUtil.getCurrentUserName()).start()); } catch (Exception e) { throw new IOException("Unable to start shell", e); } }
public void start(final Environment env) throws IOException { try { final Subject subject = ShellImpl.this.session != null ? ShellImpl.this.session .getAttribute(KarafJaasAuthenticator.SUBJECT_ATTRIBUTE_KEY) : null; String encoding = getEncoding(env); terminal = new SshTerminal(env, in, out, encoding); final PrintStream pout = new PrintStream(terminal.output(), true, encoding); final PrintStream perr = err instanceof PrintStream ? (PrintStream) err : out == err ? pout : new PrintStream(err, true, encoding); shell = sessionFactory.create(in, pout, perr, terminal, encoding, this::destroy); for (Map.Entry<String, String> e : env.getEnv().entrySet()) { shell.put(e.getKey(), e.getValue()); } JaasHelper.runAs(subject, () -> new Thread(shell, "Karaf ssh console user " + ShellUtil.getCurrentUserName()).start()); } catch (Exception e) { throw new IOException("Unable to start shell", e); } }
final Session newSession = session.getFactory().create( System.in, System.out, System.err, SuCommand.this.session.getTerminal(), null, null); Object oldIgnoreInterrupts = session.get(Session.IGNORE_INTERRUPTS);
final Session newSession = session.getFactory().create( System.in, System.out, System.err, SuCommand.this.session.getTerminal(), null, null); Object oldIgnoreInterrupts = session.get(Session.IGNORE_INTERRUPTS);
public void run() { int exitStatus = 0; try { final Session session = sessionFactory.create(in, new PrintStream(out), new PrintStream(err)); for (Map.Entry<String,String> e : env.getEnv().entrySet()) { session.put(e.getKey(), e.getValue());
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final PrintStream printStream = new PrintStream(byteArrayOutputStream); final Session commandSession = sessionFactory.create(System.in, printStream, printStream); commandSession.put("APPLICATION", System.getProperty("karaf.name", "root")); commandSession.put("USER", USER);
session = sessionFactory.create( terminal.input(), pout,
session = sessionFactory.create( terminal.input(), pout,
protected void doExecute() { if (command == null || timer == null) return; log().i(name,"execute",command); StreamToLogAdapter out = new StreamToLogAdapter(LEVEL.INFO, null); StreamToLogAdapter err = new StreamToLogAdapter(LEVEL.ERROR, null); try { SessionFactory commandProcessor=MOsgi.getService(SessionFactory.class); ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); Session commandSession=commandProcessor.create(in,out,err); commandSession.put("interactive.mode", false); commandSession.put("APPLICATION",System.getProperty("karaf.name","root")); commandSession.put("USER","karaf"); commandSession.execute(command); } catch (Throwable t) { log().w(name,t); } out.close(); err.close(); }