01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.l2.handler;
06:
07: import com.tc.async.api.AbstractEventHandler;
08: import com.tc.async.api.ConfigurationContext;
09: 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.logging.TCLogger;
14: import com.tc.logging.TCLogging;
15: import com.tc.object.dna.impl.ObjectStringSerializer;
16: import com.tc.objectserver.api.ObjectManager;
17: import com.tc.objectserver.core.api.ManagedObject;
18: import com.tc.objectserver.core.api.ServerConfigurationContext;
19: import com.tc.util.sequence.SequenceGenerator;
20: import com.tc.util.sequence.SequenceGenerator.SequenceGeneratorException;
21:
22: import java.util.Iterator;
23: import java.util.Map;
24:
25: public class L2ObjectSyncDehydrateHandler extends AbstractEventHandler {
26:
27: private static final TCLogger logger = TCLogging
28: .getLogger(L2ObjectSyncDehydrateHandler.class);
29:
30: private final SequenceGenerator sequenceGenerator;
31:
32: private Sink sendSink;
33: private ObjectManager objectManager;
34:
35: public L2ObjectSyncDehydrateHandler(
36: SequenceGenerator sequenceGenerator) {
37: this .sequenceGenerator = sequenceGenerator;
38: }
39:
40: public void handleEvent(EventContext context) {
41: ManagedObjectSyncContext mosc = (ManagedObjectSyncContext) context;
42: Map moObjects = mosc.getObjects();
43: // XXX::Note:: this sequence id is assigned before releasing any objects to ensure that transactions are not missed
44: // for object in flight for PASSIVE-UNINITIALIED L2.
45: try {
46: mosc.setSequenceID(sequenceGenerator.getNextSequence(mosc
47: .getNodeID()));
48: } catch (SequenceGeneratorException e) {
49: logger.error("Error generating a sequence number ", e);
50: releaseAllObjects(moObjects);
51: return;
52: }
53: ObjectStringSerializer serializer = new ObjectStringSerializer();
54: TCByteBufferOutputStream out = new TCByteBufferOutputStream();
55: for (Iterator i = moObjects.values().iterator(); i.hasNext();) {
56: ManagedObject m = (ManagedObject) i.next();
57: m.toDNA(out, serializer);
58: objectManager.releaseReadOnly(m);
59: }
60: mosc.setDehydratedBytes(out.toArray(), moObjects.size(),
61: serializer);
62: sendSink.add(mosc);
63: }
64:
65: private void releaseAllObjects(Map moObjects) {
66: for (Iterator i = moObjects.values().iterator(); i.hasNext();) {
67: ManagedObject m = (ManagedObject) i.next();
68: objectManager.releaseReadOnly(m);
69: }
70: }
71:
72: public void initialize(ConfigurationContext context) {
73: super .initialize(context);
74: ServerConfigurationContext oscc = (ServerConfigurationContext) context;
75: this.objectManager = oscc.getObjectManager();
76: this.sendSink = oscc.getStage(
77: ServerConfigurationContext.OBJECTS_SYNC_SEND_STAGE)
78: .getSink();
79: }
80: }
|