01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.managedobject;
05:
06: import com.tc.exception.TCRuntimeException;
07: import com.tc.io.serializer.api.Serializer;
08: import com.tc.object.ObjectID;
09: 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(
19: ManagedObjectStateSerializer serializer) {
20: this .serializer = serializer;
21: }
22:
23: public void serializeTo(Object mo, ObjectOutput out) {
24: try {
25: if (!(mo instanceof ManagedObjectImpl)) {
26: //
27: throw new AssertionError(
28: "Attempt to serialize an unknown type: " + mo);
29: }
30: ManagedObjectImpl moi = (ManagedObjectImpl) mo;
31: out.writeLong(moi.version);
32: out.writeLong(moi.id.toLong());
33: serializer.serializeTo(moi.state, out);
34: } catch (IOException e) {
35: throw new TCRuntimeException(e);
36: }
37: }
38:
39: public Object deserializeFrom(ObjectInput in) {
40: try {
41: // read data
42: long version = in.readLong();
43: ObjectID id = new ObjectID(in.readLong());
44: ManagedObjectState state = (ManagedObjectState) serializer
45: .deserializeFrom(in);
46:
47: // populate managed object...
48: ManagedObjectImpl rv = new ManagedObjectImpl(id);
49: rv.version = version;
50: rv.state = state;
51: rv.setIsDirty(false);
52: rv.setBasicIsNew(false);
53: return rv;
54: } catch (Exception e) {
55: throw new TCRuntimeException(e);
56: }
57: }
58:
59: public byte getSerializerID() {
60: return MANAGED_OBJECT;
61: }
62: }
|