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?
问问题
1832 次
4 回答
2
从 Apache Karaf 3.0.0 开始,大多数命令都由 OSGi 服务支持。
例如,bundle:watch 命令正在使用服务“ org.apache.karaf.bundle.core.BundleWatcher
”。
所以只要绑定这个服务,就可以很方便的调用 bundle:watch 功能了。
于 2015-08-07T12:02:24.563 回答
0
init 脚本还可用于测试条件并在满足该条件时运行命令,因此无需自己创建命令会话。
于 2014-06-12T03:33:58.957 回答
0
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
于 2014-11-21T22:50:22.030 回答