ClassCastException with OSGI bundle - osgi

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]

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

Related

felix maven-bundle-plugin error (&(osgi.wiring.package=com.a.b.c)(version>=xx.0.0)(!(version>=yy.0.0)

i use Apache Felix to implement osgi bundle and use it as embedded Felix framework to call boundle
here is my maven plugin to build MANIFEST.MF :
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.5.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Bundle-Activator>a.b.c.osgi.activator.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
i build project and then use jar file in embeded felix like this
BundleContext bundleContext = f.getBundleContext();
Bundle bundle = bundleContext.installBundle(
"file:/home/eclipse_workSpace/my-module/target/abc-1.1.0.jar");)
String bName = bundle.getLocation();
bundle.getRegisteredServices();
bundle.getState();
/* Bundle[] bls = bundleContext.getBundles(); */
System.out.println("starting bundle " + bName);
bundle.start();
when i start boundle i got this exception
Exception in thread "main" org.osgi.framework.BundleException: Unable to resolve a.b.c [1](R 1.0): missing requirement [a.b.c [1](R 1.0)] osgi.wiring.package; (&(osgi.wiring.package=com.google.common.base)(version>=21.0.0)(!(version>=22.0.0))) Unresolved requirements: [[a.b.c [1](R 1.0)] osgi.wiring.package; (&(osgi.wiring.package=com.google.common.base)(version>=21.0.0)(!(version>=22.0.0)))]
what should i do to solve this problem?
This error message means that your bundle depends on Google Guava, version 21. Specifically this line:
missing requirement [a.b.c [1](R 1.0)] osgi.wiring.package; (&(osgi.wiring.package=com.google.common.base)(version>=21.0.0)(!(version>=22.0.0)))
... means that your bundle "a.b.c" imports the package com.google.common.base with version greater-than-or-equal to 21 and not-greater-than-or-equal to 22. Since your bundle imports this package, there must be another bundle in your OSGi Framework that exports the package.
The solution is to ensure that Guava 21 is installed into your OSGi Framework.

What Build Module in IntelliJ actually does for maven module

When I run
mvn clean install
for my maven module then it compiles fine. No issues.
But when I open my pom.xml file in IntelliJ and I choose to Build -> Build module then I get following issues:
Information:javac 1.8.0_144 was used to compile java sources
Information:Module "mymodule" was fully rebuilt due to project configuration/dependencies changes
Information:09.10.2017 21:16 - Compilation completed with 3 errors and 3 warnings in 23s 991ms
C:\somepath\mymodule\pom.xml
Error:Error:osgi: [mymodule] Exception: java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin not found, parent: java.net.URLClassLoader#29453f44 urls:[] exception:java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin
Error:Error:osgi: [mymodule] Failed to load plugin org.apache.sling.bnd.models.ModelsScannerPlugin;generatePackagesHeader=true, error: java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin not found, parent: java.net.URLClassLoader#29453f44 urls:[] exception:java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin
Error:Error:osgi: [mymodule] Cannot load the plugin org.apache.sling.bnd.models.ModelsScannerPlugin
This is a module with AEM code and it uses maven-sling-plugin. It works fine for other developers in the project. Because it's working when executed directly from maven I'm trying to understand what IntelliJ does in the background. But actually, my problem is those compilation issues.
From what I've found IntelliJ does not call maven when Build is done. Any ideas how can I find differences between running from IntelliJ and directly from Maven?
What happens here is that the ModelScanner plugin can't be found using the current ClassLoader. The reason for this can be that you are using IntelliJ IDEA Ultimate which comes with a OSGI plugin already pre-installed called 'Osmorc'. If this OSGI plugin is active it will determine the classloader to be used for building OSGI related projects.
So simply de-activating this Osmorc plugin in IntelliJ should allow your build to revert to the classloader from the ModelScannerPlugin mentioned in the configuration of your the maven-bundle-plugin in your projects POM.xml file which should solve the problem.
If this still results in a similar Maven build error, then make sure to add a Maven dependency 'org.apache.sling.bnd.model' to your maven-bundle-plugin in your POM.xml file.
<!-- Apache Felix Bundle Plugin -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<executions>
<!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
<execution>
<id>scr-metadata</id>
<goals>
<goal>manifest</goal>
</goals>
<configuration>
<supportIncrementalBuild>true</supportIncrementalBuild>
</configuration>
</execution>
</executions>
<configuration>
<exportScr>true</exportScr>
<instructions>
<!-- Enable processing of OSGI DS component annotations -->
<_dsannotations>*</_dsannotations>
<!-- Enable processing of OSGI metatype annotations -->
<_metatypeannotations>*</_metatypeannotations>
<_plugin>org.apache.sling.bnd.models.ModelsScannerPlugin;generatePackagesHeader=true</_plugin>
</instructions>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.bnd.models</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
could you please check your core pom file. it should contain a plugin section like this:
<plugin> <!-- Enable registration of Sling Models classes via bnd plugin --> org.apache.sling.bnd.models.ModelsScannerPlugin, <!-- Allow the processing of SCR annotations via a bnd plugin --> org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin;destdir=${project.build.outputDirectory} </plugin>
but if you created a project using aem archetype the tag looks like' <_plugin>

Unresolved constraint in bundle

When I try to deploy my bundle to OSGi via maven, this shows in installed state. My Project is multimodule, so having two bundles. What I saw in taglib bundle is :
org.apache.felix.shell from org.apache.felix.shell (292)
org.apache.jackrabbit.api,version=[2.3,3) from org.apache.jackrabbit.jackrabbit-api (65)
org.apache.jackrabbit.api.jsr283.security -- Cannot be resolved
org.apache.jackrabbit.api.security,version=[2.3,3) from org.apache.jackrabbit.jackrabbit-api (65)
org.apache.jackrabbit.api.security.principal,version=[2.3,3) from org.apache.jackrabbit.jackrabbit-api (65)
org.apache.jackrabbit.api.security.user,version=[2.3,3) from org.apache.jackrabbit.jackrabbit-api (65)
After checking the logs, I see below:
14.04.2017 17:23:03.330 *ERROR* [FelixDispatchQueue] com.abc.MyPrj-services FrameworkEvent ERROR (org.osgi.framework.BundleException: Unresolved constraint in bundle com.abc.MyPrj-services [290]: Unable to resolve 290.0: missing requirement [290.0] osgi.wiring.package; (osgi.wiring.package=org.apache.jackrabbit.api.jsr283.security)) org.osgi.framework.BundleException: Unresolved constraint in bundle com.abc.MyPrj-services [290]: Unable to resolve 290.0: missing requirement [290.0] osgi.wiring.package; (osgi.wiring.package=org.apache.jackrabbit.api.jsr283.security)
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3896)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1938)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1261)
at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:298)
at java.lang.Thread.run(Thread.java:745)
I have added below dependency in both modules taglib, services.
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-api</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
Below is the pom of taglib module:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
com.mypkg.gwms.ra.tags.*;version=${project.version}
</Export-Package>
<Import-Package>
com.adobe.granite.*,
com.day.*,
javax.*,
org.apache.commons.*,
org.apache.sling.*,
org.apache.tika.*,
org.osgi.*,
org.w3c.dom.*,
org.xml.sax.*,
org.slf4j.*,
org.apache.felix.shell.*,
org.apache.jackrabbit.*,
</Import-Package>
<Include-Resource>
META-INF/${project.artifactId}-${project.version}.tld=${project.build.outputDirectory}/META-INF/${project.artifactId}-${project.version}.tld,
{maven-resources},
{maven-dependencies}
</Include-Resource>
<Sling-Bundle-Resources>
/META-INF/tags
</Sling-Bundle-Resources>
</instructions>
</configuration>
</plugin>
Thanks,
I was required to have 2 dependencies of the same API but with the different version. The latest one was using in services module. Other module taglib, was looking for the older version of the same dependency to resolve org.apache.jackrabbit.api.jsr283.security
To solve this, I added below statement in my pom.xml .
<Import-Package>
org.apache.jackrabbit.*;version=1.6.1
</Import-Package>
Only import 1.6.1 version. That's worked.
The error tells you that you also need to install a bundle that provides the package org.apache.jackrabbit.api.jsr283.security.
You can check in the Manifest of a bundle if it exports this package.
You should install a bundle which contains org.apache.jackrabbit.api.jsr283.security package and exports it, to your osgi container.

OSGi bundle doesn't start after inserting a Bundle Activator

I'm implementing a maven web project in Netbeans (version 8.0.2) with some build profiles (jetty, OSGi..) managed by POM file using plugins and custom goals of project in Netbeans.
I need to manage the lifecycle of the generated OSGi bundle, so I created an activator with the following class:
package org.activiti.explorer.conf;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class OSGiActivator implements BundleActivator {
#Override
public void start(BundleContext context) throws Exception {
System.out.println("BUNDLE STARTED");
}
#Override
public void stop(BundleContext context) throws Exception {
System.out.println("BUNDLE STOPPED");
}
}
After that I updated the maven bundle <plugin> element in the POM file adding the informaton about the activator, so the plugin is:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-Activator>org.activiti.explorer.conf.OSGiActivator</Bundle-Activator>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
<Embed-Dependency>*</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>WEB-INF/lib</Embed-Directory>
<Web-ContextPath>activiti-explorer</Web-ContextPath>
<Import-Package> org.eclipse.jetty.servlet.listener,
org.apache.jasper.servlet,
javax.naming,
javax.el;resolution:=optional,
com.sun.el;resolution:=optional,
org.apache.el;resolution:=optional,
javax.servlet.jsp;resolution:=optional,
org.apache.jasper.runtime;resolution:=optional,
org.apache.tomcat;resolution:=optional,
javax.servlet;version="[2.6.0,4.0.1]",
javax.servlet.http;version="[2.6.0,4.0.1]",
org.apache.log4j,
javax.inject,
org.slf4j.impl,
org.slf4j,
org.slf4j.spi,
org.apache.commons.io;version="[1.4,2)",
org.apache.commons.io.output;version="[1.4,2)",
org.apache.commons.lang3;version="[3.1,4)",
org.apache.commons.lang3.builder;version="[3.1,4)",
org.apache.commons.lang3.exception;version="[3.1,4)"
</Import-Package>
<Export-Package>!*</Export-Package>
</instructions>
<manifestLocation>${basedir}/target/META-INF</manifestLocation>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
Now, when I build the bundle and load it on OSGi with Felix, the bundle is activated but it doesn't start, whereas if I delete the row about the Bundle-Activator from POM file it works properly.
Note that from Felix console no error is displayed about Build classpath, imported packages and exported packages and if I try to start bundle from osgi line command, this exception is logged:
gogo: BundleException: Error starting module.
Can someone help me? If I don't use the class which implements the Activator and I don't refer it in the POM file, the bundle created runs properly in OSGi.
You are listing all the Import-Package statements explicitly. This means that the maven-bundle-plugin does not add auto detected package usages. You are missing the Import-Package for org.osgi.framework. So the bundle resolves but the package is not wired and when loading the OSGiActivator class it fails.
So the simple solution is to add the package import.
I rather recommend though to not define all the imports and let the maven bundle plugin figure them out automatically. You can do this by adding a ",*". You should then remove all the packages it figures out by itself.

service provider and OSGI issue

I have written an osgi bundle, where i have dependency for a jar(bsf-all.jar), which contains service provider as follows (under META_inf/services/);
bsh.engine.BshScriptEngineFactory
com.sun.script.freemarker.FreeMarkerScriptEngineFactory
com.sun.script.groovy.GroovyScriptEngineFactory
com.sun.script.jacl.JaclScriptEngineFactory
com.sun.script.jaskell.JaskellScriptEngineFactory
com.sun.script.java.JavaScriptEngineFactory
com.sun.phobos.script.javascript.RhinoScriptEngineFactory
com.sun.phobos.script.javascript.EmbeddedRhinoScriptEngineFactory
com.sun.script.jawk.JawkScriptEngineFactory
com.sun.script.jelly.JellyScriptEngineFactory
com.sun.script.jep.JepScriptEngineFactory
com.sun.script.jexl.JexlScriptEngineFactory
com.sun.script.jruby.JRubyScriptEngineFactory
com.sun.script.judo.JudoScriptEngineFactory
com.sun.script.juel.JuelScriptEngineFactory
com.sun.script.jython.JythonScriptEngineFactory
com.sun.script.ognl.OgnlScriptEngineFactory
org.pnuts.scriptapi.PnutsScriptEngineFactory
com.sun.script.scheme.SchemeScriptEngineFactory
com.sun.script.velocity.VelocityScriptEngineFactory
com.sun.script.xpath.XPathScriptEngineFactory
com.sun.script.xslt.XSLTScriptEngineFactory
When i check my bundle state via OSGI console , it is Active and there is no any dependency issue..
But when i try to use it(means after the server up and running)
server throws "Class not found" issue ;
java.lang.ClassNotFoundException: com.sun.phobos.script.javascript.RhinoScriptEngineFactory
This particular class is in my dependency jar(bsf-all.jar) and that class is exposed via the service provider..
I suspect there is a class loading issue with OSGi and java service provider..
My pom.xml is as follows;
<dependencies>
<dependency>
<groupId>org.apache.bsf</groupId>
<artifactId>bsf-all</artifactId>
<version>${bsf.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<version>1.6R7</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Export-Package>
org.apache.bsf.*,
org.mozilla.javascript.*,
org.pnuts.scriptapi.*,
com.sun.script.*,
com.sun.phobos.script.*,
bsh.engine.*,
javax.script.*,
</Export-Package>
<Import-Package>
com.sun.*
</Import-Package>
<DynamicImport-Package>*</DynamicImport-Package>
<Embed-Dependency>js;scope=compile|runtime;inline=false;</Embed-Dependency>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
I'm using Equinox OSGi implementation..How can i edit my pom to overcome above issue?
I feel that there are too many fundamental errors in this POM to begin speculating about the cause of the CNFE. Your <Export-Package> statement is of most concern. Why are you repackaging and exporting the whole of BSF, Rhino, Phobos and even parts of the JDK inside your bundle??
As I implied in my answer to your earlier question: you are getting bogged down in low-level details without, I think, having a good understanding of what you are trying to achieve at the high level. Therefore even if somebody posts an answer that gets you past this particular issue, you will still not have a working architecture.
Please step back and describe at a high level what you are trying to achieve and why. Then we can offer a solution based on good OSGi practices.

Resources