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

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

问题描述

我希望能够轻松启动OSGi框架(最好是Equinox)并从java main加载我的pom中列出的所有bundle。

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.

推荐答案

任何OSGi框架(R4.1或更高版本)都可以使用 FrameworkFactory API:

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方法。例如

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天全站免登陆