org.osgi.framework.BundleException:包SampleModel中未解决的约束

时间:2013-08-15 19:11:29

标签: java maven osgi osgi-bundle

我正在尝试以编程方式启动OSGi框架。我正在使用Felix框架。下面是我将启动OSGi容器的代码。

public GoldeneyeApp() {

    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();

        BundleContext bundleContext = framework.getBundleContext();

        modulesNameVersionHolder.put("SampleModel", "1.0.0");

        List<Bundle> installedBundles = new LinkedList<Bundle>();

        String basePath = "C:\\ClientTool\\LocalStorage";

        for (Map.Entry<String, String> entry : modulesNameVersionHolder.entrySet()) {

            String version = entry.getValue();
            final String filename = name + Constants.DASH + version + Constants.DOTJAR;
            final String localFilename = Constants.FILE_PROTOCOL + basePath+ File.separatorChar + filename;

            installedBundles.add(bundleContext.installBundle(localFilename));
        }   

        for (Bundle bundle : installedBundles) {
            bundle.start(); // this line throws an exception as soon as I start SampleModel bundle
        }
    } catch (BundleException e) {
        e.printStackTrace();
    }
}

在上面的代码中,我试图安装并启动我创建的一个简单的OSGi模块(SampleModel jar)。一旦我尝试启动该模块,我总是得到以下异常 -

org.osgi.framework.BundleException: Unresolved constraint in bundle SampleModel [6]: Unable to resolve 6.0: missing requirement [6.0] osgi.wiring.package; (&(osgi.wiring.package=net.sf.cglib.core)(version>=2.1.3)(!(version>=3.0.0)))
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2037)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942)
    at com.host.stream.goldeneye.GoldeneyeApp.<init>(GoldeneyeApp.java:62)
    at java.lang.J9VMInternals.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1345)
    at com.host.jetstream.application.JetstreamApplication.getInstance(JetstreamApplication.java:71)
    at com.host.jetstream.application.JetstreamApplication.main(JetstreamApplication.java:94)

下面是我的SampleModel 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.sample.model</groupId>
    <artifactId>SampleModel</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.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.0</version><!--$NO-MVN-MAN-VER$ -->
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
            <version>4.3.0</version><!--$NO-MVN-MAN-VER$ -->
            <type>jar</type>
            <scope>compile</scope>
        </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>
                    <manifestLocation>src/main/resources/META-INF</manifestLocation>
                    <instructions>
                        <Bundle-SymbolicName>SampleModel</Bundle-SymbolicName>
                        <Bundle-Activator>com.host.sample.model.samplemodel.activator.Activator</Bundle-Activator>
                        <Import-Package>*,
                            org.springframework.beans.factory;version="[3.0.5.RELEASE,4.0.0)",
                            org.springframework.beans.factory.config;version="[3.0.5.RELEASE,4.0.0)",
                            net.sf.cglib.core;version="[2.1.3,3.0.0)",
                            net.sf.cglib.proxy;version="[2.1.3,3.0.0)",
                            net.sf.cglib.reflect;version="[2.1.3,3.0.0)"
                        </Import-Package>
                    </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>

1 个答案:

答案 0 :(得分:3)

您在清单中使用'Import-Package:net.sf ...'但尚未将CGLIB(或Spring Framework)安装到OSGI运行时。如果Import-Package无法解析所有必需的包,则捆绑包将无法启动。

编辑:我忘了我有这个:https://gist.github.com/sheenobu/5935468。如果你让pax-url工作,你可以从任意网址开始安装。

EDIT2:也清除你的框架缓存,否则你最终会多次安装包。或者检查包是否已作为捆绑包安装,如果是,请不要安装它。

相关问题