Search in sources :

Example 1 with CommandProcessor

use of org.apache.felix.service.command.CommandProcessor in project opennms by OpenNMS.

the class KarafTestCase method executeCommand.

/**
     * Executes a shell command and returns output as a String.
     * Commands have a default timeout of 10 seconds.
     *
     * @param command
     * @return
     */
protected String executeCommand(final String command) {
    try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArrayOutputStream)) {
        Subject subject = new Subject();
        subject.getPrincipals().add(new RolePrincipal("admin"));
        return Subject.doAs(subject, new PrivilegedExceptionAction<String>() {

            @Override
            public String run() throws Exception {
                final CommandProcessor commandProcessor = getOsgiService(CommandProcessor.class);
                final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
                LOG.info("{}", command);
                Object response = commandSession.execute(command);
                LOG.info("Response: {}", response);
                printStream.flush();
                return byteArrayOutputStream.toString();
            }
        });
    } catch (Exception e) {
        LOG.error("Error while executing command", e);
        throw new RuntimeException(e);
    }
}
Also used : PrintStream(java.io.PrintStream) CommandSession(org.apache.felix.service.command.CommandSession) CommandProcessor(org.apache.felix.service.command.CommandProcessor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Subject(javax.security.auth.Subject)

Example 2 with CommandProcessor

use of org.apache.felix.service.command.CommandProcessor in project camel by apache.

the class CamelKarafTestSupport method executeCommand.

/**
     * Executes a shell command and returns output as a String.
     * Commands have a default timeout of 10 seconds.
     *
     * @param command    The command to execute.
     * @param timeout    The amount of time in millis to wait for the command to execute.
     * @param silent     Specifies if the command should be displayed in the screen.
     * @param principals The principals (e.g. RolePrincipal objects) to run the command under
     */
protected String executeCommand(final String command, final Long timeout, final Boolean silent, final Principal... principals) {
    waitForCommandService(command);
    String response;
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final Callable<String> commandCallable = new Callable<String>() {

        @Override
        public String call() throws Exception {
            try {
                if (!silent) {
                    System.err.println(command);
                }
                final CommandProcessor commandProcessor = getOsgiService(CommandProcessor.class);
                final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
                commandSession.execute(command);
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            printStream.flush();
            return byteArrayOutputStream.toString();
        }
    };
    FutureTask<String> commandFuture;
    if (principals.length == 0) {
        commandFuture = new FutureTask<String>(commandCallable);
    } else {
        // If principals are defined, run the command callable via Subject.doAs()
        commandFuture = new FutureTask<String>(new Callable<String>() {

            @Override
            public String call() throws Exception {
                Subject subject = new Subject();
                subject.getPrincipals().addAll(Arrays.asList(principals));
                return Subject.doAs(subject, new PrivilegedExceptionAction<String>() {

                    @Override
                    public String run() throws Exception {
                        return commandCallable.call();
                    }
                });
            }
        });
    }
    try {
        executor.submit(commandFuture);
        response = commandFuture.get(timeout, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        e.printStackTrace(System.err);
        response = "SHELL COMMAND TIMED OUT: ";
    }
    return response;
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Callable(java.util.concurrent.Callable) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Subject(javax.security.auth.Subject) CommandSession(org.apache.felix.service.command.CommandSession) CommandProcessor(org.apache.felix.service.command.CommandProcessor)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 Subject (javax.security.auth.Subject)2 CommandProcessor (org.apache.felix.service.command.CommandProcessor)2 CommandSession (org.apache.felix.service.command.CommandSession)2 Callable (java.util.concurrent.Callable)1 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1