KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > memorydatastore > message > MemoryDataStoreRequestMessage


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.memorydatastore.message;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.io.TCByteBufferOutput;
8 import com.tc.net.protocol.tcm.MessageChannel;
9 import com.tc.net.protocol.tcm.MessageMonitor;
10 import com.tc.net.protocol.tcm.TCMessageHeader;
11 import com.tc.net.protocol.tcm.TCMessageType;
12 import com.tc.object.lockmanager.api.ThreadID;
13 import com.tc.object.msg.DSOMessageBase;
14 import com.tc.object.session.SessionID;
15
16 import java.io.IOException JavaDoc;
17
18 public class MemoryDataStoreRequestMessage extends DSOMessageBase {
19   private static final byte TYPE = 1;
20   private static final byte DATA_STORE_NAME = 2;
21   private static final byte THREAD_ID = 3;
22   private static final byte DATA = 4;
23   private static final byte REMOVE_ALL_FLAG = 5;
24   private static final byte GET_ALL_FLAG = 6;
25
26   public static final int PUT = 1;
27   public static final int GET = 2;
28   public static final int REMOVE = 3;
29
30   private int type;
31   private boolean removeAll;
32   private boolean getAll;
33   private String JavaDoc dataStoreName;
34   private ThreadID threadID;
35   private TCMemoryDataStoreMessageData data;
36
37   public MemoryDataStoreRequestMessage(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
38       TCMessageType type) {
39     super(monitor, out, channel, type);
40   }
41
42   public MemoryDataStoreRequestMessage(SessionID sessionID, MessageMonitor monitor, MessageChannel channel,
43       TCMessageHeader header, TCByteBuffer[] data) {
44     super(sessionID, monitor, channel, header, data);
45   }
46
47   protected void dehydrateValues() {
48     putNVPair(TYPE, this.type);
49     putNVPair(THREAD_ID, this.threadID.toLong());
50     putNVPair(DATA_STORE_NAME, this.dataStoreName);
51     if (isRemoveAll()) {
52       putNVPair(REMOVE_ALL_FLAG, this.removeAll);
53     }
54     if (isGetAll()) {
55       putNVPair(GET_ALL_FLAG, this.getAll);
56     }
57     putNVPair(DATA, data);
58   }
59
60   protected String JavaDoc describePayload() {
61     StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
62     rv.append("Type : ");
63
64     if (isPut()) {
65       rv.append("PUT \n");
66     } else if (isGet()) {
67       rv.append("GET \n");
68     } else if (isRemove()) {
69       rv.append("REMOVE \n");
70     } else {
71       rv.append("UNKNOWN \n");
72     }
73
74     rv.append(threadID.toLong()).append(" \n");
75     rv.append(data).append(" from ").append(dataStoreName).append('\n');
76
77     return rv.toString();
78   }
79
80   protected boolean hydrateValue(byte name) throws IOException JavaDoc {
81     switch (name) {
82     case TYPE:
83       this.type = getIntValue();
84       return true;
85     case THREAD_ID:
86       this.threadID = new ThreadID(getLongValue());
87       return true;
88     case REMOVE_ALL_FLAG:
89       this.removeAll = getBooleanValue();
90       return true;
91     case GET_ALL_FLAG:
92       this.getAll = getBooleanValue();
93       return true;
94     case DATA_STORE_NAME:
95       this.dataStoreName = getStringValue();
96       return true;
97     case DATA:
98       data = new TCMemoryDataStoreMessageData(type);
99       getObject(data);
100       return true;
101     default:
102       return false;
103     }
104   }
105
106   public void initializePut(ThreadID threadID, String JavaDoc dataStoreName, byte[] key, byte[] value) {
107     this.type = PUT;
108     this.threadID = threadID;
109     this.dataStoreName = dataStoreName;
110     this.data = new TCMemoryDataStoreMessageData(type, key, value);
111   }
112
113   public void initializeGet(ThreadID threadID, String JavaDoc dataStoreName, byte[] key, boolean getAll) {
114     this.type = GET;
115     this.threadID = threadID;
116     this.dataStoreName = dataStoreName;
117     this.getAll = getAll;
118     this.data = new TCMemoryDataStoreMessageData(type, key);
119   }
120   
121   public void initializeRemove(ThreadID threadID, String JavaDoc dataStoreName, byte[] key, boolean removeAll) {
122     this.type = REMOVE;
123     this.threadID = threadID;
124     this.dataStoreName = dataStoreName;
125     this.removeAll = removeAll;
126     this.data = new TCMemoryDataStoreMessageData(type, key);
127   }
128
129   public byte[] getKey() {
130     return this.data.getKey();
131   }
132
133   public byte[] getValue() {
134     return this.data.getValue();
135   }
136
137   public int getType() {
138     return this.type;
139   }
140
141   public ThreadID getThreadID() {
142     return this.threadID;
143   }
144
145   public String JavaDoc getDataStoreName() {
146     return this.dataStoreName;
147   }
148
149   public boolean isRemoveAll() {
150     return removeAll;
151   }
152   
153   public boolean isGetAll() {
154     return getAll;
155   }
156   
157   private boolean isPut() {
158     return this.type == PUT;
159   }
160
161   private boolean isGet() {
162     return this.type == GET;
163   }
164   
165   private boolean isRemove() {
166     return this.type == REMOVE;
167   }
168 }
Popular Tags