以编程方式启动 OSGi (Equinox)? [英] Programmatically Start OSGi (Equinox)?

查看:31
本文介绍了以编程方式启动 OSGi (Equinox)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够轻松启动 OSGi 框架(最好是 Equinox)并从 java main 加载我的 pom 中列出的任何包.

I'd like to be able to easily start an OSGi framework (preferably Equinox) and load up any bundles listed in my pom from a java main.

这可能吗?如果是,怎么办?

Is this possible? If so, how?

似乎 pax 工具可以做到这一点,但我似乎找不到任何说明这种情况的文档.我知道我可以像这样启动 Equinox:

It seems like the pax tools would do this, but I can't seem to find any documentation indicating such. I know I can start up Equinox like so:

BundleContext context = EclipseStarter.startup( ( new String[] { "-console" } ), null );

但我想做更多 - 就像我说的:加载更多包,也许启动一些服务等.

But I'd like to do more - like I said: load more bundles in, maybe start some services, etc.

推荐答案

可以使用 FrameworkFactory API 以编程方式启动任何 OSGi 框架(R4.1 或更高版本):

Any OSGi framework (R4.1 or later) can be started programmatically using the FrameworkFactory API:

ServiceLoader<FrameworkFactory> ffs = ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory ff = ffs.iterator().next();
Map<String,Object> config = new HashMap<String,Object>();
// add some params to config ...
Framework fwk = ff.newFramework(config);
fwk.start();

OSGi 框架现在正在运行.由于Framework 扩展了Bundle,您可以调用getBundleContext 并调用所有正常的API 方法来操作bundle、注册服务等.例如>

The OSGi framework is now running. Since Framework extends Bundle you can call getBundleContext and call all of the normal API methods to manipulate bundles, register services, etc. For example

BundleContext bc = fwk.getBundleContext();
bc.installBundle("file:/path/to/bundle.jar");
bc.registerService(MyService.class.getName(), new MyServiceImpl(), null);
// ...

最后,您应该简单地等待框架关闭:

Finally you should simply wait for the framework to shutdown:

fwk.stop();
fwk.waitForStop(0);

重申一下,这种方法适用于任何 OSGi 框架,包括 Equinox 和 Felix,只需将框架 JAR 放在类路径中即可.

To reiterate, this approach works for any OSGi framework including Equinox and Felix just by putting the framework JAR on the classpath.

这篇关于以编程方式启动 OSGi (Equinox)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆