jackson-jaxrs-providers icon indicating copy to clipboard operation
jackson-jaxrs-providers copied to clipboard

Add optional bundle dependency

Open diamondarts opened this issue 9 years ago • 2 comments

Hello,

I am using bundle com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider in an OSGi environment. The first thing I got was this exception:

java.lang.ClassNotFoundException: com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector cannot be found by com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider_2.6.3 at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:432) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:345) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:337) at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at com.fasterxml.jackson.jaxrs.json.JsonMapperConfigurator._resolveIntrospector(JsonMapperConfigurator.java:109)

The bundle com.fasterxml.jackson.module.jackson-module-jaxb-annotations was active, but not visible by the class loader of the first bundle.

My workaround was a fragment bundle. However, my suggestion here is to make the first bundle depend optionally on the other:

Require-Bundle: com.fasterxml.jackson.module.jackson-module-jaxb-annotations;bundle-version="2.6.3";resolution:=optional

Thanks for the good work, Björn

diamondarts avatar Oct 20 '15 21:10 diamondarts

This looks odd as pom.xml for json sub-module does contain:

<osgi.import>
...
,com.fasterxml.jackson.module.jaxb;resolution:=optional

which should, as far as I know, load the dependency dynamically if requested. But I am guessing that's now how it works.

I am open to suggestions for solving this problem.

cowtowncoder avatar Dec 26 '15 21:12 cowtowncoder

@diamondarts which order did you install/activate the two bundles? If the jackson-jaxrs-json-provider bundle was installed and resolved before the jackson-module-jaxb-annotations bundle then the optional import would be disabled (because in OSGi optional imports are only checked at the time the bundle is resolved, and if the package is not available at that time then it's not checked again until the bundle is refreshed/re-installed). So one option is to make sure the jackson-module-jaxb-annotations bundle is always resolved before the jackson-jaxrs-json-provider bundle (using a deployment manager or a container like Karaf can help). Alternatively if you can't change the install order you could refresh the jackson-jaxrs-json-provider bundle after the jackson-module-jaxb-annotations bundle was installed.

Another solution would be to use DynamicImport-Package instead of the optional import:

DynamicImport-Package: com.fasterxml.jackson.module.jaxb;version="[2.6,3)"

since dynamic imports are checked when needed, instead at bundle resolution time - and if a dynamic import isn't available then it will be rechecked the next time it's requested. (In other words it only caches a successful lookup, compared to an optional import which also caches a failed lookup). So a dynamic import would let you install the bundles in any order, at the cost of repeated failed lookups for the case when the jaxb bundle is not installed.

http://moi.vonos.net/java/osgi-classloaders/#optional-imports-and-dynamic-imports covers more of the pros and cons of optional vs dynamic imports

Hope this helps

mcculls avatar Jan 22 '16 19:01 mcculls