KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > l2 > msg > ObjectSyncMessage


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

5 package com.tc.l2.msg;
6
7 import com.tc.async.api.EventContext;
8 import com.tc.bytes.TCByteBuffer;
9 import com.tc.io.TCByteBufferInputStream;
10 import com.tc.net.groups.AbstractGroupMessage;
11 import com.tc.object.ObjectID;
12 import com.tc.object.dna.impl.ObjectDNAImpl;
13 import com.tc.object.dna.impl.ObjectStringSerializer;
14 import com.tc.util.Assert;
15
16 import java.io.IOException JavaDoc;
17 import java.io.ObjectInput JavaDoc;
18 import java.io.ObjectOutput JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Map.Entry;
28
29 public class ObjectSyncMessage extends AbstractGroupMessage implements EventContext {
30
31   public static final int MANAGED_OBJECT_SYNC_TYPE = 0;
32
33   private Set JavaDoc oids;
34   private int dnaCount;
35   private TCByteBuffer[] dnas;
36   private ObjectStringSerializer serializer;
37   private Map JavaDoc rootsMap;
38
39   public ObjectSyncMessage() {
40     // Make serialization happy
41
super(-1);
42   }
43
44   public ObjectSyncMessage(int type) {
45     super(type);
46   }
47
48   protected void basicReadExternal(int msgType, ObjectInput JavaDoc in) throws IOException JavaDoc {
49     Assert.assertEquals(MANAGED_OBJECT_SYNC_TYPE, msgType);
50     readObjectIDS(in);
51     dnaCount = in.readInt();
52     readRootsMap(in);
53     serializer = readObjectStringSerializer(in);
54     this.dnas = readByteBuffers(in);
55   }
56
57   protected void basicWriteExternal(int msgType, ObjectOutput JavaDoc out) throws IOException JavaDoc {
58     Assert.assertEquals(MANAGED_OBJECT_SYNC_TYPE, msgType);
59     writeObjectIDS(out);
60     out.writeInt(dnaCount);
61     writeRootsMap(out);
62     writeObjectStringSerializer(out,serializer);
63     writeByteBuffers(out, dnas);
64     recycle(dnas);
65     dnas = null;
66   }
67
68   private void writeRootsMap(ObjectOutput JavaDoc out) throws IOException JavaDoc {
69     out.writeInt(rootsMap.size());
70     for (Iterator JavaDoc i = rootsMap.entrySet().iterator(); i.hasNext();) {
71       Entry e = (Entry) i.next();
72       out.writeUTF((String JavaDoc) e.getKey());
73       out.writeLong(((ObjectID)e.getValue()).toLong());
74     }
75   }
76   
77   private void readRootsMap(ObjectInput JavaDoc in) throws IOException JavaDoc {
78     int size = in.readInt();
79     if(size == 0) {
80       this.rootsMap = Collections.EMPTY_MAP;
81     } else {
82       this.rootsMap = new HashMap JavaDoc(size);
83       for (int i = 0; i < size; i++) {
84         this.rootsMap.put(in.readUTF(), new ObjectID(in.readLong()));
85       }
86     }
87   }
88
89
90   private void recycle(TCByteBuffer[] buffers) {
91     for (int i = 0; i < buffers.length; i++) {
92       buffers[i].recycle();
93     }
94   }
95
96   private void writeObjectIDS(ObjectOutput JavaDoc out) throws IOException JavaDoc {
97     out.writeInt(oids.size());
98     for (Iterator JavaDoc i = oids.iterator(); i.hasNext();) {
99       ObjectID oid = (ObjectID) i.next();
100       out.writeLong(oid.toLong());
101     }
102   }
103
104   private void readObjectIDS(ObjectInput JavaDoc in) throws IOException JavaDoc {
105     int size = in.readInt();
106     oids = new HashSet JavaDoc(size);
107     for (int i = 0; i < size; i++) {
108       oids.add(new ObjectID(in.readLong()));
109     }
110   }
111
112   public void initialize(Set JavaDoc dnaOids, int count, TCByteBuffer[] serializedDNAs,
113                          ObjectStringSerializer objectSerializer, Map JavaDoc roots) {
114     this.oids = dnaOids;
115     this.dnaCount = count;
116     this.dnas = serializedDNAs;
117     this.serializer = objectSerializer;
118     this.rootsMap = roots;
119   }
120
121   public int getDnaCount() {
122     return dnaCount;
123   }
124
125   public Set JavaDoc getOids() {
126     return oids;
127   }
128
129   public ObjectStringSerializer getSerializer() {
130     return serializer;
131   }
132
133   public Map JavaDoc getRootsMap() {
134     return rootsMap;
135   }
136
137   /**
138    * This method calls returns a list of DNAs that can be applied to ManagedObjects. This method could only be called
139    * once. It throws an AssertionError if you ever call this twice
140    */

141   public List JavaDoc getDNAs() {
142     Assert.assertNotNull(this.dnas);
143     TCByteBufferInputStream toi = new TCByteBufferInputStream(this.dnas);
144     ArrayList JavaDoc objectDNAs = new ArrayList JavaDoc(dnaCount);
145     for (int i = 0; i < dnaCount; i++) {
146       ObjectDNAImpl dna = new ObjectDNAImpl(serializer, false);
147       try {
148         dna.deserializeFrom(toi);
149       } catch (IOException JavaDoc e) {
150         throw new AssertionError JavaDoc(e);
151       }
152       Assert.assertFalse(dna.isDelta());
153       objectDNAs.add(dna);
154     }
155     this.dnas = null;
156     return objectDNAs;
157   }
158
159 }
160
Popular Tags