开发者

How can I let differenct version of JAVA API work together?

I want to support different versions of a 3rd software. So I plan to create a factory which can create different versions of Object, but I don't know how t开发者_开发问答o do that, different version of 3rd software have the same package hierarchy, and conflicts will happen when I put them together into one project, anyone have such experience and any ideas ?

Thanks

Jeff Zhang


Your best bet is OSGi. Also see this Tutorial as a starting point.


The easiest way is to have adapters in your code (a package for every version you support).

If the versions of the library are compatible with older versions, you can compile against the newest versions (this is how JDBC drivers are done, that need to support JDBC versions 2, 3, and 4 with the same driver jar), but if they are not, you will have to compile these adapter packages separately (against their version of the library). The second way is safer, too. You can then merge the binaries together. Your factory will make sure that only the correct code will run.

You would then have a factory that figures out which version of the library to use (either by probing it somehow, or have to user configure it explicitly), and instantiates the appropriate adapter.

Update: The above assumes that you want to produce a JAR file that can work with different versions of another JAR file, but that at run-time (in a given installation), there would be only one fixed version of the third-party library. If you need to support multiple versions of the same classes in the same JVM, then you really need to look at OSGi.


Another solution if you need to have several versions loaded at runtime is to load the classes with their own classloader using parent-last delegation. You will have to write your own classloader for that, you can google for "parent last classloader" to find some examples.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜