tags:

views:

65

answers:

3

Hi all,

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 to 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

+3  A: 

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

bryantsai
@zjffdu: This is most definitely the way to go. OSGi is built to manage complex dependencies, just like this one, with multiple versions of libraries and multiple providers of services.
Mark E
Problem is that both your code, the third-party library, and the rest of application need to be "OSGi-ready".
Thilo
@Thilo It's simple enough to bundle an arbitrary third-party library, takes me about 20-30s using Eclipse's import functionality.
Mark E
@Mark: okay, that takes the third-party out of the question, but how about your library and the rest of the application (none of which are likely to use OSGi at this point)?
Thilo
@Thilo: fair enough, you can always deploy your application as a JAR and repackage it as an OSGi bundle...there's an upstart cost, sure, but if you're set on using multiple versions of a library it's hard to justify doing something other than OSGi. OSGi isn't always the solution but when you need to deal with complex dependencies it's hard to find something better.
Mark E
@Mark: I don't think the OP really wants to have multiple versions of the library at runtime. A more common case is to support (one of) multiple versions with the same jar file, just like a JDBC driver does that can work with different versions of java.sql.
Thilo
+1  A: 

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.

Thilo
A: 

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.

pgras