开发者

osgi: import package that partially exists inside bundle

I have a package X.Y.Z that exists in 2 bundles A and B:

bundle A
 package X.Y.Z 
  class Class1

bundle B
 pa开发者_Python百科ckage X.Y.Z 
  class Class2

Bundle B exports package X.Y.Z. Bundle A imports package X.Y.Z and gets an exception that his own class Class1 is not found. Should it work?

I use glassfish 3.1 with felix


No it should not work. If you import package X.Y.Z then that import will be used in preference to the bundle's own internal contents.

More generally, you have a problem known as split packages. Packages should be coherent and exported by a single bundle, not smeared across multiple bundles. You should refactor your bundle contents so that all classes belonging to package X.Y.Z are present in a single bundle.


Again, Niel is absolutely correct. But, sometimes vendors, for whatever reason will have multiple .jar files containing the same packages. This is sometimes done when they want to provide small .jar files for discreet implementations of thier product. An example of why this may be done is if they have a text-processing algorithm for an EDI document that is different than a text processing algorithm for an xml document. In this example, they may choose to create two .jar (versions 1 and 2) files containing "badlyPlannedImplementation.util" containing the different implementationing classes. Personally, I've only run into this a couple of times, but the question is how do you handle it?

When you run into the issue where you have two .jar files that export the same package and you want access to both packages classes you use a mechanism called "shading". Shading is when you take those two packages and you gather their contents together in another .jar files package. This used to be done by a maven plugin called "maven-shade-plugin" but now the functionality is part of the maven-bundle plugin.

First, create a new project, we'll call ours "badlyPlannedImplementationShaded". Then, in your project create a pom.xml file. In your dependency section, include dependencies for both of your .jar files that you're trying to shade together.

Then, add the following to your build section.

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactid>
   <version>2.1.0</version>
   <extensions>true</extensions>
   <configuration>
      <instructions>
         <Bundle-Version>${project.version}</Bundle-Version>
         <Export-Package>
            badlyPlannedImplementation.util;version="1",
            badlyPlannedImplementation.util;version="2"
         </Export-Package>
      </instructions>
   </configuration>
</plugin> 

Doing this will create a new bundle that contains a util package that has all of the classes from the two .jar files you were trying to use.

I hope that helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜