java 以编程方式启动 OSGi (Equinox)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4673406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically Start OSGi (Equinox)?
提问by javamonkey79
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.
我希望能够轻松启动 OSGi 框架(最好是 Equinox)并从 java main 加载我的 pom 中列出的任何包。
Is this possible? If so, how?
这可能吗?如果是这样,如何?
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:
似乎 pax 工具可以做到这一点,但我似乎找不到任何说明这一点的文档。我知道我可以像这样启动 Equinox:
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.
但我想做更多 - 就像我说的:加载更多包,也许启动一些服务等。
回答by Neil Bartlett
Any OSGi framework (R4.1 or later) can be started programmatically using the FrameworkFactory
API:
任何 OSGi 框架(R4.1 或更高版本)都可以使用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();
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
OSGi 框架现在正在运行。由于Framework
扩展,Bundle
您可以调用getBundleContext
和调用所有正常的 API 方法来操作包、注册服务等。例如
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);
To reiterate, this approach works for anyOSGi framework including Equinox and Felix just by putting the framework JAR on the classpath.
重申一下,这种方法适用于任何OSGi 框架,包括 Equinox 和 Felix,只需将框架 JAR 放在类路径上即可。
回答by earcam
This thread might be a bit stale, but anyway...
这个线程可能有点陈旧,但无论如何......
Pax has excellent support for maven urls, it even has a wrap url handler allowing you to dynamically convert non-osgi jar to nice tidy bundles.
Pax 对 maven url 有很好的支持,它甚至有一个 wrap url 处理程序,允许您将非 osgi jar 动态转换为漂亮的 tidy bundle。
http://wiki.ops4j.org/display/paxurl/Mvn+Protocol
http://wiki.ops4j.org/display/paxurl/Mvn+Protocol
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-wrap</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-mvn</artifactId>
<version>1.2.5</version>
</dependency>
The command would then be:
该命令将是:
install -s mvn:groupId:artifactId:version:classifier
Note: chicken-egg scenario - you have to install these using a file: url handler first or put them into an autodeploy directory.
注意:鸡-蛋方案 - 您必须先使用 file: url 处理程序安装它们,或者将它们放入 autodeploy 目录中。
Karaf has this all build in to it's distro, so maybe have a look at Karaf launcher source?
Karaf 将所有这些都内置到它的发行版中,所以也许看看 Karaf 启动器源代码?
2nd note: deploying snapshots are enable by appending @snapshots to the repo URL, configuration is managed via ConfigAdmin
第二个注意事项:通过将@snapshots 附加到 repo URL 来启用部署快照,配置通过 ConfigAdmin 进行管理
In terms of managing all your POM defined dependencies have a look at Karaf features - there's a plugin that would enable to generate a features XML file which can then be used to deploy your entire app: http://karaf.apache.org/manual/2.1.99-SNAPSHOT/developers-guide/features-maven-plugin.html
在管理所有 POM 定义的依赖项方面,请查看 Karaf 功能 - 有一个插件可以生成功能 XML 文件,然后可用于部署整个应用程序:http: //karaf.apache.org/manual /2.1.99-SNAPSHOT/developers-guide/features-maven-plugin.html
Further more this XML artifact can be deployed to your OBR, so you can take a vanilla Felix/Equinox/Karaf setup, add the mvn url handler and configure with your company's mvn repo then provision the entire app =)
此外,此 XML 工件可以部署到您的 OBR,因此您可以采用普通的 Felix/Equinox/Karaf 设置,添加 mvn url 处理程序并使用您公司的 mvn repo 进行配置,然后配置整个应用程序 =)
回答by KitsuneYMG
Edit: Realized you wanted to start from inside java. Shame on me for not reading close enough
编辑:意识到您想从 java 内部开始。为我阅读不够仔细而感到羞耻
Check out this link. http://www.eclipsezone.com/eclipse/forums/t93976.rhtml
看看这个链接。 http://www.eclipsezone.com/eclipse/forums/t93976.rhtml
Essentially
本质上
public static void main(String args[]) throws Exception {
String[] equinoxArgs = {"-console","1234","-noExit"};
BundleContext context = EclipseStarter.startup(equinoxArgs,null);
Bundle bundle = context.installBundle(
"http://www.eclipsezone.com/files/jsig/bundles/HelloWorld.jar");
bundle.start();
}
Edit: Maven
编辑:马文
It seems that https://groups.google.com/group/spring-osgi/web/maven-url-handler?pli=1contains an OSGi URl Handlers Service that can take URLS of the following format and load bundles from them ( mvn://repo/bundle_path )
似乎https://groups.google.com/group/spring-osgi/web/maven-url-handler?pli=1包含一个 OSGi URl 处理程序服务,它可以获取以下格式的 URL 并从中加载包( mvn://repo/bundle_path )