Embeded OSGi(Felix)bundle start BundleException:缺少要求

时间:2016-12-06 15:59:18

标签: java eclipse maven osgi osgi-bundle

我有一个嵌入式OSGi框架(Felix)。 我正在安装基本软件包没有问题,我可以访问Felix网络控制台。

但是启动libra-commons-osgi-core-wsbundle-1.4.0.war会给我这个错误: “org.osgi.framework.BundleException:无法解析hu.libra.commnos.osgi.core.wsbundle [9](R 9.0):缺少要求[hu.libra.commnos.osgi.core.wsbundle [9](R 9.0)] osgi.wiring.package;(osgi.wiring.package = hu.libra.commnos.osgi.core.service)未解决的要求:[[hu.libra.commnos.osgi.core.wsbundle [9](R 9.0) )] osgi.wiring.package;(osgi.wiring.package = hu.libra.commnos.osgi.core.service)]“

安装并启动了hu.libra.commnos.osgi.core.service包(Felix webconsole将bundle显示为Active并显示已注册的服务)

hu.libra.commnos.osgi.core.service \ META-INF \ MANIFEST.MF包含

Export-Package:hu.libra.commons.osgi.core.service; version =“1.4.0”; uses:=“org.osgi.framework”

hu.libra.commnos.osgi.core.wsbundle \ META-INF \ MANIFEST.MF包含

Import-Package:org.osgi.framework; version =“[1.8,2)”,javax.servlet,jav  ax.servlet.http,hu.libra.commnos.osgi.core.service

有什么问题?谢谢!

我的嵌入式框架:

的.java

package hu.libra.commons.osgi.core;


import static org.osgi.framework.Constants.FRAGMENT_HOST;
import static org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN;
import static org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public class LibraOSGiCore {


    //LOGGING
    private static final Logger log = Logger.getLogger(LibraOSGiCore.class);


    //FIELDS
    private static final Framework framework;
    private static final List<Bundle> bundleList = new LinkedList<>();


    //CONSTRUCTORS
    static {
        try {
            //log4j
            PropertyConfigurator.configure("log4j.properties");

            //config
            Map<String, String> config = new HashMap<>();
            config.put(FRAMEWORK_STORAGE_CLEAN, FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
            setConfig(config);

            //framework
            FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
            framework = frameworkFactory.newFramework(config);
            framework.start();
        }
        catch (RuntimeException | BundleException e) {
            log.fatal(e, e);
            throw new RuntimeException(e);
        }
    }


    //MAIN
    public static void main(
        String[] args) {

        try {
            try {
                installBundles();
                startBundles();
            }
            catch (IOException | BundleException e) {
                log.fatal(e, e);
                throw new RuntimeException(e);
            }
            try {
                framework.waitForStop(0);
            }
            catch (InterruptedException e) {
                return;
            }
        }
        finally {
            System.exit(0);
        }
    }


    //METHODS - private
    private static void setConfig(
        Map<String, String> config) {

        //TODO use config.xml
        config.put("org.osgi.service.http.port", "8181");
        //config.put("felix.webconsole.username", "...");
        //config.put("felix.webconsole.password", "...");
    }

    private static Bundle installBundle(
        String name,
        String postfix,
        String ext)
        throws IOException,
            BundleException {

        if (postfix != null) {
            postfix = "-" + postfix;
        }
        else {
            postfix = "";
        }
        String resourceName = name + postfix + "." + ext;
        InputStream is;
        File file = new File(resourceName);
        if (file.exists()) {
            is = new FileInputStream(file);
        }
        else {
            is = LibraOSGiCore.class.getResourceAsStream(resourceName);
        }
        try(InputStream xis = is) {
            Bundle bundle = framework.getBundleContext().installBundle("file:/" + name + "." + ext, is);
            return bundle;
        }
    }

    private static void installBundles()
        throws IOException,
            BundleException {

        //Felix basic bundles
        bundleList.add(installBundle("org.apache.felix.log", "1.0.1", "jar"));
        bundleList.add(installBundle("org.apache.felix.configadmin", "1.8.12", "jar"));
        bundleList.add(installBundle("org.apache.felix.eventadmin", "1.4.8", "jar"));
        bundleList.add(installBundle("org.apache.felix.http.servlet-api", "1.1.2", "jar"));
        bundleList.add(installBundle("org.apache.felix.http.api", "3.0.0", "jar"));

        //Felix Jetty bundle
        bundleList.add(installBundle("org.apache.felix.http.jetty", "3.4.0", "jar"));

        //Felix Webconsole bundle
        bundleList.add(installBundle("org.apache.felix.webconsole", "4.2.16-all", "jar"));

        //Core bundles
        bundleList.add(installBundle("libra-commons-osgi-core-service", "1.4.0", "jar"));
        bundleList.add(installBundle("libra-commons-osgi-core-wsbundle", "1.4.0", "war"));
    }


    private static void startBundles()
        throws BundleException {

        for (Bundle bundle : bundleList) {
            if (bundle.getHeaders().get(FRAGMENT_HOST) != null)
                continue;

            bundle.start();
        }
    }
}

的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/xsd/maven-4.0.0.xsd">
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <groupId>hu.libra.commons</groupId>
    <artifactId>libra-commons-osgi-core</artifactId>
    <version>1.4.0</version>
    <name>Libra Common OSGi Core</name>
    <packaging>bundle</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
                <plugin>
                  <groupId>org.apache.felix</groupId>
                  <artifactId>maven-bundle-plugin</artifactId>
                  <extensions>true</extensions>
                  <executions>
                    <execution>
                      <id>bundle-manifest</id>
                      <phase>process-classes</phase>
                      <goals>
                        <goal>manifest</goal>
                      </goals>
                      <configuration>
                        <instructions>
                          <Bundle-SymbolicName>hu.libra.commnos.osgi.core</Bundle-SymbolicName>
                        </instructions>
                      </configuration>
                    </execution>
                  </executions>
                  <configuration>
                    <supportedProjectTypes>
                      <supportedProjectType>jar</supportedProjectType>
                      <supportedProjectType>bundle</supportedProjectType>
                    </supportedProjectTypes>
                  </configuration>
                </plugin>               
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    <dependencies>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

        <!-- OSGi -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>5.6.1</version>
        </dependency>
    </dependencies>
</project>

hu.libra.commnos.osgi.core.service 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/xsd/maven-4.0.0.xsd">
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <groupId>hu.libra.commons</groupId>
    <artifactId>libra-commons-osgi-core-service</artifactId>
    <version>1.4.0</version>
    <name>Libra Common OSGi Core Service Bundle</name>
    <packaging>bundle</packaging>

    <build>
        <plugins> 
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>       
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <supportedProjectTypes>
                        <supportedProjectType>jar</supportedProjectType>
                        <supportedProjectType>bundle</supportedProjectType>
                    </supportedProjectTypes>
                    <instructions>
                        <Bundle-SymbolicName>hu.libra.commons.osgi.core.service</Bundle-SymbolicName>
                        <Bundle-Activator>hu.libra.commons.osgi.core.service.Activator</Bundle-Activator>
                        <Export-Package>hu.libra.commons.osgi.core.service</Export-Package>
                    </instructions>                 
                </configuration>
            </plugin>           
        </plugins>      
    </build>

    <dependencies>
        <!-- OSGi -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

hu.libra.commnos.osgi.core.wsbundle 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/xsd/maven-4.0.0.xsd">
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <groupId>hu.libra.commons</groupId>
    <artifactId>libra-commons-osgi-core-wsbundle</artifactId>
    <version>1.4.0</version>
    <name>Libra Common OSGi Core Webservice Bundle</name>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
                <plugin>
                  <artifactId>maven-war-plugin</artifactId>
                  <configuration>
                    <archive>
                      <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                  </configuration>
                </plugin>
                <plugin>
                  <groupId>org.apache.felix</groupId>
                  <artifactId>maven-bundle-plugin</artifactId>
                  <extensions>true</extensions>
                  <executions>
                    <execution>
                      <id>bundle-manifest</id>
                      <phase>process-classes</phase>
                      <goals>
                        <goal>manifest</goal>
                      </goals>
                    </execution>
                  </executions>
                  <configuration>
                    <supportedProjectTypes>
                      <supportedProjectType>war</supportedProjectType>
                    </supportedProjectTypes>
                        <instructions>
                          <Bundle-SymbolicName>hu.libra.commnos.osgi.core.wsbundle</Bundle-SymbolicName>
                          <Bundle-Activator>hu.libra.commons.osgi.core.wsbundle.Activator</Bundle-Activator>
                          <Private-Package>hu.libra.commons.osgi.core.wsbundle</Private-Package>                          
                          <Import-Package>
                            org.osgi.framework,
                            javax.servlet,
                            javax.servlet.http,
                            javax.servlet.*,
                            javax.servlet.jsp.*,
                            javax.servlet.jsp.jstl.*,
                            hu.libra.commnos.osgi.core.service
                          </Import-Package>
                          <DynamicImport-Package>
                            javax.*,
                            org.xml.sax,
                            org.xml.sax.*,
                            org.w3c.*
                          </DynamicImport-Package>
                          <Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
                          <Embed-Directory>WEB-INF/lib</Embed-Directory>
                          <Web-ContextPath>/libraosgicore</Web-ContextPath>
                          <Webapp-Context>/libraosgicore</Webapp-Context>
                        </instructions>
                  </configuration>
                </plugin>           
        </plugins>
    </build>

    <dependencies>
        <!-- OSGi -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>       

        <!-- Libra OSGi Core Service -->
        <dependency>
            <groupId>hu.libra.commons</groupId>
            <artifactId>libra-commons-osgi-core-service</artifactId>
            <version>1.4.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- WS -->
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.2.10</version>
        </dependency>
    </dependencies>
</project>

0 个答案:

没有答案