27

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

这可能吗?如果是这样,怎么做?

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

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

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

4

3 回答 3

54

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

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 方法来操作包、注册服务等。例如

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

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

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

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

于 2011-01-12T20:51:13.443 回答
5

这个线程可能有点陈旧,但无论如何......

Pax 对 maven url 有很好的支持,它甚至有一个 wrap url 处理程序,允许您将非 osgi jar 动态转换为漂亮整洁的包。

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>

该命令将是:

install -s mvn:groupId:artifactId:version:classifier

注意:鸡蛋场景 - 您必须先使用 file: url 处理程序安装它们,或者将它们放入自动部署目录。

Karaf 将这一切都内置到了它的发行版中,所以也许看看 Karaf 启动器的源代码?

第二个注意事项:通过将@snapshots 附加到 repo URL 来启用部署快照,配置通过 ConfigAdmin 管理

在管理所有 POM 定义的依赖项方面,请查看 Karaf 功能 - 有一个插件可以生成功能 XML 文件,然后可用于部署整个应用程序:http: //karaf.apache.org/manual /2.1.99-SNAPSHOT/developers-guide/features-maven-plugin.html

此外,此 XML 工件可以部署到您的 OBR,因此您可以采用普通的 Felix/Equinox/Karaf 设置,添加 mvn url 处理程序并使用您公司的 mvn 存储库进行配置,然后配置整个应用程序 =)

于 2011-03-24T18:02:35.013 回答
4

编辑:意识到你想从java内部开始。为我没有读得足够近而感到羞耻

看看这个链接。 http://www.eclipsezone.com/eclipse/forums/t93976.rhtml

本质上

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();
}

编辑:马文

似乎https://groups.google.com/group/spring-osgi/web/maven-url-handler?pli=1包含一个 OSGi URl 处理程序服务,它可以获取以下格式的 URL 并从中加载包( mvn://repo/bundle_path )

于 2011-01-12T20:39:50.217 回答