以编程方式启动OSGi框架时的java.util.NoSuchElementException

时间:2013-08-15 00:08:32

标签: java osgi equinox osgi-bundle

我正在尝试以编程方式从Java启动OSGi框架(最好是Equinox),然后加载任何捆绑包。

所以我正在通过这个article说明如何做到这一点。但我每次都会在下面的每一行得到 java.util.NoSuchElementException

ServiceLoader.load(FrameworkFactory.class).iterator().next();

以下是代码 -

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.logging.Logger;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public App() {      

    try {

        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
        Map<String, String> config = new HashMap<String, String>();
        //TODO: add some config properties
        Framework framework = frameworkFactory.newFramework(config);
        framework.start();

            //...  some other code

        }
    }

我不确定我在这里做错了什么?我正在进行基于maven的项目。下面是我这个项目的pom.xml文件 -

<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/maven-v4_0_0.xsd">

    <parent>
        <groupId>com.host.Stream</groupId>
        <artifactId>Stream-parent</artifactId>
        <version>2.0.0-SNAPSHOT</version>
        <relativePath>../Build/superpom</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.host.domain.eye</groupId>
    <artifactId>eye</artifactId>
    <packaging>jar</packaging>
    <name>eye</name>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>                         <!-- Configuration of the archiver -->
                    <archive>                       <!-- Manifest specific configuration -->
                        <manifest>                  <!-- Classpath is added to the manifest of the created jar file. -->
                            <addClasspath>true</addClasspath>   <!-- Configures the classpath prefix. This configuration option is used to 
                                specify that all needed libraries are found under lib/ directory. -->
                            <classpathPrefix>lib/</classpathPrefix> <!-- Specifies the main class of the application -->
                            <mainClass>com.host.personalization.eye.App</mainClass>
                        </manifest>
                    </archive>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.class</include>
                    </includes>
                    <finalName>${project.artifactId}</finalName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outputDirectory>target/config/StreamConf</outputDirectory>
                    <resources>
                        <resource>
                            <directory>buildsrc/StreamConf</directory>
                            <includes>
                                <include>**</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>maven-replacer-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>target/config/StreamConf/eyewiring.xml</file>
                    <regex>false</regex>
                    <replacements>
                        <replacement>
                            <token>dynamic_build_label_place_holder</token>
                            <value>${project.artifactId}-${project.version}-${buildNumber}</value>
                        </replacement>

                    </replacements>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <id>assembly</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>generate-resources</id>
                        <goals>
                            <goal>manifest</goal>
                        </goals>

                        <configuration>
                        <manifestLocation>buildsrc/StreamConf/META-INF</manifestLocation>
                            <instructions>
                                <Bundle-Name>${project.name}</Bundle-Name>
                                <Bundle-SymbolicName>com.host.domain.eye</Bundle-SymbolicName>
                                <Import-Package>*,
                                    pics,
                                    links,
                                    javax.el;version="[2.0,3.0)";resolution:=optional,
                                    javax.inject;version="[1.0.0,2.0.0)",
                                    javax.servlet;version="[2.5,4.0)",
                                    javax.servlet.annotation;version="[3.0.0,4.0.0)",
                                    javax.servlet.http;version="[2.5,4.0)",
                                    javax.servlet.jsp;version="[2.0,3.0)",
                                    javax.servlet.jsp.el;version="[2.0,3.0)",
                                    org.aopalliance.aop;version="[1.0.0,2.0.0)",
                                    org.eclipse.virgo.web.dm;version="[2.1.0.RELEASE,3.0)",
                                    org.springframework.aop;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.aop.framework;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.aop.scope;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans;version="3.0.5.RELEASE",
                                    org.springframework.beans.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans.factory;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans.factory.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans.factory.config;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.context;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.context.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.context.config;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.stereotype;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.util;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.bind.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.context;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.context.request;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.context.support;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.servlet;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.servlet.config;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.servlet.view;version="[3.0.5.RELEASE,4.0)",</Import-Package>
                            </instructions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <dependency>
            <groupId>com.host.Stream</groupId>
            <artifactId>Streamframework</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.host.external</groupId>
            <artifactId>ucirrus-db</artifactId>
            <version>0.7.3</version>
        </dependency>

        <dependency>
            <groupId>com.host.Stream</groupId>
            <artifactId>Streamcore</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-extender</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-io</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>
</project>

2 个答案:

答案 0 :(得分:2)

Service Loader在类路径中搜索您要求的接口 - 在本例中为FrameworkFactory。要修复它,您需要在类路径上放置一个OSGi Framework实现。

例如,如果选择Felix,请确保在启动应用程序时类路径中存在org.apache.felix.framework-<version>.jar

答案 1 :(得分:1)

确保您对FrameworkFactory.class的导入位于org.osgi.framework.launch.FrameworkFactory(不是org.apache.felix.framework.FrameworkFactory)上。当我分解ServiceLoader.load()调用抛出ClassNotFoundexception的语句时,我遇到了同样的错误(NoSuchElementException)。不知道为什么下次调用会导致加载异常黯然失色 - 但我还原并确认这种情况正在发生。祝你好运!