0

The below code executes karaf commands in the console. This is working fine in karaf version 3.0.3. It fails in 4.0.0 or above

@Inject 
CommandProcessor commandProcessor;
private class dummyCallable implements Callable{
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
    String[] commands;
    dummyCallable( String[] commands){
        this.commands = commands;
    }
    public String call() {
        try {
            for(String command:commands) {
                System.err.println(command);
                commandSession.execute(command);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        return byteArrayOutputStream.toString();
    }
}
protected String executeCommands(final String ...commands) {
    String response;
    dummyCallable dd = new dummyCallable(commands);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    FutureTask<String> futureTask2 = new FutureTask<String>(dd);
    try {
        executor.submit(futureTask2);
        response =  futureTask2.get();
    } catch (Exception e) {
        e.printStackTrace(System.err);
        response = "SHELL COMMAND TIMED OUT: ";
    }
return response;
}

Can anybody provide me with a code which will work karaf 4.0.0

Charity
  • 213
  • 1
  • 5
  • 23

1 Answers1

2

In Karaf 4, you should inject into your services a SessionFactory and call sessionFactory.createSession(System.in, printStream, System.err)

The methods are the same between Karaf3 and Karaf4, the class and package have changed :

  • CommandProcessor => SessionFactory
  • CommandSession => Session
Jérémie B
  • 10,611
  • 1
  • 26
  • 43