1 4 package com.tc.objectserver.managedobject; 5 6 import com.tc.exception.TCRuntimeException; 7 import com.tc.io.serializer.api.Serializer; 8 import com.tc.object.ObjectID; 9 import com.tc.objectserver.core.api.ManagedObjectState; 10 11 import java.io.IOException ; 12 import java.io.ObjectInput ; 13 import java.io.ObjectOutput ; 14 15 public class ManagedObjectSerializer implements Serializer { 16 private final ManagedObjectStateSerializer serializer; 17 18 public ManagedObjectSerializer(ManagedObjectStateSerializer serializer) { 19 this.serializer = serializer; 20 } 21 22 public void serializeTo(Object mo, ObjectOutput out) { 23 try { 24 if (!(mo instanceof ManagedObjectImpl)) { 25 throw new AssertionError ("Attempt to serialize an unknown type: " + mo); 27 } 28 ManagedObjectImpl moi = (ManagedObjectImpl) mo; 29 out.writeLong(moi.version); 30 out.writeLong(moi.id.toLong()); 31 serializer.serializeTo(moi.state, out); 32 } catch (IOException e) { 33 throw new TCRuntimeException(e); 34 } 35 } 36 37 public Object deserializeFrom(ObjectInput in) { 38 try { 39 long version = in.readLong(); 41 ObjectID id = new ObjectID(in.readLong()); 42 ManagedObjectState state = (ManagedObjectState) serializer.deserializeFrom(in); 43 44 ManagedObjectImpl rv = new ManagedObjectImpl(id); 46 rv.version = version; 47 rv.state = state; 48 rv.setIsDirty(false); 49 rv.setBasicIsNew(false); 50 return rv; 51 } catch (Exception e) { 52 throw new TCRuntimeException(e); 53 } 54 } 55 56 public byte getSerializerID() { 57 return MANAGED_OBJECT; 58 } 59 } 60 | Popular Tags |