Results 1 to 7 of 7
  1. #1
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default Creating a Pluggable Application, how?

    I'm developing a chat program which should be pluggable. Meaning another developer is able to write additional code, compile it to some format and the main application can use that new plugin to perform additional tasks. This should be possible without touching the main applications code / recompile it.

    In order for that to work, would I use jar files, which the main app loads at run time? (since hot pluggable plugins would be really nice :))

    Any hint is greatly appreciated!

  2. #2
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    I just found something, which could be (basically) the solution:
    site.red-illusions <dot> dk/2006/11/04/simple-dynamic-loadingunloading-of-code/
    (I'm still not allowed to post links :()

    Anyway, as I don't believe this fruitful debate is at an end ;), please feel free to suggest other ways to dynamically extend an application.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I have a suggestion. But you should have a excellent design for that. You can make a single interface to the additional jar file which you want to use as a plugging.

    But the difficulty you have is, on the main package you have to decide how to pass parameters from the plugging. Say you pass data using an array. Then you have to implement that aspect on the main application. In simple word before compile the main application you should have a complete architecture for the whole application.

  4. #4
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    Thanks for you reply!

    The design of that single interface is indeed of great importance (I've made an abstract class though). I hope I will do a good job on that part.

    I've tried as the posted link explained. However I've got problems:
    As the SystemClass loader doesn't load the class(es), I'm getting a ClassNotFoundException when receiving data send from plugins (let's call it a ChatMessage).
    This is due to the way I laid out a networking framework which sends and receives Objects (basically some ease of use wrapper around sending using ObjectOutputStream's). So when the SystemClassLoader doesn't know the ChatMessage,
    Java Code:
    Object o = objectInputStream.readObject();
    fails with that Exception, as the application wide ClassLoader doesn't know the class (I believe so).

    I would like to have something like a directory structure like:
    myapp/plugins/name.space.plugin1.jar
    myapp/plugins/name.space.plugin2.jar
    ...

    And then at application startup time or in between something like
    Java Code:
     mysticallyAddMissingJars();
     Class<?> cls = classloader.loadClass("name.space.plugin1");
     MyPlugin plugin =  (MyPlugin) cls.newInstance();
     plugin.basicProcedure();
    I think what I need and don't know how to perform is to dynamically add .jar files to the runtime, so that the included content is application wide available.
    Last edited by MasterD; 03-25-2009 at 01:37 PM.

  5. #5
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    As I've read elsewhere (in german and more or less a side note: www <dot> wer-weiss-was <dot> de/theme35/article1378911.html) you can associate a ClassLoader with a class by loading that class by the ClassLoader. So when that class creates new Objects, it should use the specified ClassLoader.

    So I tried the following, to associate my own ClassLoader with the ObjectInputStream, so that it can instanciate the plugged classes:
    Java Code:
    ClassLoader loader = ChatPluginLoader.getClassLoader();
    Class<?> cls = loader.loadClass(ObjectInputStream.class.getName());
    Constructor<?> cnstr = cls.getConstructor(InputStream.class);
    ois = (ObjectInputStream) cnstr.newInstance(socket.getInputStream());
    ChatPluginLoader is pure static and loads all the myapp/plugins/*.jar and has the SystemClassLoader set as parent.

    But this doesn't work either. When calling ois.readObject(); it fails (and that is the same application which uses the same ChatPluginLoader).

  6. #6
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Thumbs down Working, but not very nicely

    I was able to get that stuff running .... somehow....

    Basically I add the .jar to the SystemClassLoader, by using the Reflection API. And that's why I don't like it. I needed to change the access modifier of a method to public, and I believe this shouldn't be done. However it works.

    Can you replace the existing SystemClassLoader with an own one?

    I would love to call something like
    Java Code:
    ClassLoader.setSystemClassLoader(myLoader);
    FYI, this is the code to add a .jar to the SystemClassLoader (basically from: twit88 <dot> com/blog/2007/10/04/java-dynamic-loading-of-class-and-jar-file/)

    Java Code:
    public addURL(URL u) throws IOException{
    URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;
    try {
    	Method method = sysclass.getDeclaredMethod("addURL", URL.class);
    	method.setAccessible(true);
    	method.invoke(classloader, new Object[] { u });
    } catch (Throwable t) {
    	t.printStackTrace();
    	throw new IOException(
    			"Error, could not add URL to system classloader");
    }
    }

  7. #7
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    I really would like to here the opinions from a more advanced Java user on this topic, pretty please?

Similar Threads

  1. Replies: 0
    Last Post: 02-16-2008, 10:31 PM
  2. Replies: 0
    Last Post: 02-16-2008, 10:29 PM
  3. Replies: 1
    Last Post: 08-07-2007, 05:41 AM
  4. Creating a notepad application with java
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 06:18 PM
  5. Creating J2EE Modules For Enterprise Application
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •