【问题标题】:How do I programatically invoke a Felix/Karaf shell command?如何以编程方式调用 Felix/Karaf shell 命令?
【发布时间】:2012-02-24 12:30:12
【问题描述】:

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

【问题讨论】:

    标签: apache-felix apache-karaf


    【解决方案1】:

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

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

    所以只要绑定这个服务,就可以很方便的调用 bundle:watch 功能了。

    【讨论】:

      【解决方案2】:

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

      在用于集成测试 Karaf 本身的 KarafTestSupport 类中 (见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
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-03-21
            • 2014-12-13
            • 2013-01-26
            • 2014-05-17
            • 1970-01-01
            • 2023-04-05
            • 2010-12-16
            • 2021-05-08
            相关资源
            最近更新 更多