开发者

Equivalent of ObjectOutputStream, saving not only its state but the whole object?

I'm letting the user import plugin-like classes from a remote location using URLClassLoader, so these imported classes do NOT exist in the build path (however, they all implement an interface IPlugin which is included).

I assumed one could simply use ObjectOutputStream to save all the loaded plugins to file, and later read these with ObjectInputStream. That doesn't seem to be the case though, since all it saves is the state of the object, not the containing logic (i.e. methods).

What I'd hope to do is to save the list of l开发者_开发百科oaded plugins (activePlugins) with ObjectOutputStream:

ObjectOutputStream oos = new ObjectOutputStream(*fileoutputstream*);
oos.writeObject(activePlugins);
oos.close();

Then at another runtime, load/restore all these plugins with ObjectInputStream:

ObjectInputStream ois = new ObjectInputStream(*fileinputstream*);
activePlugins = (ArrayList<IPlugin>) ois.readObject();

But since the actual object classes are not available in the build path (they are somewhere else on the harddrive), it goes haywire. What I'm after is some way of loading objects without having the classes available, i.e. loading objects with states and without dependencies.


you need your own classloader. you basically want something similar to URLClassLoader, but with the ability to download and cache the jars locally. you might want to look at extending URLClassLoader or implementing something similar to it. you basically need to just hook into the part where the jar is downloaded and stick it somewhere locally (or load it from that cached location if you already downloaded it previously).


Have a look at RMI. This extends serialization with a class transferring mechanism, so you can serialize and deserialize objects of (at the receiver) unknown classes at well, and execute their methods.

This is done using some remote class loading mechanism, I think.


Altought Java is not my primary programming framework, I have seen the same problem in other "frameworks" like PHP, Delphi and C#.

One solution is to have declared class files & paths.

The other suggestion, is that, since you expecified, you don't care about the logic, only ("data") state, you could declare a generic class that stores the value of the properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜