KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > msg > RequestManagedObjectResponseMessage


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.object.msg;
5
6 import com.tc.async.api.EventContext;
7 import com.tc.bytes.TCByteBuffer;
8 import com.tc.io.TCByteBufferOutput;
9 import com.tc.net.protocol.tcm.MessageChannel;
10 import com.tc.net.protocol.tcm.MessageMonitor;
11 import com.tc.net.protocol.tcm.TCMessageHeader;
12 import com.tc.net.protocol.tcm.TCMessageType;
13 import com.tc.object.dna.impl.ObjectDNAImpl;
14 import com.tc.object.dna.impl.ObjectStringSerializer;
15 import com.tc.object.session.SessionID;
16 import com.tc.util.CommonShutDownHook;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22
23 /**
24  * @author steve
25  */

26 public class RequestManagedObjectResponseMessage extends DSOMessageBase implements EventContext {
27
28   private final static byte SERIALIZER_ID = 1;
29   private final static byte TOTAL_ID = 2;
30   private final static byte BATCH_ID = 3;
31   private final static byte DNA_COUNT = 4;
32   private final static byte DNA_DATA = 5;
33
34   private Collection JavaDoc objects;
35   private ObjectStringSerializer serializer;
36   private int total;
37   private long batchID;
38   private TCByteBuffer[] dnaData;
39   private int dnaCount;
40
41   public RequestManagedObjectResponseMessage(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
42                                              TCMessageType type) {
43     super(monitor, out, channel, type);
44   }
45
46   public RequestManagedObjectResponseMessage(SessionID sessionID, MessageMonitor monitor, MessageChannel channel, TCMessageHeader header,
47                                              TCByteBuffer[] data) {
48     super(sessionID, monitor, channel, header, data);
49   }
50
51   public Collection JavaDoc getObjects() {
52     return Collections.unmodifiableCollection(objects);
53   }
54
55   public void initialize(TCByteBuffer[] dnas, int count, ObjectStringSerializer aSerializer, long bid, int tot) {
56     this.dnaCount = count;
57     this.dnaData = dnas;
58     this.serializer = aSerializer;
59     this.batchID = bid;
60     this.total = tot;
61   }
62
63   public ObjectStringSerializer getSerializer() {
64     return serializer;
65   }
66
67   public long getBatchID() {
68     return batchID;
69   }
70
71   public int getTotal() {
72     return total;
73   }
74
75   protected void dehydrateValues() {
76     putNVPair(SERIALIZER_ID, serializer);
77     putNVPair(BATCH_ID, batchID);
78     putNVPair(TOTAL_ID, total);
79     putNVPair(DNA_COUNT, dnaCount);
80     putNVPair(DNA_DATA, dnaData);
81     dnaData = null;
82   }
83
84   protected boolean hydrateValue(byte name) throws IOException JavaDoc {
85     switch (name) {
86       case DNA_DATA: {
87         for (int i = 0, n = dnaCount; i < n; i++) {
88           objects.add(getObject(new ObjectDNAImpl(serializer, false)));
89         }
90         return true;
91       }
92       case DNA_COUNT:
93         dnaCount = getIntValue();
94         this.objects = new ArrayList JavaDoc(dnaCount);
95         return true;
96       case BATCH_ID:
97         this.batchID = getLongValue();
98         return true;
99       case TOTAL_ID:
100         this.total = getIntValue();
101         return true;
102       case SERIALIZER_ID:
103         this.serializer = (ObjectStringSerializer) getObject(new ObjectStringSerializer());
104         return true;
105       default:
106         return false;
107     }
108   }
109
110   static int rcount;
111   static int bufferCount;
112   static {
113     CommonShutDownHook.addShutdownHook(new Runnable JavaDoc() {
114       public void run() {
115         logger.info("No of times Buffers wasted = " + rcount + " Buffers wasted count = " + bufferCount);
116       }
117     });
118   }
119
120   // TODO :: It is recycled only on write. Not on read.
121
public void doRecycleOnRead() {
122     rcount++;
123     bufferCount += getEntireMessageData().length;
124   }
125 }
126
Popular Tags