OSGI:maven wrapped bundle throws java.lang.ClassNotFoundException if it try to access another wrapped bundle

OSGI : maven wrapped bundle throws java.lang.ClassNotFoundException if it tries to access another wrapped bundle

由于以下依赖项不是 OSGI 就绪的,所以我将它们包装如下:

  <bundle>wrap:mvn:com.google.maps/google-maps-services/0.9.2</bundle>
  <bundle>wrap:mvn:com.squareup.okhttp3/okhttp/3.13.1</bundle>

似乎我的第一个包无法访问第二个包,尽管我将其包含在我的导入中如下:

<Import-Package>okhttp3,*</import-Package>

我得到:

Caused by: java.lang.NoClassDefFoundError: okhttp3/Authenticator
    at com.google.maps.GeoApiContext$Builder.<init>(GeoApiContext.java:318)
    at com.elavon.nabsd.creditsafe.beans.GooglePlacesAPIContext.<clinit>(GooglePlacesAPIContext.java:11)
    at com.elavon.nabsd.creditsafe.beans.AddressParser.getAddress(AddressParser.java:35)
    at com.elavon.nabsd.creditsafe.beans.AddressParser.dummyResponse(AddressParser.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
    at org.apache.camel.component.bean.MethodInfo.doProceed(MethodInfo.java:279)
    at org.apache.camel.component.bean.MethodInfo.proceed(MethodInfo.java:252)
    ... 13 more
Caused by: java.lang.ClassNotFoundException: okhttp3.Authenticator not found by wrap_mvn_com.google.maps_google-maps-services_0.9.2 [1180]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556)
    at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:77)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 24 more

清单

Manifest-Version: 1.0
Bnd-LastModified: 1552097042208
Build-Jdk: 1.8.0_181
Built-By: c041356
Bundle-Blueprint: OSGI-INF/blueprint/address-parser-blueprint.xml
Bundle-Description: XXX
Bundle-DocURL: XXX
Bundle-ManifestVersion: 2
Bundle-Name: XXX
Bundle-SymbolicName: address-parser
Bundle-Vendor: XXX
Bundle-Version: 1.0.24
Created-By: Apache Maven Bundle Plugin
DynamicImport-Package: *
Export-package: com.squareup.okhttp3,okhttp3
Import-Package: okhttp3,com.fasterxml.jackson.annotation,com.google.maps
 ,com.google.maps.errors,com.google.maps.model,org.apache.camel.componen
 t.jasypt,org.jasypt.encryption.pbe.config,org.osgi.service.blueprint;ve
 rsion="[1.0.0,2.0.0)",org.slf4j,org.apache.camel.component.http4,org.ap
 ache.camel.component.jackson,com.squareup.okhttp3
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-3.0.0.201509101326

wrap_mvn_com.squareup.okhttp3_okhttp_3.13.1 (1202)
--------------------------------------------------
Originally-Created-By = Apache Maven 3.6.0
Created-By = 1.8.0_181 (Oracle Corporation)
Manifest-Version = 1.0
Bnd-LastModified = 1552095323499
Generated-By-Ops4j-Pax-From = wrap:mvn:com.squareup.okhttp3/okhttp/3.13.1
Build-Jdk = 1.8.0_162
Built-By = jwilson
Automatic-Module-Name = okhttp3
Tool = Bnd-2.3.0.201405100607

Bundle-ManifestVersion = 2
Bundle-SymbolicName = wrap_mvn_com.squareup.okhttp3_okhttp_3.13.1
Bundle-Version = 0
Bundle-Name = wrap_mvn_com.squareup.okhttp3_okhttp_3.13.1

Require-Capability =
        osgi.ee;filter:=(&(osgi.ee=JavaSE)(version=1.8))

Export-Package =
        okhttp3;uses:="javax.annotation,javax.net,javax.net.ssl,okio",
        okhttp3.internal;uses:="javax.annotation,javax.net.ssl,okhttp3,okhttp3.internal.cache,okhttp3.internal.connection,okhttp3.internal.http,okhttp3.internal.http2,okio",
        okhttp3.internal.annotations;uses:="javax.annotation,javax.annotation.meta",
        okhttp3.internal.cache;uses:="javax.annotation,okhttp3,okhttp3.internal.io,okio",
        okhttp3.internal.cache2,
        okhttp3.internal.connection;uses:="javax.annotation,javax.net.ssl,okhttp3,okhttp3.internal.http,okhttp3.internal.http2,okhttp3.internal.ws",
        okhttp3.internal.duplex,
        okhttp3.internal.http;uses:="javax.annotation,okhttp3,okhttp3.internal.connection,okio",
        okhttp3.internal.http1;uses:="okhttp3,okhttp3.internal.connection,okhttp3.internal.http,okio",
        okhttp3.internal.http2;uses:="okhttp3,okhttp3.internal.connection,okhttp3.internal.http,okio",
        okhttp3.internal.io;uses:=okio,
        okhttp3.internal.platform;uses:="javax.annotation,javax.net.ssl,okhttp3,okhttp3.internal.tls",
        okhttp3.internal.proxy,
        okhttp3.internal.publicsuffix,
        okhttp3.internal.tls;uses:=javax.net.ssl,
        okhttp3.internal.ws;uses:="javax.annotation,okhttp3,okio"
Import-Package =
        android.os;resolution:=optional,
        android.util;resolution:=optional,
        javax.annotation;resolution:=optional,
        javax.annotation.meta;resolution:=optional,
        javax.net;resolution:=optional,
        javax.net.ssl;resolution:=optional,
        javax.security.auth.x500;resolution:=optional,
        okio;resolution:=optional,

wrap_mvn_com.google.maps_google-maps-services_0.9.2 (1180)
----------------------------------------------------------
Created-By = 1.8.0_181 (Oracle Corporation)
Manifest-Version = 1.0
Bnd-LastModified = 1552091296783
Generated-By-Ops4j-Pax-From = wrap:mvn:com.google.maps/google-maps-services/0.9.2
Tool = Bnd-2.3.0.201405100607

Bundle-ManifestVersion = 2
Bundle-SymbolicName = wrap_mvn_com.google.maps_google-maps-services_0.9.2
Bundle-Version = 0
Bundle-Name = wrap_mvn_com.google.maps_google-maps-services_0.9.2

Require-Capability =
        osgi.ee;filter:=(&(osgi.ee=JavaSE)(version=1.8))

Export-Package =
        com.google.maps;uses:="com.google.gson,com.google.maps.errors,com.google.maps.internal,com.google.maps.model,okhttp3",
        com.google.maps.errors,
        com.google.maps.internal;uses:="com.google.appengine.api.urlfetch,com.google.gson,com.google.gson.stream,com.google.maps,com.google.maps.errors,com.google.maps.model,okhttp3",
        com.google.maps.internal.ratelimiter,
        com.google.maps.model;uses:=com.google.maps.internal
Import-Package =
        com.google.appengine.api.urlfetch;resolution:=optional,
        com.google.gson;resolution:=optional,
        com.google.gson.stream;resolution:=optional,
        javax.crypto;resolution:=optional,
        javax.crypto.spec;resolution:=optional,
        okhttp3;resolution:=optional,
        okio;resolution:=optional,
        org.slf4j;resolution:=optional

包装的捆绑包将自动发现的包依赖项作为 "optional",因为在使用捆绑包提供的特定功能时,无法可靠地判断是否实际需要某些特定的包。

因此,在捆绑包解析时其他捆绑包及其导出包的存在决定了哪些包对所创建的 class-loader 已解析包装包实际可见。

在您的情况下,当解析器在解析捆绑包 "google-maps".

时搜索要导入的可用包时,捆绑包 "okhttp3" 导出的包不可用

这反过来意味着您需要确保:

  1. 在解析包 "google-maps"
  2. 时,包 "okhttp3" 对解析器可见
  3. 确保所需的包导入不是 "optional"

虽然第一个选项看起来更容易,但第二个选项更可靠和灵活。

wrap: protocol 支持覆盖生成的 OSGi headers 以便您可以根据需要指定详细信息。在我们的例子中,它将是:

<bundle>wrap:mvn:com.google.maps/google-maps-services/0.9.2$overwrite=merge&amp;Import-Package=okhttp3,okio,*;resolution:=optional</bundle>

(未经测试,仅基于文档)

通知

在使用wrap 协议之前,请先查看所需的库是否已被其他项目OSGi-fied 使用。一个很好的开始是查看 Apache ServiceMix 项目提供的捆绑包(它提供了一个 okhttp3 捆绑包):