Search in sources :

Example 1 with CommandNotFoundException

use of org.apache.felix.gogo.runtime.CommandNotFoundException 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)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CommandNotFoundException (org.apache.felix.gogo.runtime.CommandNotFoundException)1 Session (org.apache.karaf.shell.api.console.Session)1 SessionFactory (org.apache.karaf.shell.api.console.SessionFactory)1 JobExecutionException (org.quartz.JobExecutionException)1