• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using different versions of the same class

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to build an application for administration of a number of content management servers running on separate physical machines (with different OS). Each CM-server exposes itself via a vendor-supplied server API (Java based). That API may be called by using a vendor-supplied client side API (also Java based).
In order to manage a specific server I need to use the correct version of the client-side API. Say for example that I want to manage two CM-Servers that uses API version 4.1 and API version 3.2 respectively. It is not possible for me to use the 4.1 API to call the 3.2 API. I need to load the correct client API classes dynamically.
Does anyone have any idea how to solve this problem?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a different ClassLoader for each version of the code.
eg your application has its default ClasslOader, and there are 2 jars V1.jar and V2.jar that are not on the application CLASSPATH!
Inside your application, have two URLClassLoaders, one that refers to V1.jar and a second that refers to V2.jar
It is possible to load Classes and instantiate them as Objects. You can't cast them since you don't have them on the system classpath to compile, but you can use reflection to execute the methods you want. Rather than excessive use of reflection, you may want to create a wrapper that delegates to the particular version you are currently refering to.
Hope this helps,
Dave.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the answer!
Another question - The reason for not putting the two jar files in your example on the CLASSPATH, is it because you want to avoid it being loaded by a parent classloader?
Per
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. The default behaviour for ClassLoaders is to try to let the parent load it first. Therefore if your URLClassLoaders try to load their own version, they'll defer to the parent and it'll load the first version it sees and that's all you'll get.
Dave
 
reply
    Bookmark Topic Watch Topic
  • New Topic