kylar
asked on
Dynamic Classloading
I am writing an application that works like this:
2 clients that connect to the same server, and the server shares certain events from client to client. Each client has it's own set of application-specific plug-ins. If one client has a plug-in that the other client doesn't have, I want to send the appropriate classes to the second client, and have them dynamically loaded. I have most of this done already, but as to how to dynamically load the new classes, I don't know even where to start. I'm specifically looking for resources, code snippets and pointers to other projects that may be doing the same thing. This can happen in one of 2 ways:
1) Receive all the classes as a byte stream, write them to disk as a jar file, then load it via a classloader. This one I have a general idea how to proceed, but need more specific references, pointers to books, etc, that would be helpful.
2) Recieve the classes and store them in memory, not writing them to disk. Some how classload them and have them ready for instantiation. I have no clue how to proceed here.
I'm also willing to add lots more points, and split them up, so post whatever you want, and I'll distribute points as an ongoing thing.
Thanks a lot,
Kylar
2 clients that connect to the same server, and the server shares certain events from client to client. Each client has it's own set of application-specific plug-ins. If one client has a plug-in that the other client doesn't have, I want to send the appropriate classes to the second client, and have them dynamically loaded. I have most of this done already, but as to how to dynamically load the new classes, I don't know even where to start. I'm specifically looking for resources, code snippets and pointers to other projects that may be doing the same thing. This can happen in one of 2 ways:
1) Receive all the classes as a byte stream, write them to disk as a jar file, then load it via a classloader. This one I have a general idea how to proceed, but need more specific references, pointers to books, etc, that would be helpful.
2) Recieve the classes and store them in memory, not writing them to disk. Some how classload them and have them ready for instantiation. I have no clue how to proceed here.
I'm also willing to add lots more points, and split them up, so post whatever you want, and I'll distribute points as an ongoing thing.
Thanks a lot,
Kylar
Sorry I pressed enter accidentally...
You read the data into the readClassFromNetwork method and then do something like:
byte [] bytedata = readClassFromNetwork();
Then you construct the class instance by doing
Class c = defineClass(nameOfClass, bytedata, 0, bytedata.length);
Then you can use the newInstance)( method to get the instance of the class.
Of course this is just some tips. You obviously need to do more work but I hope this gives you some ideas about what is going on.
Hope it helps.
You read the data into the readClassFromNetwork method and then do something like:
byte [] bytedata = readClassFromNetwork();
Then you construct the class instance by doing
Class c = defineClass(nameOfClass, bytedata, 0, bytedata.length);
Then you can use the newInstance)( method to get the instance of the class.
Of course this is just some tips. You obviously need to do more work but I hope this gives you some ideas about what is going on.
Hope it helps.
Here are some of the approaches.. some are in the RMI scenario n others are general please let us know if the links helped ya a lill?
1.
http://www.javaworld.com/javaworld/jw-12-1998/jw-12-enterprise.html
2.
http://www.pramodx.20m.com/dynamic_class_loading_in_java.htm
3.
http://developer.java.sun.com/developer/JDCTechTips/2001/tt0227.html
4.
http://gcc.gnu.org/ml/java/2000-10/msg00002.html
1.
http://www.javaworld.com/javaworld/jw-12-1998/jw-12-enterprise.html
2.
http://www.pramodx.20m.com/dynamic_class_loading_in_java.htm
3.
http://developer.java.sun.com/developer/JDCTechTips/2001/tt0227.html
4.
http://gcc.gnu.org/ml/java/2000-10/msg00002.html
Hi,
I think you would need to keep classes longer than session lasts, so save your jar files on disk. And review java.net.URLClassLoader--I guess it does everything you need.
I can imagine following scenario:
1. Client 1 bootstrap connects to server and reports set of plugins it has.
1a. (Optional) Server could have a list of known plugins, so at this point new plugin can be aploaded to server.
2. There is no other client yet, so Client 1 simply waits for notification.
3. Client 2 bootstrap connects to server and reports set of plugins
4. Server compares plugin sets, sends requests to upload missing plugins to both parties (or to Client 2 if step 1a was choosen)
5. Server instructs clients to dounload missing plugins or directly pushes plugins
6. Clients have all necessary code. They can create URLClassLoaders pointing to necessary jars and use that classloaders to load main code.
7. Well, I guess you would need some plugin entry point discovery mechanics. I would use manifest entry (something like Main-Class) which points to plugin startup class, then use ClassLoader.loadClass().ne wInstance( ) to create plugin instance (sure, you need to use URLClassLoader created at point 6).
Here is a bit of code which does plugin loading (no server communication part):
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11475783
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
I think you would need to keep classes longer than session lasts, so save your jar files on disk. And review java.net.URLClassLoader--I
I can imagine following scenario:
1. Client 1 bootstrap connects to server and reports set of plugins it has.
1a. (Optional) Server could have a list of known plugins, so at this point new plugin can be aploaded to server.
2. There is no other client yet, so Client 1 simply waits for notification.
3. Client 2 bootstrap connects to server and reports set of plugins
4. Server compares plugin sets, sends requests to upload missing plugins to both parties (or to Client 2 if step 1a was choosen)
5. Server instructs clients to dounload missing plugins or directly pushes plugins
6. Clients have all necessary code. They can create URLClassLoaders pointing to necessary jars and use that classloaders to load main code.
7. Well, I guess you would need some plugin entry point discovery mechanics. I would use manifest entry (something like Main-Class) which points to plugin startup class, then use ClassLoader.loadClass().ne
Here is a bit of code which does plugin loading (no server communication part):
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11475783
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everyone, I have made other questions to split up the points:
Gir:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20284129
Baz:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20284130
Rash:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20284131
You've all been extremely helpful!
Cheers,
Kylar
Gir:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20284129
Baz:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20284130
Rash:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20284131
You've all been extremely helpful!
Cheers,
Kylar
ASKER
Rashid, you still haven't picked up your points.. I don't like leaving questions open if I can avoid it :) Here is the link again:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20284131
-K
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20284131
-K
Once you open the connection to the server and you read the bytes into an array then you can either serialize them into the disk or load them straight into memory without serialization.
If you want to construct the instance of the loaded class you should do somehting like this.
private byte [] readClassFromNetwork()
{
}