【发布时间】:2014-12-22 22:45:15
【问题描述】:
我正在尝试使用 maven 将 apache felix 嵌入到一个简单的 hello world java 项目中,但我找不到获取对捆绑服务的引用的方法。我已经将 org.apache.felix.bundlerepository 包从 jar 安装到 OSGi 中,并将其作为 maven 依赖项添加到我的项目中。之后,我开始捆绑,从中获取 BundleContext,然后在该捆绑上下文上调用 getServiceReference(RepositoryAdmin.class.getName())。我不满意的第一件事是我必须使用已安装包中的 BundleContext,如果我使用框架的 BundleContext,则 ServiceReference 将始终为空。这不方便。
第二个更重要的问题是,当我最终从 bundlerepository 包中收到对 RepositoryAdmin 服务的引用时,我无法将其转换为 org.apache.felix.bundlerepository.RepositoryAdmin,执行以下代码:
(RepositoryAdmin)admin.getBundleContext().getService(ref)
会抛出这个异常:
java.lang.ClassCastException: org.apache.felix.bundlerepository.impl.RepositoryAdminImpl cannot be cast to org.apache.felix.bundlerepository.RepositoryAdmin
我知道这是一种类路径问题,可能是由接口不兼容引起的,但我使用相同版本(2.0.2)的捆绑 jar 作为我项目的 maven 依赖项。 我也知道 Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA osgi 配置选项应该强制捆绑使用来自主机应用程序的包,但它没有帮助我。
这是我的项目https://github.com/ArtemZ/osgi-study/blob/master/src/main/java/com/artemz/demo/Main.java 的主类,它很乱,因为我在上面尝试了不同的东西以获取对服务的引用,但实际上没有一个对我有用。
希望有人能帮助我,因为我对这个问题真的很绝望。
【问题讨论】:
标签: java osgi apache-felix