views:

35

answers:

2

I have a system for which I am creating a plugin through a well defined access point. However the plugin in question uses some jar that the framework also uses but of a different version.

The code is structured in a way where the code integrating in the framework (extending the plugin extension points) and the code doing the actual work is very well decoupled (basically boils down to an API of less than 10 classes). However behind these few classes is hidden a fairly big and complex system (60+ jar dependency)

The question is as follows :

How do I ensure that anything instantiated by the API classes is done using a seperate private library of jar files ?

In other words, how to I isolate the plugin so that it runs with it's own private library without interfering with the main application's libraries ?

A: 

As far as I know, the only way to load two versions fo the same class in Java is to run two instances of the JVM.

atk
Nope. You could do with a custom class loader. See http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html
Dirk
Indeed a custom class loader should do it. However how would I grantee the class loader would obfuscate the private library while ensuring the API classes would find their dependencies. I will look into the Class loader API see if I can make it fit my needs.
Newtopian
@atk : it is possible since this is the model under which most web app servers would operate. Some would fork a new VM but most will allow executing different applications under the same VM while keeping each isolated from each-other. I was curious to know it there was a quick way to achieve the same functionality.
Newtopian
+4  A: 

That's what OSGi is for. And Equinox a small nice implementation.

Pablo Lalloni
I knew about OSGi but was looking for something more lightweight. If I had access to the main application I probably would have chosen OSGi to implement the plugin infrastructure but since I am only developing a plugin for an existing system it seems like overkill. Not mentioning the added complexity in introducing this. I am keeping it in mind though as if other solution were to fail I would fall back on OSGi.
Newtopian
Mmm... then I guess you could add classworlds to your plugin dependencies and use it for setting up a custom classloader giving preference to plugin's classpath.
Pablo Lalloni
For this to work you need to setup some kind of loader class in your plugin which setup the custom classloader and then use it to dynamically load the "real" plugin classes.
Pablo Lalloni
This would be easier to exemplify if you posted some more info like how is the interface your plugin exposes to the system and how your plugin is instantiated by the system.
Pablo Lalloni
Forgot the link to classworlds examples: http://classworlds.codehaus.org/apiusage.html
Pablo Lalloni