KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > managedobject > ManagedObjectSerializer


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13 import java.io.ObjectOutput JavaDoc;
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 JavaDoc mo, ObjectOutput JavaDoc out) {
23     try {
24       if (!(mo instanceof ManagedObjectImpl)) {
25         //
26
throw new AssertionError JavaDoc("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 JavaDoc e) {
33       throw new TCRuntimeException(e);
34     }
35   }
36
37   public Object JavaDoc deserializeFrom(ObjectInput JavaDoc in) {
38     try {
39       // read data
40
long version = in.readLong();
41       ObjectID id = new ObjectID(in.readLong());
42       ManagedObjectState state = (ManagedObjectState) serializer.deserializeFrom(in);
43
44       // populate managed object...
45
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 JavaDoc e) {
52       throw new TCRuntimeException(e);
53     }
54   }
55
56   public byte getSerializerID() {
57     return MANAGED_OBJECT;
58   }
59 }
60
Popular Tags