OSGi bundle with javax.json.JsonException for MoXy - java

What OSGi bundle should I install for javax.json package for RS services?
I've got an error:
Caused by: java.lang.NoClassDefFoundError: javax/json/JsonException
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getNewXMLReader(SAXUnmarshaller.java:209)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getXMLReader(SAXUnmarshaller.java:197)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:452)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:400)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:741)
at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:649)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:349)
at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.readFrom(MOXyJsonProvider.java:668)
The JSON bundle I've installed:
JSR 353 (JSON Processing) Default Providerorg.glassfish.javax.json
Symbolic Name org.glassfish.javax.json
Version 1.0.4
Bundle Location initial#reference:file:plugins/org.glassfish.javax.json_1.0.4.jar
The MOXy bundle:
EclipseLink MOXyorg.eclipse.persistence.moxy
Symbolic Name org.eclipse.persistence.moxy
Version 2.6.4.v20160829-44060b6
Bundle Location initial#reference:file:plugins/org.eclipse.persistence.moxy_2.6.4.v20160829-44060b6.jar
with import:
javax.json; resolution:=optional
It looks like everything should work fine, but it isn't. MoXy don't get the javax.json dependencies. What's wrong? Wrong version of bundle? Wrong loading order? What do I need to do to bring them to work?

It seems to be a general problem with how Equinox is resolving optional package imports.
The MoXy bundle was loaded before the JSon bundle, so the javax.json package was not available then. Because the reference was optional, it was 'resolved' as 'not provided', and Equinox has not re-resolved them after importing the json bundle.
The solution:
1) Influence the install order.
or
2) Set start level for Moxy to higher value then JSon.

Related

Is it required to import transitive dependencies as bundles to KURA

I am very new to OSGI and KURA. I am tackling with a problem since yesterday and I did not understand its reason.
Please, tell me if my way is wrong.
I am using dropbox-core-sdk (version 3.0.0) in my project. I have downloaded its jar and also, I have researched that it has a dependency on jackson-core (version 2.7.4). I have also downloaded its jar and I have created a bundle with dropbox-core-sdk.jar and jackson-core.jar.
Firstly, I have imported the dependencies (bundle with dropbox and jackson) and then imported my own project.
When I start my project, it throws the following exception;
java.lang.NoClassDefFoundError: javax/net/ssl/HttpsURLConnection
at com.dropbox.core.http.StandardHttpRequestor.prepRequest(StandardHttpRequestor.java:196)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:70)
at com.dropbox.core.http.StandardHttpRequestor.startPost(StandardHttpRequestor.java:28)
at com.dropbox.core.DbxRequestUtil.startPostRaw(DbxRequestUtil.java:232)
at com.dropbox.core.v2.DbxRawClientV2$1.execute(DbxRawClientV2.java:100)
at com.dropbox.core.v2.DbxRawClientV2.executeRetriable(DbxRawClientV2.java:256)
at com.dropbox.core.v2.DbxRawClientV2.rpcStyle(DbxRawClientV2.java:97)
at com.dropbox.core.v2.users.DbxUserUsersRequests.getCurrentAccount(DbxUserUsersRequests.java:120)
at org.eclipse.kura.example.hello_osgi.DropBoxTransfer.<init>(DropBoxTransfer.java:37)
at org.eclipse.kura.example.hello_osgi.DropBoxUpdateJob.execute(DropBoxUpdateJob.java:20)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
I have two related questions;
When we create a bundle from a public api, should this bundle contain the transitive dependencies of the public api?
Even if I supplied the Dropbox api with its transitive dependencies, why threw the program such an exception?
Typically NoClassDefFoundError happens when a bundle loads a class that is not present in the bundle and there is not Import-Package statement for the package of the class.
When creating bundles make sure you use a bnd to auto create the Manifest with suitable Import-Package and Export-Package instructions.
I would always use the build to create a bundle from a jar. As I use maven I would use a maven plugin. See this question for some possible ways to create bundles.

two dependency chains OSGI

I have the following error:
in text :
Error executing command: Error restarting bundles:
Unable to start bundle 278: Uses constraint violation. Unable to resolve resource demo-persistence-jpa [demo-persistence-jpa [278](R 278.0)] because it is exposed to package 'javax.persistence' from resources javax.persistence [javax.persistence [248](R 248.0)] and org.apache.geronimo.specs.geronimo-jpa_2.0_spec [org.apache.geronimo.specs.geronimo-jpa_2.0_spec [266](R 266.0)] via two dependency chains.
Chain 1:
demo-persistence-jpa [demo-persistence-jpa [278](R 278.0)]
import: (&(osgi.wiring.package=javax.persistence)(version>=2.1.0))
|
export: osgi.wiring.package: javax.persistence
javax.persistence [javax.persistence [248](R 248.0)]
Chain 2:
demo-persistence-jpa [demo-persistence-jpa [278](R 278.0)]
import: (osgi.wiring.package=org.hibernate.proxy)
|
export: osgi.wiring.package=org.hibernate.proxy; uses:=javax.persistence
com.springsource.org.hibernate [com.springsource.org.hibernate [230](R 230.0)]
import: (&(osgi.wiring.package=javax.persistence)(version>=1.0.0)(!(version>=2.0.0)))
|
export: osgi.wiring.package: javax.persistence
org.apache.geronimo.specs.geronimo-jpa_2.0_spec [org.apache.geronimo.specs.geronimo-jpa_2.0_spec [266](R 266.0)] Unresolved requirements: [[demo-persistence-jpa [278](R 278.0)] osgi.wiring.package; (osgi.wiring.package=org.hibernate.proxy)]
as you can see the problem is my bundle demo-persistence-jpa imports the package `javax.persistence which is available via two chains, this I understand
what I don't understand :
My bundle imports within the range version>=2.1.0
org.hibernate.proxy imports within the range (version>=1.0.0)(!(version>=2.0.0))), so there should be no problem
My bundle imports org.hibernate.proxy
so there should be no problem, as the version required by my bundle is not the same as the one required by org.hibernate.proxy
or am I mistaken ?
The problem is
demo-persistence-jpa needs both javax.persistence and org.hibernate.proxy.
hibernate bundle exports org.hibernate.proxy
hibernate bundle states it uses:=javax.persistence
for the resolver this means that whoever uses packages from hibernate bundle has to be wired to the exact same bundle/classloader providing javax.persistence that hibernate bundle is wired to.
If the runtime din't ensure that and each was wired to different bundles/classloders you would get ClassCastException the moment something from hibernate bundle returns you an object from javax.persistence because it will be coming from different classloader.
In the case above, the resolver can no ensure that because hibernate bundle needs javax.persistence version to be lower than 2.0 and demo-persistence-jpa needs the version to be higher than 2.1!
The solution is to either :
use newer version of hibernate (assuming there is one) that works with javax.persistence >= 2.1
make demo-persistence-jpa import javax.persistence < 2.1
I think the problem is that you have two bundles providing the spec in two different versions. This will not work. You have to make sure you can get along with just one jpa spec bundle.
How do you install hibernate and your own bundle? If you use the Apache karaf feature for hibernate it should work.

Can't get a working reference to an OSGi service

I'm trying to embed apache felix into a simple hello world java project with maven, but I can't find a way to get a reference to a service of a bundle. I've installed org.apache.felix.bundlerepository bundle into OSGi from a jar and also added it as a maven dependency to my project. After that I'm starting the bundle, getting BundleContext from it and then calling getServiceReference(RepositoryAdmin.class.getName()) on that bundle context. The first thing I'm unhappy with is that I have to use BundleContext from the installed bundle, if I'd use BundleContext of the Framework the ServiceReference will be always null. This is not convinient.
The second, more important issue, is that when I finally receiving a reference to RepositoryAdmin service from bundlerepository bundle I can't cast it to org.apache.felix.bundlerepository.RepositoryAdmin, executing the following code:
(RepositoryAdmin)admin.getBundleContext().getService(ref)
will throw this exception:
java.lang.ClassCastException: org.apache.felix.bundlerepository.impl.RepositoryAdminImpl cannot be cast to org.apache.felix.bundlerepository.RepositoryAdmin
I know this is a kind of classpath issue and may be caused by incompatibility of interfaces, but I'm using a bundle jar of the same version (2.0.2) as a maven dependency of my project.
I'm also aware of Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA osgi configuration option which should force a bundle to use a package from the host application, but it didnt help me.
Here is the main class of my project https://github.com/ArtemZ/osgi-study/blob/master/src/main/java/com/artemz/demo/Main.java which is messy because I was trying different things on it in order to get a reference to a service, but none actually worked for me.
Hope someone will help me, because I'm really desperate with this issue.
Thanks for giving some more details about what you are doing. I already followed the mails on the felix list.
Now I think I understand what happens. The reason why you can not get the service from outside the RepositoryAdmin bundle is that the package you define in
FRAMEWORK_SYSTEMPACKAGES_EXTRA is "org.apache.felix.bundlerepository; version=2.0.2"
is not the same version as the package from the RepositoryAdmin bundle. I downloaded the bundle and looked into the Manifest:
Export-Package: org.osgi.service.repository;version="1.0";uses:="org.osg
i.resource",org.apache.felix.bundlerepository;version="2.1";uses:="org.
osgi.framework"
So as you see the version you should export from the system bundle is 2.1 not 2.0.2.
In OSGi the versions are defined per package not on the bundle level. So while most times they are the same this is not always true. Espcecially for OSGi spec packages.
So when the package version are different you have two effects:
1. You will not be able to find a service with a different package
2. If you get a service object in some other way like you did then you will have a class cast exception as they are loaded by different classloaders.
So can you try the 2.1 version and report if it works?

org.apache.sling.api.resource,version=[2.3,3) -- Cannot be resolved

Hi I am not able to access content of my project, I have uploaded all the packages which are required to access content from CQ. Only thing I can see is
org.apache.sling.api.resource,version=[2.3,3) -- Cannot be resolved
Can this be the reason for exception and if yes please let me know how to resolve it.
CQ version 5.6
The error message means that the OSGi framework is unable to supply a version >= 2.3 and < 3 of the org.apache.sling.api.resource Java package for a bundle B that wants to import it.
As a result, bundle B cannot be activated, and parts of your system won't work.
Looking at the webconsole (under /system/console by default in Sling and CQ) you can see that this package is provided by the org.apache.sling.api bundle, so either you have an old version of that bundle in your system, or you have installed incompatible bundles that require a newer version of that package.
You can add the missing package by explicilty importring it. Accecess the pom.xml file in the core folder of your project core/pom.xml and list the dependency under Import-Package
<configuration>
<bnd><![CDATA[
Import-Package:
javax.annotation;version=0.0.0,
org.apache.sling.api.servlets;version="[2.3,3)",*
]]></bnd>
</configuration>
Check version of org.apache.sling.api.resource in package-info.class file in org.apache.sling.api.resource package in uber-jar.
You might have version between 2.3 and 3. Try installing lower version uber-jar that has package version <2.3

Package uses conflict: Import-Package: de.foo.bar; version="0.0.0"

I try to install a bundle in an OSGi environment (FUSE ESB) but do not manage to get it resolved. The error message is:
The bundle could not be resolved. Reason: Package uses conflict: Import-Package: de.foo.bar; version="0.0.0"
My bundle imports the package de.foo.bar.
The bundle which exports the package de.foo.bar does this with a 'uses' directive.
Export-Package = de.foo.bar;uses:="{other packages}";version="2.4.0"
As I understood I have to ensure that my bundle must import all other packages mentioned in the 'uses' directive of the de.foo.bar package (in the right version).
I checked this and also tried several version changes (0.0.0 and the real version numbers) but can not get it to work.
So, what does the error message realy means (maybe I understood it wrong)? What do I have to check?
Thanks for any help
Klaus
System Information:
FUSE ESB 4.2.0 (based on servicemix)
using maven-bundle-plugin 2.1.0 to generate OSGi MANIFEST header
I finally found what was wrong.
My bundle is a Spring Dynamic Module bundle and I did a mistake in the spring bean configuration (use a 'ref' instead a 'value' in a constructor-arg). Normally spring configuration errors are reported as such - I do not know why the current error resulted in the misleading message.
EDIT:
The faulty Spring configuration does not cause the uses conflict. It finally was the import of the package org.apache.log4j which is exported by different bundles (in my FUSE ESB container) and apparently was different wired to the bundles I tried to install.
Trying to solve my problem I found the article Diagnosing OSGi uses conflicts which I found helpfull to understand the problem.

Categories

Resources