java - OSGi:无法直接从存储库安装超过 1 个资源?

标签 java osgi osgi-bundle resolver bnd

您好,感谢您的阅读,

我当前在将多个资源安装到我的 OSGi 框架中时遇到问题。我正在使用带有 Bndtools 插件的 Eclipse IDE。

我有一个在线 OSGi 存储库,遵循 RFC 112 。我已经成功编写了一个方法,可以将所有这些资源解析为 List<Resource> 。现在,我可以通过使用列表的 .get(int) 方法来获取资源。

我写了一个方法,可以直接将资源安装到框架中,代码如下:

public void installResource(Resource res) {

    RepositoryContent myRepoContext = (RepositoryContent) res;

    InputStream myInputStream = myRepoContext.getContent();

    try {
        installBundle(myInputStream);
        System.out.println("Installation of " + res.toString() + " SUCCESSFUL.");
    } catch (Exception e) {
        System.out.println("Installation of " + res.toString() + " FAILED.");
        e.printStackTrace();
    }

这是使用 installBundle(InputStream inputstream我写的方法如下:

public void installBundle(InputStream inputStream) {
    Bundle bundle = null;
    try {
        bundle = context.installBundle(null, inputStream);
        System.out.println("Context installed bundle correctly");

    } catch (BundleException e) {
        System.out.println("Context could not install bundle correctly.");
        e.printStackTrace();
    }

}

现在我想做的是将多个资源安装到我的框架中。我希望它们出现在 RESOLVED 中状态,只是 INSTALLED状态就够了。

当我尝试安装 1 个资源时,一切正常:

installResource(resource1);

这工作正常,因为如果我随后使用 lb 请求框架中当前安装的 bundle /资源命令在 shell 中,我可以看到该包位于 INSTALLED 中状态:

13|Installed  |    1|org.dyamand.test.serialization (0.13.15)|0.13.15

到目前为止一切顺利,资源的安装方法正在运行。虽然我想。当我尝试安装另一个资源时,出现了问题。我没有收到任何错误消息或异常。相反,我看到我自己打印出类似 Installation of " + res.toString() + " SUCCESSFUL." 的消息。 。这应该意味着资源已正确安装,因为没有错误或没有返回任何内容。

但是当我使用 lb 再次检查 bundle 时,第二个资源/包不存在。第一个是。已使用许多不同的资源尝试过此操作(有些资源处于 INSTALLED 状态,有些资源处于 RESOLVED 状态)。 它从来没有工作过,只是安装了第一个资源。

例如,如果我停止框架并使用以下代码再次启动它:

installResource(resource1);
installResource(resource2);

唯一发生的事情是资源1开始安装。

如果之前的资源不在 RESOLVED 中,则可能无法安装第二个资源或ACTIVE状态?

我一开始也是这么想的,但事实证明这是错误的。我没有直接从 OSGi 存储库中获取的资源进行安装,而是访问 Maven Central 并获得了 3 个随机包及其直接 URL。可以在下面的代码中找到 URL 以及如何调用该方法进行安装。再次使用installBundle方法:

     installBundle("https://search.maven.org/remotecontent?filepath=org/osgi/org.osgi/3.0.0/org.osgi-3.0.0.jar");
     installBundle("https://search.maven.org/remotecontent?filepath=org/osgi/enroute/examples/microservice/rest-app-jpa/0.0.1/rest-app-jpa-0.0.1.jar");
     installBundle("https://search.maven.org/remotecontent?filepath=org/coindirect/api/1.0.1/api-1.0.1.jar");

当像这样启动框架,然后使用 lb 请求 bundle 时,所有 3 个都出现在 INSTALLED 中状态。请参阅lb输出如下:

 13|Installed  |    1|osgi (3.0.0)|3.0.0
 14|Installed  |    1|rest-app-jpa (0.0.1)|0.0.1
 15|Installed  |    1|https://search.maven.org/remotecontent?filepath=org/coindirect/api/1.0.1/api-1.0.1.jar (0.0.0)|0.0.0

我一直在互联网上搜索为什么会发生这种情况的解释,所以我向大家提出问题:有人知道为什么我不能直接在框架中安装多个资源吗?

我不明白直接从 URL 安装与直接从资源安装有什么不同,因为它们都可以工作(至少第一次)。有人已经说过,我可以从他们的直接 URL 链接安装所有包,是的,这可以工作,但是:我没有从存储库中取出的资源的直接 URL 链接。我只能访问Capability具有命名空间 osgi.content就像我的方法获取InputStream一样以便可以安装资源。

非常感谢您的帮助。谢谢!

最佳答案

我假设context.installBundle(null, inputStream)指的是this method 。如果是这样,您将传递 null 作为包的 locationThe specs没有明确说明如果 locationnull 会发生什么,但他们确实说了

Every bundle is uniquely identified by its location string. If an installed bundle is using the specified location, the installBundle methods must return the Bundle object for that installed bundle and not install a new bundle.

所以我的猜测是 null 是(或以某种方式成为)有效的 location,然后第一次尝试之后的每次尝试都尝试使用相同的 location 根据规范,它不会安装该 bundle 。

关于java - OSGi:无法直接从存储库安装超过 1 个资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510246/

相关文章:

java - 试图找到二维数组的平均值时出现逻辑错误

maven - 尝试了解 OSGi 解析错误

osgi - Fabric8 或 JbossFuse 中实例的自动启动

java - 如何进行单元测试来测试我的 Spring/OSGi 配置?

java - 从操作栏中过滤 fragment 适配器

java - 覆盖或禁用 CDI 与 Bean 验证的集成

OSGI Bundle 的 start() 方法未被调用

deployment - 如何在 maven-bundle-plugin(v2.3.7) 中包含第三方库

java - 不幸的是 MyApp 已停止消息,不知道如何解决

eclipse - 如何获取 Eclipse 的主机 OSGI 控制台