Search in sources :

Example 1 with AbstractCommand

use of org.apache.felix.gogo.commands.basic.AbstractCommand in project fabric8 by jboss-fuse.

the class CommandSupport method executeCommand.

private static void executeCommand(String cmdstr, CommandSession commandSession) {
    LOGGER.info(cmdstr);
    // When using the ssh:ssh command, the current ssh client calls KarafAgentFactory which expects the SSH_AUTH_SOCK env variable to be set,
    // so work around the problem by registering the CommandSession in OSGi so that this variable is correctly initialised
    BundleContext syscontext = ServiceLocator.getSystemContext();
    ServiceRegistration<CommandSession> reg = syscontext.registerService(CommandSession.class, commandSession, null);
    try {
        // Get the command service
        List<String> tokens = Arrays.asList(cmdstr.split("\\s"));
        String[] header = tokens.get(0).split(":");
        Assert.assertTrue("Two tokens in: " + tokens.get(0), header.length == 2);
        String filter = "(&(osgi.command.scope=" + header[0] + ")(osgi.command.function=" + header[1] + "))";
        AbstractCommand command = (AbstractCommand) ServiceLocator.awaitService(Function.class, filter, 30000, TimeUnit.MILLISECONDS);
        commandSession.put(AbstractCommand.class.getName(), command);
        Class<?> actionClass = command.getActionClass();
        LOGGER.debug("Using action: {} from {}", actionClass, actionClass.getClassLoader());
        boolean keepRunning = true;
        while (!Thread.currentThread().isInterrupted() && keepRunning) {
            try {
                commandSession.execute(cmdstr);
                keepRunning = false;
            } catch (Exception ex) {
                if (retryException(ex)) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException iex) {
                        Thread.currentThread().interrupt();
                    }
                } else {
                    throw new CommandExecutionException(ex);
                }
            }
        }
    } finally {
        reg.unregister();
    }
}
Also used : AbstractCommand(org.apache.felix.gogo.commands.basic.AbstractCommand) Function(org.apache.felix.service.command.Function) CommandSession(org.apache.felix.service.command.CommandSession) BundleContext(org.osgi.framework.BundleContext)

Aggregations

AbstractCommand (org.apache.felix.gogo.commands.basic.AbstractCommand)1 CommandSession (org.apache.felix.service.command.CommandSession)1 Function (org.apache.felix.service.command.Function)1 BundleContext (org.osgi.framework.BundleContext)1