0

下面的代码在控制台中执行 karaf 命令。这在 karaf 版本 3.0.3 中运行良好。它在 4.0.0 或更高版本中失败

@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;
}

任何人都可以为我提供一个可以使用 karaf 4.0.0 的代码吗

4

1 回答 1

2

在 Karaf 4 中,您应该将 a 注入您的服务SessionFactory并调用sessionFactory.createSession(System.in, printStream, System.err)

Karaf3和Karaf4的方法相同,类和包发生了变化:

  • CommandProcessor => 会话工厂
  • CommandSession => 会话
于 2016-02-09T21:20:16.983 回答