PRADEEP REDDY KANDI
PRADEEP REDDY KANDI

Reputation: 87

ClassCastException with OSGI bundle

I am working on OSGi bundle, which uses javax.ws.rs-api (2.0.1). Karaf is already having jsr311-api (1.1.1) loaded as bundle. When I try to load my OSGi bundle, I see the following exception. Is there a way we can ignore the previously loaded bundle?

The activate method has thrown an exception
java.lang.LinkageError: ClassCastException: attempting to castbundle://137.0:1/javax/ws/rs/ext/RuntimeDelegate.class to bundle://177.0:1/javax/ws/rs/ext/RuntimeDelegate.class
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:146)[137:javax.ws.rs.jsr311-api:1.1.1]
    at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)[137:javax.ws.rs.jsr311-api:1.1.1]
    at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:95)[137:javax.ws.rs.jsr311-api:1.1.1]
    at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)[137:javax.ws.rs.jsr311-api:1.1.1]

Upvotes: 2

Views: 814

Answers (1)

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

Your bundle must import only the packages you need versions. You have to create META-INF\MANIFEST.MF with Import-Package header, which will contain the list of packages required only versions.

Import-Package: javax.ws.rs.ext,version="2.0.1"

List all the packages that cause the conflict. I think here they are:

javax.ws.rs,version="2.0.1"
javax.ws.rs.client,version="2.0.1"
javax.ws.rs.container,version="2.0.1"
javax.ws.rs.core,version="2.0.1"
javax.ws.rs.ext,version="2.0.1"

You can specify a range of versions : [2.0.1, 3) and so on.

Real example:

Import-Package: org.osgi.service.blueprint; version="[1.0.0, 2.0.0)"

You can use maven-bundle-plugin to create requered MANIFEST.MF:

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.3.7</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Description>${project.description}</Bundle-Description>
                    <Import-Package>
                        javax.ws.rs;version=2.0.1,
                        javax.ws.rs.client;version=2.0.1,
                        javax.ws.rs.container;version=2.0.1,
                        javax.ws.rs.core;version=2.0.1,
                        javax.ws.rs.ext;version=2.0.1,
                        *,
                        org.apache.camel.osgi
                    </Import-Package>
                    <Export-Package>
                        your.package
                    </Export-Package>
                </instructions>
            </configuration>
        </plugin>

Don't forget to install bundle version 2.0.1

Upvotes: 1

Related Questions