java.util.NoSuchElementException while starting an OSGi framework programatically

眉间皱痕 提交于 2019-12-17 21:32:20

问题


I am trying to programatically start an OSGi framework (preferably Equinox) from Java and then load up any bundles.

So I was going through this article which says how to do that. But I am getting java.util.NoSuchElementException everytime at the below line everytime

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

Below is the code-

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

        }
    }

I am not sure what wrong I am doing here? I am having a maven based project. Below is my pom.xml file for this project-

<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>

回答1:


Service Loader searches the classpath for the interface you have asked for -- in this case FrameworkFactory. To fix it you need to put an OSGi Framework implementation on the classpath.

For example if you choose Felix, make sure that org.apache.felix.framework-<version>.jar is present on the classpath when you launch your application.




回答2:


Make sure that your import for FrameworkFactory.class is on org.osgi.framework.launch.FrameworkFactory (not org.apache.felix.framework.FrameworkFactory). I ran into this same error (NoSuchElementException), when I broke up the statements the ServiceLoader.load() call was throwing a ClassNotFoundexception. Not sure why the load exception was eclipsed by the next call - but I reverted and confirmed this was happening. Good Luck!



来源:https://stackoverflow.com/questions/18244334/java-util-nosuchelementexception-while-starting-an-osgi-framework-programaticall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!