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(); }
/** * Print a message in the console. * @param session the shell's session * @param msg the message to print out with a newline */ public static void printMessage(Session session, String msg) { PrintStream writer = session.getConsole(); writer.println(msg); } }
private String readLine(String prompt, Character ch) throws IOException { Object oldIgnoreInterrupts = session.get(Session.IGNORE_INTERRUPTS); session.put(Session.IGNORE_INTERRUPTS, Boolean.TRUE); try { String line = session.readLine(prompt, ch); if (line == null) { throw new IOException("interrupted"); } return line; } finally { session.put(Session.IGNORE_INTERRUPTS, oldIgnoreInterrupts); } }
((NameCallback) callback).setName(user); } else if (callback instanceof PasswordCallback) { String password = SuCommand.this.session.readLine("Password: ", '*'); ((PasswordCallback) callback).setPassword(password.toCharArray()); } else { 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); try { session.put(Session.IGNORE_INTERRUPTS, Boolean.TRUE); String name = "Karaf local console user " + ShellUtil.getCurrentUserName(); Thread thread = new Thread(newSession, name); thread.join(); } finally { session.put(Session.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
protected String getCurrentSubShell(Session session) { String s = (String) session.get(Session.SUBSHELL); if (s == null) { s = ""; } return s; }
@Override public Object execute() throws Exception { BufferedReader reader = null; Object arg0 = session.get("0"); try { try { Path file = session.currentDir().resolve(script); log.info("Printing file: " + file); reader = new BufferedReader(Files.newBufferedReader(file)); session.put( Integer.toString(i+1), args.get(i) ); session.execute(w.toString()); } finally { for (int i = 0; args != null && i < args.size(); i++) { session.put( Integer.toString(i+1), null ); session.put("0", arg0); if (reader != null) { try {
case "console": { final MProperties pe = new MProperties(parameters); MThread a = (MThread) session.get("__log_tail"); if (a != null) { session.put("__log_tail", null); } else { PrintStream sConsole = session.getConsole(); if (sConsole == null || session.getTerminal() == null) return null; @SuppressWarnings("resource") final Console os = (Console) (pe.getString("console","ansi").equals("ansi") ? appender.start(); session.put("__log_tail", appender);
/** * <p> * Get the OSGI-INF/bundle.info entry from the bundle and display it. * </p> * * @param bundle the bundle. */ @Override protected void executeOnBundle(Bundle bundle) throws Exception { session.execute("*:help 'bundle|" + bundle.getBundleId() + "'"); }
private void run(final SessionFactory sessionFactory, String command, final InputStream in, final PrintStream out, final PrintStream err, ClassLoader cl) throws Exception { try (org.jline.terminal.Terminal jlineTerminal = TerminalBuilder.terminal()) { final Terminal terminal = new JLineTerminal(jlineTerminal); try (Session session = createSession(sessionFactory, command.length() > 0 ? null : in, out, err, terminal)) { session.put("USER", user); session.put("APPLICATION", application); discoverCommands(session, cl, getDiscoveryResource()); if (command.length() > 0) { // Shell is directly executing a sub/command, we don't setup a console // in this case, this avoids us reading from stdin un-necessarily. session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode())); session.put(Session.PRINT_STACK_TRACES, "execution"); try { session.execute(command); } catch (Throwable t) { ShellUtil.logException(session, t); } } else { // We are going into full blown interactive shell mode. session.run(); } } } }
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(); }
@Override public String[] interactive(ClientSession s, String name, String instruction, String lang, String[] prompt, boolean[] echo) { String[] answers = new String[prompt.length]; try { for (int i = 0; i < prompt.length; i++) { answers[i] = session.readLine(prompt[i] + " ", echo[i] ? null : '*'); } } catch (IOException e) { } return answers; } @Override
@Override public Object execute() throws Exception { Subject subject = new Subject(); LoginContext loginContext = new LoginContext(realm, subject, callbacks -> { for (Callback callback : callbacks) { if (callback instanceof NameCallback) { ((NameCallback) callback).setName(user); } else if (callback instanceof PasswordCallback) { String password = SudoCommand.this.session.readLine("Password: ", '*'); ((PasswordCallback) callback).setPassword(password.toCharArray()); } else { throw new UnsupportedCallbackException(callback); } } }); loginContext.login(); final StringBuilder sb = new StringBuilder(); for (String s : command) { if (sb.length() > 0) { sb.append(" "); } sb.append(s); } JaasHelper.doAs(subject, (PrivilegedExceptionAction<Object>) () -> session.execute(sb)); loginContext.logout(); return null; }
public static void logException(Session session, Throwable t) { try { session.put(Session.LAST_EXCEPTION, t); if ("always".equals(pst)) { String str = applyStyle(getStackTrace(t), cm, "st"); session.getConsole().print(str); } else if ("CommandNotFoundException".equals(name)) { String str = applyStyle("Command not found: ", cm, "em") + applyStyle((String) t.getClass().getMethod("getCommand").invoke(t), cm, "ee"); session.getConsole().println(str); } else if ("CommandException".equals(name)) { String str; str = applyStyle(t.getMessage(), cm, "em"); session.getConsole().println(str); } else if ("execution".equals(pst)) { String str = applyStyle(getStackTrace(t), cm, "st"); session.getConsole().print(str); } else { String str = applyStyle("Error executing command: ", cm, "em") + applyStyle(t.getMessage() != null ? t.getMessage() : t.getClass().getName(), cm, "ee"); session.getConsole().println(str); session.getConsole().flush(); } catch (Exception ignore) {
System.err.println(command); Object result = session.execute(command); if (result != null) { session.getConsole().println(result.toString());
private int getTermHeight() { Terminal term = session.getTerminal(); return term != null ? term.getHeight() : 25; }
((NameCallback) callback).setName(user); } else if (callback instanceof PasswordCallback) { String password = SuCommand.this.session.readLine("Password: ", '*'); ((PasswordCallback) callback).setPassword(password.toCharArray()); } else { 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); try { session.put(Session.IGNORE_INTERRUPTS, Boolean.TRUE); String name = "Karaf local console user " + ShellUtil.getCurrentUserName(); Thread thread = new Thread(newSession, name); thread.join(); } finally { session.put(Session.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
private String readLine(String prompt, Character ch) throws IOException { Object oldIgnoreInterrupts = session.get(Session.IGNORE_INTERRUPTS); session.put(Session.IGNORE_INTERRUPTS, Boolean.TRUE); try { String line = session.readLine(prompt, ch); if (line == null) { throw new IOException("interrupted"); } return line; } finally { session.put(Session.IGNORE_INTERRUPTS, oldIgnoreInterrupts); } }
protected String getCompletionType(Session session) { String completion = (String) session.get(Session.COMPLETION_MODE); if (completion == null) { completion = Session.COMPLETION_MODE_GLOBAL; } return completion; }