如何以编程方式调用 Felix/Karaf shell 命令?
如果我检测到我正在开发环境中运行,我想自动调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自 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.
问题已经有一段时间了,但我会回答。
您需要使用 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).
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.
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)
相关方法开始:
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: