使用Apache Felix Gogo启动最小示例捆绑包时,为什么会出现BundleException?

时间:2019-05-06 17:24:21

标签: java osgi apache-felix osgi-bundle

我正在尝试从下而上学习OSGI。首先制作并运行minimal example bundle

我使用apache felix网站上的maven和先前链接的示例建立了一个简单的项目。这个最小的项目似乎可以很好地编译,并且在检查生成的.jar文件的内容时看不到任何问题。

然后我使用felix:install <mybundle.jar>将生成的捆绑.jar文件安装在apache felix的gogo osgi-terminal工具中。之后,我尝试使用felix:start <mybundle.jar>启动它。

这是我的问题发生的时间。我得到了org.osgi.framework.BundleException的堆栈跟踪。

所以这是我要解决的问题。我没有发现明显的问题或解决方案,可能会帮助或解释为什么我遇到此问题。

我还使用apache felix框架版本6.0.3来运行我的捆绑软件。

所以,我的问题是:为什么会遇到这些问题,我应该怎么做才能解决这些问题,以便我可以重新开始学习OSGI?

考虑到所有技术问题,我一直在寻找伟大的互联网的无限智慧,但是找不到在相同情况下遇到相同问题的人。

有关更多信息,我使用的是Java 12和IntelliJ Idea Community Edition内置的Maven。使用Java 8进行相同的设置时,我遇到了同样的问题,并已升级到12,只是因为这样做比早晚感到明智。

OSGI-Activator类是迄今为止该项目中唯一的代码,如下所示:

package com.github.hannesknutsson.privatepkg;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;

public class Activator implements BundleActivator, ServiceListener {

    private BundleContext context;

    public void start(BundleContext bundleContext) throws Exception {
        context = bundleContext;

        System.out.println("Starting to listen for service events.");
        context.addServiceListener(this);
    }

    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("Stopped listening for service events.");
    }

    public void serviceChanged(ServiceEvent serviceEvent) {
        String[] objectClass = (String[])
                serviceEvent.getServiceReference().getProperty("objectClass");

        if (serviceEvent.getType() == ServiceEvent.REGISTERED)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " registered.");
        }
        else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " unregistered.");
        }
        else if (serviceEvent.getType() == ServiceEvent.MODIFIED)
        {
            System.out.println(
                    "Ex1: Service of type " + objectClass[0] + " modified.");
        }
    }
}

用于编译结果并将其打包到捆绑的.jar文件中的Maven pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>testproject</artifactId>
        <groupId>com.github.hannesknutsson</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <packaging>bundle</packaging>
    <artifactId>TestBundle</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
                <version>3.1.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>${project.groupId}.services</Export-Package>
                        <Private-Package>${project.groupId}.privatepkg</Private-Package>
                        <Bundle-Activator>${project.groupId}.privatepkg.Activator</Bundle-Activator>
                    </instructions>
                </configuration>
                <version>4.2.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>12</source>
                    <target>12</target>
                </configuration>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </build>

</project>

由此产生的堆栈跟踪如下:

org.osgi.framework.BundleException: Unable to cache bundle: TestBundle-1.0-SNAPSHOT.jar
        at org.apache.felix.framework.Felix.installBundle(Felix.java:3231)
        at org.apache.felix.framework.BundleContextImpl.installBundle(BundleContextImpl.java:147)
        at org.apache.felix.framework.BundleContextImpl.installBundle(BundleContextImpl.java:120)
        at org.apache.felix.gogo.command.Basic.start(Basic.java:734)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:139)
        at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:91)
        at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:599)
        at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:526)
        at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:415)
        at org.apache.felix.gogo.runtime.Pipe.doCall(Pipe.java:416)
        at org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:229)
        at org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:59)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.net.MalformedURLException: no protocol: TestBundle-1.0-SNAPSHOT.jar
        at java.base/java.net.URL.<init>(URL.java:650)
        at org.apache.felix.framework.util.SecureAction.createURL(SecureAction.java:256)
        at org.apache.felix.framework.cache.JarRevision.initialize(JarRevision.java:148)
        at org.apache.felix.framework.cache.JarRevision.<init>(JarRevision.java:76)
        at org.apache.felix.framework.cache.BundleArchive.createRevisionFromLocation(BundleArchive.java:799)
        at org.apache.felix.framework.cache.BundleArchive.reviseInternal(BundleArchive.java:480)
        at org.apache.felix.framework.cache.BundleArchive.<init>(BundleArchive.java:148)
        at org.apache.felix.framework.cache.BundleCache.create(BundleCache.java:462)
        at org.apache.felix.framework.Felix.installBundle(Felix.java:3227)
        ... 19 more
java.net.MalformedURLException: no protocol: TestBundle-1.0-SNAPSHOT.jar

1 个答案:

答案 0 :(得分:1)

Felix在安装捆绑软件时需要一个URI。尝试使用完整文件:URL。

或者,您可以下载felix发行版,然后将捆绑软件放入捆绑软件目录中。

通常请注意,felix教程有些过时,并且使用了一些您在生产中永远不会使用的非常低级的方法。

请参阅以下问题的答案:Where to find and install org.osgi.framework package?