arsenal
arsenal

Reputation: 24184

org.osgi.framework.BundleException: Unresolved constraint in bundle SampleModel

I am trying to launch an OSGi framework programatically. I am using Felix framework for this. Below is the code I have which will launch an OSGi container.

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

In the above code, I am trying to install and start a simple OSGi module (SampleModel jar) that I have created. As soon as I try to start that module, I always get the below exception-

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)

Below is my SampleModel pom.xml file-

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

Upvotes: 0

Views: 10506

Answers (1)

Sheena Artrip
Sheena Artrip

Reputation: 2010

You are using 'Import-Package: net.sf...' in your manifest but you haven't installed CGLIB(or Spring Framework) into the OSGI runtime yet. If Import-Package doesn't resolve every required package, your bundle won't start.

EDIT: I forgot I had this: https://gist.github.com/sheenobu/5935468 . If you get pax-url working, you can then start installing from arbitrary urls.

EDIT2: also clear your frameworks cache otherwise you end up installing packages multiple times. Or check if the package is already installed as a bundle and, if so, don't install it.

Upvotes: 3

Related Questions