views:

82

answers:

3

I'm having a session object that does NOT implement the java.lang.Serializable. (its a legacy application, and now I can't go and change the code)

Is there any way where I can store the session object some where and later GET BACK THE SAME STATE in a STANDALONE application.

One thought is to use RMI. Is there any better way of achieving this.

EDIT: The Legacy application has APIs that give me the Session object. On the Session object, I'll be invoking the methods. Getting the Session object is really an expensive operation. So, I would like to Store it some where for any subsequent actions until the Session is valid.

Thank you :)

+1  A: 

Let's assume that you can interrogate your session object to determine its state, and that given that state you can create a new session object that is the equivalent. Then you need a helper class, each instance of which is associated with a session object and which is serializable. You write the serialization of the helper class to store the state of the session, and that when you create a new one by serialization it creates the session.

DJClayworth
the problem is that the OP has no way (I think) of recreating the session object from serialized data. So you might be able to get its guts and serialize them, but if it doesn't expose methods to allow you to deserialize manually....?
Yar
A: 

From the question I assume that you can add code to the outer edges of the applications but that there are everywhere "Beware of the JabberWock" signs.

If it is not serializable then the object will probably contain references to intrinsically unserializable stuff like services, stateful beans, ... and other stuff which only makes sense in the container. Now normally most of this stuff you probably wont need outside the container.

RMI will not solve your problem, because then the payload must be serializable, back to square 1.

A pragmatic approach could be to use a simple XML generator like XML Digester of Apache Commons if it still exists, or some simple JSON generator, and pick the data from the Session you need in the standalone app and dump it in a tree which makes sense.

Peter Tillemans
Sorry, I didn't get the pragmatic approach. The Session objects have methods that I'll be invoking. I wont just read the data that has in it. I've edited the problem statement. Sorry if it WAS not clear.
HanuAthena
Thank you. Now I see. The serialization bit threw me off. Now I understand the RMI part. Yes that appears to be a good approach : create an RMI facade with an interface to access the content/methods in the Session object which remains in the JVM where it originated.I prefer stateless interfaces with 1 method per use-case passing the session id, but you can also create RMI wrappers for single instances of a session and publish them in a JNDI context for retrieval on the standalone client. If you use an EJB container this is real easy, for Spring there is Spring-Remoting which can help.
Peter Tillemans
+1  A: 

If the session object does refer to stuff that is non-serializable (not just in terms of the interface but rather in terms of what kinds of non-serializable stateful instances it holds onto), then you will have a hard time reconstructing it in a meaningful way.

If this is not the case, then you can use XStream to serialize it, even if Serializable was not implemented.

Luckily, it's pretty easy to test: if your reconstructed session object works for you, you're in the clear.

Yar
I tried Serializing using XStream. Still no luck. There are certain attributes which cannot be serialized, I guess. I'm getting the error: Exception occured: com.thoughtworks.xstream.converters.ConversionException: Could not call java.security.CodeSource.readObject() : Index: 2, Size: 2 when I try to serialize the Object.
HanuAthena
@HanuAthena, how do you interact with the session object? You cannot even see the code?
Yar
I can! I've the jar files for the Session class and its dependencies. But few of the properties in the Session object are nested deeply and some where for one attribute when its trying to serialize I'm getting that error.
HanuAthena
@HanuAthena, you say you cannot change the code, but to serialize it and deserialize you'll at least have to mark the stuff you don't want to serialize as transient (or whatever the tags are for xcode). So you could do that by altering the original code, or perhaps informing Xcode that you don't want to serialize certain properties (perhaps you have to alter original code for that).
Yar