Search in sources :

Example 6 with SessionFactory

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

the class SessionFactoryImplTest method createWithNullInputStream.

@Test
public void createWithNullInputStream() throws UnsupportedEncodingException {
    final SessionFactory sessionFactory = new SessionFactoryImpl(createMock(ThreadIO.class));
    sessionFactory.create(null, createMock(PrintStream.class), createMock(PrintStream.class));
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) ThreadIO(org.apache.felix.service.threadio.ThreadIO) PrintStream(java.io.PrintStream) SessionFactoryImpl(org.apache.karaf.shell.impl.console.SessionFactoryImpl) Test(org.junit.Test)

Example 7 with SessionFactory

use of org.apache.karaf.shell.api.console.SessionFactory 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 8 with SessionFactory

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

the class Activator method doStart.

@Override
protected void doStart() throws Exception {
    SessionFactory sf = getTrackedService(SessionFactory.class);
    if (sf == null) {
        return;
    }
    if (!ensureStartupConfiguration("org.apache.karaf.shell")) {
        return;
    }
    RegexCommandLoggingFilter filter = new RegexCommandLoggingFilter();
    filter.setPattern("ssh (.*?)-P +([^ ]+)");
    filter.setGroup(2);
    register(CommandLoggingFilter.class, filter);
    filter = new RegexCommandLoggingFilter();
    filter.setPattern("ssh (.*?)--password +([^ ]+)");
    filter.setGroup(2);
    register(CommandLoggingFilter.class, filter);
    sessionFactory = sf;
    sessionFactory.getRegistry().getService(Manager.class).register(SshAction.class);
    if (Boolean.parseBoolean(bundleContext.getProperty("karaf.startRemoteShell"))) {
        createAndRunSshServer();
    }
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) RegexCommandLoggingFilter(org.apache.karaf.shell.support.RegexCommandLoggingFilter) Manager(org.apache.karaf.shell.api.action.lifecycle.Manager)

Example 9 with SessionFactory

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

the class ActionMaskingCallbackTest method setUp.

@Before
public void setUp() {
    ThreadIO tio = new ThreadIOImpl();
    CommandProcessor cp = new CommandProcessorImpl(tio);
    SessionFactory sf = new SessionFactoryImpl(tio);
    InputStream is = new ByteArrayInputStream(new byte[0]);
    PrintStream os = new PrintStream(new ByteArrayOutputStream());
    Session session = new HeadlessSessionImpl(sf, cp, is, os, os);
    parser = new KarafParser(session);
    ActionCommand cmd = new ActionCommand(null, UserAddCommand.class);
    cb = ActionMaskingCallback.build(cmd);
}
Also used : ThreadIO(org.apache.felix.service.threadio.ThreadIO) SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) PrintStream(java.io.PrintStream) HeadlessSessionImpl(org.apache.karaf.shell.impl.console.HeadlessSessionImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CommandProcessorImpl(org.apache.felix.gogo.runtime.CommandProcessorImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KarafParser(org.apache.karaf.shell.impl.console.parsing.KarafParser) ByteArrayInputStream(java.io.ByteArrayInputStream) CommandProcessor(org.apache.felix.service.command.CommandProcessor) SessionFactoryImpl(org.apache.karaf.shell.impl.console.SessionFactoryImpl) Session(org.apache.karaf.shell.api.console.Session) Before(org.junit.Before)

Aggregations

SessionFactory (org.apache.karaf.shell.api.console.SessionFactory)9 PrintStream (java.io.PrintStream)5 Session (org.apache.karaf.shell.api.console.Session)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ThreadIOImpl (org.apache.felix.gogo.runtime.threadio.ThreadIOImpl)2 CommandProcessor (org.apache.felix.service.command.CommandProcessor)2 ThreadIO (org.apache.felix.service.threadio.ThreadIO)2 SessionFactoryImpl (org.apache.karaf.shell.impl.console.SessionFactoryImpl)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1