Search in sources :

Example 16 with Session

use of org.apache.karaf.shell.api.console.Session in project karaf by apache.

the class CommandTracker method addingService.

@Override
public Object addingService(final ServiceReference reference) {
    Object service = context.getService(reference);
    if (service instanceof CommandWithAction) {
        final CommandWithAction oldCommand = (CommandWithAction) service;
        final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {

            @Override
            public String getScope() {
                return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
            }

            @Override
            public String getName() {
                return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
            }

            @Override
            public String getDescription() {
                final Command cmd = oldCommand.getActionClass().getAnnotation(Command.class);
                if (cmd != null) {
                    return cmd.description();
                }
                try {
                    Method method = oldCommand.getActionClass().getMethod("description");
                    method.setAccessible(true);
                    return (String) method.invoke(oldCommand.createNewAction());
                } catch (Throwable ignore) {
                }
                return getName();
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                final ArgumentCompleter completer = new ArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                return completer::complete;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return oldCommand.execute(commandSession, arguments);
            }
        };
        sessionFactory.getRegistry().register(command);
        return command;
    } else if (service instanceof org.apache.felix.gogo.commands.CommandWithAction) {
        final org.apache.felix.gogo.commands.CommandWithAction oldCommand = (org.apache.felix.gogo.commands.CommandWithAction) service;
        final org.apache.karaf.shell.api.console.Command command = new org.apache.karaf.shell.api.console.Command() {

            @Override
            public String getScope() {
                return reference.getProperty(CommandProcessor.COMMAND_SCOPE).toString();
            }

            @Override
            public String getName() {
                return reference.getProperty(CommandProcessor.COMMAND_FUNCTION).toString();
            }

            @Override
            public String getDescription() {
                final org.apache.felix.gogo.commands.Command cmd = oldCommand.getActionClass().getAnnotation(org.apache.felix.gogo.commands.Command.class);
                if (cmd != null) {
                    return cmd.description();
                }
                try {
                    Method method = oldCommand.getActionClass().getMethod("description");
                    method.setAccessible(true);
                    return (String) method.invoke(oldCommand.createNewAction());
                } catch (Throwable ignore) {
                }
                return getName();
            }

            @Override
            public Completer getCompleter(final boolean scoped) {
                final OldArgumentCompleter completer = new OldArgumentCompleter(oldCommand, getScope(), getName(), scoped);
                return completer::complete;
            }

            @Override
            public Parser getParser() {
                return null;
            }

            @Override
            public Object execute(Session session, List<Object> arguments) throws Exception {
                // TODO: remove not really nice cast
                CommandSession commandSession = (CommandSession) session.get(".commandSession");
                return oldCommand.execute(commandSession, arguments);
            }
        };
        sessionFactory.getRegistry().register(command);
        return command;
    } else {
        return null;
    }
}
Also used : List(java.util.List) Completer(org.apache.karaf.shell.api.console.Completer) Method(java.lang.reflect.Method) CommandWithAction(org.apache.karaf.shell.commands.CommandWithAction) Parser(org.apache.karaf.shell.api.console.Parser) CommandSession(org.apache.felix.service.command.CommandSession) Command(org.apache.karaf.shell.commands.Command) Session(org.apache.karaf.shell.api.console.Session) CommandSession(org.apache.felix.service.command.CommandSession)

Example 17 with Session

use of org.apache.karaf.shell.api.console.Session in project ddf by codice.

the class CommandJob method doExecute.

public void doExecute(JobExecutionContext context) throws JobExecutionException {
    String commandInput;
    try {
        commandInput = checkInput(context);
    } catch (CommandException e) {
        LOGGER.debug("unable to get command from job execution context", e);
        return;
    }
    SessionFactory sessionFactory = getSessionFactory();
    if (sessionFactory == null) {
        LOGGER.debug("unable to create session factory: command=[{}]", commandInput);
        return;
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Session session = null;
    try (PrintStream output = getPrintStream(byteArrayOutputStream)) {
        session = sessionFactory.create(null, output, output);
        if (session == null) {
            LOGGER.debug("unable to create session: command=[{}]", commandInput);
            return;
        }
        if (commandInput != null) {
            try {
                LOGGER.trace("Executing command [{}]", commandInput);
                session.execute(commandInput);
                LOGGER.trace("Execution Output: {}", byteArrayOutputStream.toString(StandardCharsets.UTF_8.name()));
            } catch (CommandNotFoundException e) {
                LOGGER.info("Command could not be found. Make sure the command's library has been loaded and try again: {}", e.getLocalizedMessage());
                LOGGER.debug("Command not found.", e);
            } catch (Exception e) {
                LOGGER.info("Error with execution. ", e);
            }
        }
    } catch (UnsupportedEncodingException e) {
        LOGGER.info("Unable to produce output", e);
    } finally {
        if (session != null) {
            session.close();
        }
        try {
            byteArrayOutputStream.close();
        } catch (IOException e) {
            LOGGER.debug("Could not close output stream", e);
        }
    }
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) PrintStream(java.io.PrintStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandNotFoundException(org.apache.felix.gogo.runtime.CommandNotFoundException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException) CommandNotFoundException(org.apache.felix.gogo.runtime.CommandNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Session(org.apache.karaf.shell.api.console.Session)

Example 18 with Session

use of org.apache.karaf.shell.api.console.Session in project ddf by codice.

the class CommandJobTest method testNullCommand.

/**
     * Do not execute command on null input
     *
     * @throws Exception
     */
@SuppressWarnings("ConstantConditions")
@Test
public void testNullCommand() throws Exception {
    // given
    String command = null;
    FirstArgumentAnswer captureInput = new FirstArgumentAnswer();
    Session session = getSession(captureInput);
    when(sessionFactory.create(any(), any(), any())).thenReturn(session);
    CommandJob job = getCommandJob();
    // when
    job.execute(getJobExecutionContext(command));
    // then
    verifySessionCalls(command, session, 0, 1);
}
Also used : Session(org.apache.karaf.shell.api.console.Session) Test(org.junit.Test)

Aggregations

Session (org.apache.karaf.shell.api.console.Session)18 PrintStream (java.io.PrintStream)7 Test (org.junit.Test)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Subject (javax.security.auth.Subject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 CommandSession (org.apache.felix.service.command.CommandSession)3 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 Closure (org.apache.felix.gogo.runtime.Closure)2 ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)2 Command (org.apache.karaf.shell.api.console.Command)2 Completer (org.apache.karaf.shell.api.console.Completer)2 SessionFactory (org.apache.karaf.shell.api.console.SessionFactory)2 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)2 ManagerImpl (org.apache.karaf.shell.impl.action.command.ManagerImpl)2 HeadlessSessionImpl (org.apache.karaf.shell.impl.console.HeadlessSessionImpl)2 JLineTerminal (org.apache.karaf.shell.impl.console.JLineTerminal)2 SessionFactoryImpl (org.apache.karaf.shell.impl.console.SessionFactoryImpl)2