我如何以编程方式调用Felix / Karaf shell命令?

时间:2012-02-01 19:59:05

标签: apache-felix apache-karaf

如果我发现我在开发环境中运行,我想自动调用Karaf“dev:watch”命令。我考虑过将dev:watch *直接添加到etc / shell.init.script但我不希望它无条件地运行。所以,我正在考虑创建一个简单的服务来检查Java属性(像-Ddevelopment=true这样简单的东西)并调用org.apache.karaf.shell.dev.Watch本身。我想我可以向OSGi询问一个带有(&(osgi.command.function=watch)(osgi.command.scope=dev))的Function实例,但是我需要创建一个模拟CommandSession来调用它。这看起来太复杂了。有更好的方法吗?

4 个答案:

答案 0 :(得分:2)

自Apache Karaf 3.0.0以来,大多数命令都由OSGi服务支持。

例如,bundle:watch命令正在使用该服务 " org.apache.karaf.bundle.core.BundleWatcher"

因此,只需绑定此服务,您就可以非常方便地调用bundle:watch功能。

答案 1 :(得分:0)

问题已经有一段时间了,但我会回答。

你需要使用CommandSession类,这不是一件容易的事。 This blog post可以指导你。它与Pax考试有关,但可以在任何情况下应用。还有更多选择,例如使用远程SSH客户端,甚至更好的远程JXM管理控制台(reference)。

答案 2 :(得分:0)

如果满足条件,init脚本也可用于测试条件并运行命令,因此无需自己创建命令会话。

答案 3 :(得分:0)

Karaf来源本身揭示了一个答案:

在KarafTestSupport类中,用于集成测试Karaf本身 (见https://git-wip-us.apache.org/repos/asf?p=karaf.git;a=blob;f=itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java;h=ebdea09ae8c6d926c8e4ac1fae6672f2c00a53dc;hb=HEAD

相关方法开始:

/**
 * Executes a shell command and returns output as a String.
 * Commands have a default timeout of 10 seconds.
 *
 * @param command    The command to execute.
 * @param timeout    The amount of time in millis to wait for the command to execute.
 * @param silent     Specifies if the command should be displayed in the screen.
 * @param principals The principals (e.g. RolePrincipal objects) to run the command under
 * @return
 */
protected String executeCommand(final String command, final Long timeout, final Boolean silent, final Principal ... principals) {
    waitForCommandService(command);

    String response;
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final SessionFactory sessionFactory = getOsgiService(SessionFactory.class);
    final Session session = sessionFactory.create(System.in, printStream, System.err);
    //
    //
    //
    // Snip
相关问题