Created
May 3, 2011 14:05
-
-
Save thbkrkr/953375 to your computer and use it in GitHub Desktop.
Client/Server serialization/deserialization for using Hibernate with GWT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Server serialization | |
public class MServerSerializationStreamWriter extends AbstractSerializationStreamWriter | |
{ | |
@Override | |
public void writeObject(Object object) throws SerializationException | |
{ | |
if (object instanceof HibernateProxy) | |
{ | |
HibernateProxy proxyId = (HibernateProxy)object; | |
String id = storeInSession(proxy); | |
object = new MHibernateProxy(proxy.getHibernateLazyInitializer().getPersistentClass() | |
.getName(), proxyId); | |
super.writeObject(object); | |
} | |
else if (object instanceof PersistentCollection) | |
{ | |
PersistentCollection persistentCollection = (PersistentCollection)object; | |
if (persistentCollection.wasInitialized()) | |
{ | |
createNewCollection(persistentCollection); | |
} | |
else | |
{ | |
String proxyId = storeInSession(persistentCollection); | |
createMockCollection(object, proxyId); // | |
} | |
} | |
else | |
{ | |
super.writeObject(object); | |
} | |
} | |
... | |
} | |
private void createNewCollection(PersistentCollection object) throws SerializationException | |
{ | |
if (object instanceof PersistentSet) | |
{ | |
writeCollection(getObjectTypeSignature(new HashSet()), (PersistentSet)object); | |
} | |
else if (object instanceof PersistentList) | |
... | |
else if (object instanceof PersistentMap) | |
... | |
} | |
private void createMockCollection(Object object, String id) throws SerializationException | |
{ | |
if (object instanceof PersistentSet) | |
{ | |
Set res = new MockPersistentSet(id); | |
super.writeObject(res); | |
} | |
else if (object instanceof PersistentList) | |
... | |
else if (object instanceof PersistentMap) | |
... | |
} | |
// Server deserialization | |
public final class MServerSerializationStreamReader extends MAbstractSerializationStreamReader | |
{ | |
@Override | |
public Object readObject() throws SerializationException | |
{ | |
Object proxy = super.readObject(); | |
if (proxy instanceof IMPersistentCollection) | |
{ | |
String proxyId = ((IMPersistentCollection)value).getProxyId(); | |
proxy = getFromSession(serializationPolicyProvider, proxyId); | |
} | |
if (proxy instanceof MHibernateProxy) | |
{ | |
String proxyId = ((MHibernateProxy)value).getProxyId(); | |
proxy = getFromSession(serializationPolicyProvider, proxyId); | |
} | |
return proxy; | |
} | |
... | |
} | |
// Server MHibernateProxy_CustomFieldSerializer | |
public static void deserialize(SerializationStreamReader streamReader, MHibernateProxy instance) throws SerializationException | |
{ | |
instance.setProxyId(streamReader.readString()); | |
instance.setClazz(streamReader.readString()); | |
} | |
public static void serialize(SerializationStreamWriter streamWriter, MHibernateProxy instance) | |
throws SerializationException | |
{ | |
streamWriter.writeString(instance.getProxyId()); | |
streamWriter.writeString(instance.getClazz()); | |
} | |
// Client MHibernateProxy_CustomFieldSerializer | |
public static Object instantiate(SerializationStreamReader streamReader) throws SerializationException | |
{ | |
MHibernateProxy instance = new MHibernateProxy(); | |
instance.setProxyId(streamReader.readString()); | |
instance.setClazz(streamReader.readString()); | |
IProxyFactory factory = (IProxyFactory)GWT.create(ProxyFactory.class); | |
Identifiable identifiable = factory.newInstance(instance.getClazz()); | |
IMockEntity mock = (IMockEntity)identifiable; | |
mock.setProxyId(instance.getProxyId()); | |
return identifiable; | |
} | |
public static void serialize(SerializationStreamWriter streamWriter, Object instance) throws SerializationException | |
{ | |
MHibernateProxy proxy = (MHibernateProxy)instance; | |
streamWriter.writeString(proxy.getProxyId()); | |
streamWriter.writeString(proxy.getClazz()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment