Karaf 4.1 - 如何在第三方osgi jar包中添加DynamicImport-Package标签?

时间:2016-08-22 04:07:29

标签: osgi classloader shiro apache-karaf karaf

我在Karaf 4.1中执行自己的捆绑包时遇到问题,我使用Shiro来保持用户会话,但是当我恢复保存的会话时,我有一个例外:

Caused by: java.lang.ClassNotFoundException: io.twim.models.User
     
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:?]
    at org.apache.felix.framework.BundleWiringImpl.doImplicitBootDelegation(BundleWiringImpl.java:1782) ~[?:?]
    at org.apache.felix.framework.BundleWiringImpl.searchDynamicImports(BundleWiringImpl.java:1717) ~[?:?]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1552) ~[?:?]
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79) ~[?:?]
  

我理解我的案例中的问题Shiro正在执行(强制转换)反序列化持久化会话对象但在Shiro的ClassLoader中没有我的类io.twim.models.User。我的karaf实例安装了这个包:

karaf@twim()> list
START LEVEL 100 , List Threshold: 50
ID │ State  │ Lvl │ Version             │ Name
───┼────────┼─────┼─────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
51 │ Active │  80 │ 3.1.0               │ DataStax Java Driver for Apache Cassandra - Core
52 │ Active │  80 │ 19.0.0              │ Guava: Google Core Libraries for Java
73 │ Active │  50 │ 2.16.1              │ camel-blueprint
83 │ Active │  80 │ 1.3.0               │ Apache Shiro :: Core
86 │ Active │  80 │ 1.0.0.SNAPSHOT      │ twim-cache :: Distributed cache for TWIM
87 │ Active │  80 │ 1.0.0.SNAPSHOT      │ twim-cassandra :: Implementation Cassandra to TWIM
88 │ Active │  80 │ 1.0.0.SNAPSHOT      │ twim-common :: Bundle utility for all models, utilities, constants
89 │ Active │  80 │ 1.0.0.SNAPSHOT      │ twim-core-model :: Bundle utility for all models used in TWIM
90 │ Active │  80 │ 1.0.0.SNAPSHOT      │ twim-db :: Utilitaries to persitence into TWIM
91 │ Active │  80 │ 1.0.0.SNAPSHOT      │ twim-jaas :: JAAS authentication module for TWIM

我需要在包83中添加标签DynamicImport-Package:

83 │ Active │  80 │ 1.3.0               │ Apache Shiro :: Core

在karaf中执行 dynamic-import 命令,我已经解决了这个问题:

karaf@twim()> dynamic-import 83

但是我想在我的功能安装程序中自动执行此操作,添加此标记 DynamicImport-Package:io.twim.models ,现在我有这样的features.xml:

<feature name="twim-auth" version="${project.version}">
    <feature>twim-cassandra</feature>
    <bundle>mvn:org.apache.shiro/shiro-core/1.3.0</bundle>

    <bundle>mvn:io.twim/twim-core-model/${project.version}</bundle>
    <bundle>mvn:io.twim/twim-jaas/${project.version}</bundle>
</feature>

我如何在features.xml中执行此操作?

2 个答案:

答案 0 :(得分:1)

wrap protocol可用于从jar中即时构建osgi包。你可以用它来为一个existant bundle添加一些指令,但我从来没用过这种方式。尝试这样的事情:

<bundle>wrap:mvn:org.apache.shiro/shiro-core/1.3.0$DynamicImport-Package=io.twim.models</bundle>

答案 1 :(得分:1)

终于!,我已经找到了问题,感谢@ alexandre-cartapanis只需要进行修正,这使得它完美无缺:

<bundle>wrap:mvn:org.apache.shiro/shiro-core/1.3.0/$DynamicImport-Package=io.twim.models&amp;overwrite=merge</bundle>

此处"Wrap deployer"部分有更多解释。