KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > l2 > handler > L2ObjectSyncDehydrateHandler


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.handler;
6
7 import com.tc.async.api.AbstractEventHandler;
8 import com.tc.async.api.ConfigurationContext;
9 import com.tc.async.api.EventContext;
10 import com.tc.async.api.Sink;
11 import com.tc.io.TCByteBufferOutputStream;
12 import com.tc.l2.context.ManagedObjectSyncContext;
13 import com.tc.object.dna.impl.ObjectStringSerializer;
14 import com.tc.objectserver.api.ObjectManager;
15 import com.tc.objectserver.core.api.ManagedObject;
16 import com.tc.objectserver.core.api.ServerConfigurationContext;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 public class L2ObjectSyncDehydrateHandler extends AbstractEventHandler {
22
23   private Sink sendSink;
24   private ObjectManager objectManager;
25
26   public void handleEvent(EventContext context) {
27     ManagedObjectSyncContext mosc = (ManagedObjectSyncContext) context;
28     Map JavaDoc moObjects = mosc.getObjects();
29     // Only send objects that are NOT already there in the client. Look at the comment below.
30
ObjectStringSerializer serializer = new ObjectStringSerializer();
31     TCByteBufferOutputStream out = new TCByteBufferOutputStream();
32     for (Iterator JavaDoc i = moObjects.values().iterator(); i.hasNext();) {
33       ManagedObject m = (ManagedObject) i.next();
34       m.toDNA(out, serializer);
35       objectManager.releaseReadOnly(m);
36     }
37     mosc.setDehydratedBytes(out.toArray(), moObjects.size(), serializer);
38     sendSink.add(mosc);
39   }
40
41   public void initialize(ConfigurationContext context) {
42     super.initialize(context);
43     ServerConfigurationContext oscc = (ServerConfigurationContext) context;
44     this.objectManager = oscc.getObjectManager();
45     this.sendSink = oscc.getStage(ServerConfigurationContext.OBJECTS_SYNC_SEND_STAGE).getSink();
46   }
47 }
48
Popular Tags