7

I'm trying to find a way to support 2 different versions of an API in my code base. They have the same package names but work much differently under the hood (two completely different systems). How can I do this?

Both of these API's also have a dependency on Bouncy Castle but they use different versions of it. How do I also take this into account?

Rob
  • 73
  • 4
  • 2
    You could use a tool to rename the packages, even in built jars, or you could use something like OSGi (renaming the packages is much easier) – Peter Lawrey May 17 '16 at 13:05
  • How do you plan to use those libraries in your own code ? 2 different versions mean 2 different methods signatures (for example) ... you HAVE to make 2 different versions of your application ... i don't see any other choice here ... – Pras May 17 '16 at 13:05
  • 1
    @PeterLawrey What tools have you used or do you know of? I just found one called "Jar Jar Links" seems to do this. – Rob May 17 '16 at 13:13
  • @Pras When I need to communicate with the older system I will use the older API and when i need to communicate with the newer system I will use the newer API. 2 versions of the application wont fly. – Rob May 17 '16 at 13:15
  • is your application some kind of webservices ? RESTfull service ? 2 different end points would do the trick in that case ... or, i don't see how you will be able to differentiate between the old system and the new one ... are you able to differentiate the 2 systems in the first place ? – Pras May 17 '16 at 13:23
  • What kind of API are you consuming? Is it a RESTful API? Is it just a library? Please provide more info – fps May 17 '16 at 13:47

3 Answers3

2

I would not recommend this unless you know what you're doing but you could use a URLClassLoader as follows:

URLClassLoader classLoaderA = URLClassLoader.newInstance(new URL[] {new URL("versionA.jar")});
URLClassLoader classLoaderB = URLClassLoader.newInstance(new URL[] {new URL("versionB.jar")});

Load the class:

classLoaderA.loadClass("SomeClass");

Another option is to have a look at OSGI.

Diyarbakir
  • 1,999
  • 18
  • 36
  • This does not account for his main issues of loading separate versions of Bouncy Castle as well as multiple versions of the same classes from his API. Your method will still Parent load first and if not programmed correctly will have ClassNotFound implications. – Mr00Anderson May 17 '16 at 17:33
1

The solution I would start with is... Loading the API into a custom class loader that loads the child class over the parent class first. If you compile Bouncy Castle inside of the API then you wont need to worry about loading it separately. If you dynamically load the Bouncy Castle jar during run time, then in the custom class loader, you would add Bouncy Castle and your API to that class loader. Using URLClassLoader and see my link below for Parent last loading.

How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?

Community
  • 1
  • 1
Mr00Anderson
  • 854
  • 8
  • 16
0

You could create a custom class loader to load the appropriate classes based on the logic required for loading one version of the API vs the other.

cmparish
  • 66
  • 3