Thursday, June 15, 2006

hacking XMLEncoder for unsigned and network friendly applets

XMLEncoder is very handy to serialize javabeans without the burden of hard coding encoders and decoders. Unfortunately, the current Sun implementation doesn't work properly with unsigned applet (there are hacks if you are willing to pay for an applet certificate).

Especially using XMLEncoder in an unsigned applet is very likely to result in security exceptions and will run slow and without any background threading solution.

Indeed, the DefaultPersistenceDelegate used by XMLEncoder for common javabeans uses to kill the network by opening dozens of sockets because it looks up for beanInfo classes on the server code base for every type inside your bean.

Fortunately, I worked hard and dicovered a hack: use the following encoder instead of XMLEncoder (you may adapt it a bit to your own beans in order to avoid some more specific beanInfo lookups). It's currently used in the last JGraphpad Community Edition online diagram editor.

Also, to decode server side XML files in an applet using an usigned applet, we use AppletFriendlyXmlDecoder, an applet friendly derivative of XMLDecoder.

The full source of AppletFriendlyEncoder which extends XMLEncoder is to be found on the Visualmodeller wiki here:
http://wiki.visualmodeller.org/AppletFriendlyXmlEncoder

A demo of it can be found in JGraphpad Community Edition demowed online at Visualmodeller here:
http://wiki.visualmodeller.org