0
votes

Following on from OSGI bundle dependencies

I have reverted maven-bundle-plugin back to using the defaults. Here is my current pom:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.felix.test</groupId>
    <artifactId>com.felix.test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
            <version>1.9.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient-osgi</artifactId>
            <version>4.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <version>2.5.4</version>
                        <type>maven-plugin</type>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.4</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Everything bundles and installs OK. When I try to start the bundle I'm told I'm missing net.sf.ehcache which I install. Then I'm missing slf4j.api which I install. Then I'm missing slf4j.impl and I have tried installing pretty much every slf4j.impl possibility from https://jpm4j.org/#!/ but most (slf4j-simple-1.7.12.jar, slf4j-log4j12-1.7.12.jar) report back:

org.osgi.framework.BundleException: Fragment bundles can not be started.

This is my current error from GoGo:

org.osgi.framework.BundleException: Unable to resolve com.felix.test [16](R 16.0): missing requirement [com.felix.test [16](R 16.0)] osgi.wiring.package; (&(osgi.wiring.package=net.sf.ehcache)(version>=2.10.0)(!(version>=3.0.0))) [caused by: Unable to resolve net.sf.ehcache [17](R 17.0): missing requirement [net.sf.ehcache [17](R 17.0)] osgi.wiring.package; (&(osgi.wiring.package=org.slf4j)(version>=1.7.0)(!(version>=2.0.0))) [caused by: Unable to resolve slf4j.api [23](R 23.0): missing requirement [slf4j.api [23](R 23.0)] osgi.wiring.package; (&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]] Unresolved requirements: [[com.felix.test [16](R 16.0)] osgi.wiring.package; (&(osgi.wiring.package=net.sf.ehcache)(version>=2.10.0)(!(version>=3.0.0)))]

Hopefully I'm getting closer...

Thank you!

1

1 Answers

1
votes

You have two errors to address. The first is "fragment bundles cannot be started". The error message tells you everything you need to know. The slf4j implementation bundles are fragments, and you cannot start fragments. So just don't start them!

You haven't specified how you are running your OSGi Framework, but somewhere you must have some code that iterates over all the installed bundles and calls the start() method on each. You need to modify your code to not call start() on bundles that are fragments. You can tell if any bundle is a fragment as follows:

(bundle.adapt(BundleRevision.class).getTypes() | BundleRevision.TYPE_FRAGMENT) > 0;

OR:

bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;

The second error says that the bundle named com.felix.test has a dependency on package net.sf.ehcache. I have never heard of com.felix.test... is this the bundle you are building? Do you actually use Ehcache in your code? If so, you obviously need to install the Ehcache bundle. If you do have Ehcache installed, then it might be the wrong version; your bundle requires version 2.10.0 up to but excluding 3.0.0.