/** * 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 void removedService(ServiceReference<Object> reference, List<Command> commands) { for (Command command : commands) { sessionFactory.getRegistry().unregister(command); } } }
/** * 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 void removedService(ServiceReference<Object> reference, List<Command> commands) { for (Command command : commands) { sessionFactory.getRegistry().unregister(command); } } }
@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; } } });
private void register(final Function command) { log.debug("Registering command: {}", command); if (sessionFactory != null) { sessionFactory.getRegistry().register(command); } else { log.warn("Unable to register command, sessionFactory is null: {}", 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 removedService(ServiceReference reference, Object service) { if (service instanceof org.apache.karaf.shell.api.console.Command) { sessionFactory.getRegistry().unregister(service); } if (service instanceof List) { List<org.apache.karaf.shell.api.console.Command> commands = (List<org.apache.karaf.shell.api.console.Command>) service; for (org.apache.karaf.shell.api.console.Command command : commands) { sessionFactory.getRegistry().unregister(command); } } context.ungetService(reference); } }
@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 Set<String> getCommands() { return factory.getRegistry().getCommands().stream() .map(c -> c.getScope() + ":" + c.getName()) .collect(Collectors.toSet()); } @Override
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 } }
@Override public Set<String> getCommands() { return factory.getRegistry().getCommands().stream() .map(c -> c.getScope() + ":" + c.getName()) .collect(Collectors.toSet()); } @Override
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 } }
private void waitForCommandService(String command) { // the commands are represented by services. Due to the asynchronous nature of services they may not be // immediately available. This code waits the services to be available, in their secured form. It // means that the code waits for the command service to appear with the roles defined. if (command == null || command.length() == 0) { return; } int spaceIdx = command.indexOf(' '); if (spaceIdx > 0) { command = command.substring(0, spaceIdx); } int colonIndx = command.indexOf(':'); String scope = (colonIndx > 0) ? command.substring(0, colonIndx) : "*"; String name = (colonIndx > 0) ? command.substring(colonIndx + 1) : command; try { long start = System.currentTimeMillis(); long cur = start; while (cur - start < SERVICE_TIMEOUT) { if (sessionFactory.getRegistry().getCommand(scope, name) != null) { return; } Thread.sleep(100); cur = System.currentTimeMillis(); } } catch (Exception e) { throw new RuntimeException(e); } }
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(); }
@Override public void add(final BeanEntry<Named, Action> beanEntry, final BundleContext bundleContext) throws Exception { Command command = beanEntry.getImplementationClass().getAnnotation(Command.class); if (command != null) { log.debug("Registering command: {}", beanEntry); sessionFactory.getRegistry().register(new BeanEntryCommand(beanLocator, beanEntry)); } else { log.warn("Missing @Command annotation on action: {}", beanEntry); } }
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(); }
protected void discoverCommands(Session session, ClassLoader cl, String resource) throws IOException, ClassNotFoundException { Manager manager = new ManagerImpl(session.getRegistry(), session.getFactory().getRegistry(), true); Enumeration<URL> urls = cl.getResources(resource); while (urls.hasMoreElements()) { URL url = urls.nextElement(); BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream())); String line = r.readLine(); while (line != null) { line = line.trim(); if (line.length() > 0 && line.charAt(0) != '#') { final Class<?> actionClass = cl.loadClass(line); manager.register(actionClass); } line = r.readLine(); } r.close(); } }
final PrintStream printStream = new PrintStream(byteArrayOutputStream); final SessionFactory sessionFactory = getOsgiService(SessionFactory.class); final Session session = sessionFactory.create(System.in, printStream, System.err);
protected void discoverCommands(Session session, ClassLoader cl, String resource) throws IOException, ClassNotFoundException { Manager manager = new ManagerImpl(session.getRegistry(), session.getFactory().getRegistry(), true); Enumeration<URL> urls = cl.getResources(resource); while (urls.hasMoreElements()) { URL url = urls.nextElement(); BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream())); String line = r.readLine(); while (line != null) { line = line.trim(); if (line.length() > 0 && line.charAt(0) != '#') { final Class<?> actionClass = cl.loadClass(line); manager.register(actionClass); } line = r.readLine(); } r.close(); } }