0

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!

Community
  • 1
  • 1
timothyclifford
  • 6,799
  • 7
  • 57
  • 85

1 Answers1

1

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.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Neil you're all over OSGI, thank you so much. Going to try your excellent suggestions now... – timothyclifford Jul 16 '15 at 10:59
  • Finally got it to work, had to find and manually install the dependencies manually for `httpclient-osgi`, `ehcache` and `commons-lang3`. Is there any easy way to see what dependencies are required or do I just have to work through them one by one trying to start? Thank you again Neil – timothyclifford Jul 16 '15 at 11:38
  • 1
    Yes, in Bndtools we have the concept of Repositories and the Resolver. These allow you to start with a small set of core bundles, usually your own "main" bundle plus a couple of utilities such as the shell or webconsole etc. Then click the "Resolve" button and Bndtools will calculate all of the bundles that you need to include in order to get the core bundles working. This extends not just to package imports but other capabilities such as extenders, services, etc. It does of course require you to have a working repository installed such as jpm4j. – Neil Bartlett Jul 16 '15 at 12:54
  • Is Bndtools only for Eclipse? – timothyclifford Jul 16 '15 at 13:20
  • Bndtools is an Eclipse-based IDE, yes. – Neil Bartlett Jul 16 '15 at 15:36