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

发布于 2025-01-01 16:07:55 字数 385 浏览 0 评论 0原文

如果我检测到我正在开发环境中运行,我想自动调用 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来调用它。这看起来太复杂了。有更好的方法吗?

I want to automatically invoke the Karaf "dev:watch" command if I detect that I'm running in a dev environment. I've considered adding dev:watch * directly to etc/shell.init.script but I don't want it to run unconditionally. So, I'm considering creating a simple service that checks a Java property (something simple like -Ddevelopment=true) and invokes org.apache.karaf.shell.dev.Watch itself. I think I can ask OSGi for a Function instance with (&(osgi.command.function=watch)(osgi.command.scope=dev)) but then I need to create a mock CommandSession just to invoke it. That just seems too complicated. Is there a better approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯角 2025-01-08 16:07:55

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

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

所以只要绑定这个服务,就可以非常方便地调用bundle:watch功能。

Since Apache Karaf 3.0.0 most commands are backed by OSGi services.

So for example the bundle:watch command is using the service
"org.apache.karaf.bundle.core.BundleWatcher".

So just bind this service and you can call the bundle:watch functionality very conveniently.

温柔一刀 2025-01-08 16:07:55

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

您需要使用 CommandSession 类,这并不简单。 这篇博文可以为您提供指导。它与 Pax Exam 相关,但可以应用于任何情况。还有更多替代方案,例如使用远程 SSH 客户端,甚至更好的远程 JXM 管理控制台 (

It has been a while since the question, but I will answer.

You need to use CommandSession class, it is not trivial. This blog post could guide you. It is related with Pax Exam, but could be applied in any situation. There are more alternatives like using a remote SSH client or even better the remote JXM management console (reference).

白云悠悠 2025-01-08 16:07:55

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

The init script can also be used to test for a condition and run a command if that condition is satisfied, so there is no need to create a command session yourself.

浊酒尽余欢 2025-01-08 16:07:55

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

在用于集成测试 Karaf 本身的 KarafTestSupport 类中
(参见https://git-wip-us.apache.org/repos/asf?p=karaf.git;a=blob;f=itests/src/test/java/或g/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

The Karaf source itself reveals one answer:

In the KarafTestSupport class which is used for integration testing Karaf itself
(see 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)

The relevant method starts:

/**
 * 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文