0

我已经创建了基于 Maven 的项目,在该项目中我试图通过使用下面的代码来启动一个 OSGi 容器(Felix),但每次,它都会给我以下异常 -

Exception in thread "main" java.util.NoSuchElementException
    at java.util.ServiceLoader$ServiceIterator.next(ServiceLoader.java:187)
    at com.ebay.personalization.osgi.test.osgitest.App.main(App.java:28)

下面是我的代码:

import org.apache.commons.io.FileUtils;
import org.apache.felix.framework.FrameworkFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;

public class App {
    public static void main(String[] args) throws BundleException, IOException {

        FileUtils.deleteDirectory(new File("felix-cache"));
        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();

        Framework framework = frameworkFactory.newFramework(new HashMap<String, String>());
        framework.start();

        BundleContext context = framework.getBundleContext();
        List<Bundle> installedBundles = new LinkedList<Bundle>();

        installedBundles.add(context.installBundle("C:\\Store\\TestingModel-1.0.0.jar"));

        for (Bundle bundle : installedBundles) {
            bundle.start();
        }

        ServiceReference reference = context.getServiceReference(ITestService.class.getName());
        ITestService service = (ITestService) context.getService(reference);
        service.speak();
    }
}

一旦我运行上面的代码,我总是得到上面提到的异常-

下面是我的 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>
        <groupId>com.host.domain</groupId>
        <artifactId>DomainParent</artifactId>
        <version>1.6.1-RELEASE</version>
    </parent>

    <!-- POM Information about the Project -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.host.personalization.osgi.test</groupId>
    <artifactId>OsgiTest</artifactId>
    <version>1.0.0</version>

    <!-- Packing Type is bundle for OSGI Library Bundle -->
    <packaging>bundle</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.cglib</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.url</groupId>
            <artifactId>pax-url-mvn</artifactId>
            <version>1.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>

    <!-- Build Configration -->
    <build>
        <plugins>
            <!-- Apache Felix Bundle Plugin - For Generation of Manifest after Compile 
                phase -->
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <!-- Configuration for generating the Manifest.mf -->
                <configuration>
                    <manifestLocation>src/main/resources/META-INF</manifestLocation>
                    <!-- Manifest Headers which need to customized during manifest generation -->
                    <instructions>
                        <Bundle-SymbolicName>OsgiTest</Bundle-SymbolicName>
                    <instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- Configuration of repositories for dependency resolution -->
    <repositories>
        <!-- domain Bundles Repository -->
        <!-- This is needed to locate the domain Parent project. Other repositories 
            come from the parent. -->
        <repository>
            <id>releases</id>
            <url>http://nxdomain/content/repositories/releases/</url>
            <releases>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>thirdparty</id>
            <url>http://nxdomain/content/repositories/thirdparty/</url>
            <releases>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

谁能告诉我我在这里做错了什么?我知道 ServiceLoader 在类路径中搜索 FrameworkFactory 接口,如果找不到,则会抛出异常。但就我而言,我已经依赖于 felix 框架,所以它应该在我的类路径中。对吗?那我做错了什么?

4

0 回答 0