KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > TCMessageType


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

5 package com.tc.net.protocol.tcm;
6
7 import com.tc.exception.TCRuntimeException;
8 import com.tc.logging.TCLogger;
9 import com.tc.logging.TCLogging;
10 import com.tc.util.Util;
11
12 import gnu.trove.TIntObjectHashMap;
13
14 import java.lang.reflect.Field JavaDoc;
15 import java.lang.reflect.Modifier JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Define all the various TC Message type numbers.
24  */

25
26 public final class TCMessageType {
27   // //////////////////////////////////////////////
28
// NOTE: Never recycle these numbers, always add a new constant
29
// //////////////////////////////////////////////
30
public static final int TYPE_PING_MESSAGE = 1;
31   public static final int TYPE_PONG_MESSAGE = 2;
32   public static final int TYPE_REQUEST_ROOT_MESSAGE = 8;
33   public static final int TYPE_LOCK_REQUEST_MESSAGE = 9;
34   public static final int TYPE_COMMIT_TRANSACTION_MESSAGE = 10;
35   public static final int TYPE_REQUEST_ROOT_RESPONSE_MESSAGE = 11;
36   public static final int TYPE_REQUEST_MANAGED_OBJECT_MESSAGE = 12;
37   public static final int TYPE_REQUEST_MANAGED_OBJECT_RESPONSE_MESSAGE = 13;
38   public static final int TYPE_BROADCAST_TRANSACTION_MESSAGE = 14;
39   public static final int TYPE_OBJECT_ID_BATCH_REQUEST_MESSAGE = 18;
40   public static final int TYPE_OBJECT_ID_BATCH_REQUEST_RESPONSE_MESSAGE = 19;
41   public static final int TYPE_ACKNOWLEDGE_TRANSACTION_MESSAGE = 24;
42   public static final int TYPE_LOCK_RESPONSE_MESSAGE = 26;
43   public static final int TYPE_CLIENT_HANDSHAKE_MESSAGE = 28;
44   public static final int TYPE_BATCH_TRANSACTION_ACK_MESSAGE = 29;
45   public static final int TYPE_CLIENT_HANDSHAKE_ACK_MESSAGE = 30;
46   public static final int TYPE_CONFIG_PUSH_MESSAGE = 31;
47   public static final int TYPE_OVERRIDE_APPLICATION_CONFIG_MESSAGE = 32;
48   public static final int TYPE_LOCK_RECALL_MESSAGE = 33;
49   public static final int TYPE_JMX_MESSAGE = 34;
50   public static final int TYPE_LOCK_QUERY_RESPONSE_MESSAGE = 35;
51   public static final int TYPE_JMXREMOTE_MESSAGE_CONNECTION_MESSAGE = 36;
52   public static final int TYPE_MEMORY_DATA_STORE_REQUEST_MESSAGE = 37;
53   public static final int TYPE_MEMORY_DATA_STORE_RESPONSE_MESSAGE = 38;
54   public static final int TYPE_CLUSTER_MEMBERSHIP_EVENT_MESSAGE = 39;
55   public static final int TYPE_CLIENT_JMX_READY_MESSAGE = 40;
56
57   public static final TCMessageType PING_MESSAGE = new TCMessageType();
58   public static final TCMessageType PONG_MESSAGE = new TCMessageType();
59   public static final TCMessageType REQUEST_ROOT_MESSAGE = new TCMessageType();
60   public static final TCMessageType LOCK_REQUEST_MESSAGE = new TCMessageType();
61   public static final TCMessageType LOCK_RECALL_MESSAGE = new TCMessageType();
62   public static final TCMessageType LOCK_RESPONSE_MESSAGE = new TCMessageType();
63   public static final TCMessageType LOCK_QUERY_RESPONSE_MESSAGE = new TCMessageType();
64   public static final TCMessageType COMMIT_TRANSACTION_MESSAGE = new TCMessageType();
65   public static final TCMessageType REQUEST_ROOT_RESPONSE_MESSAGE = new TCMessageType();
66   public static final TCMessageType REQUEST_MANAGED_OBJECT_MESSAGE = new TCMessageType();
67   public static final TCMessageType REQUEST_MANAGED_OBJECT_RESPONSE_MESSAGE = new TCMessageType();
68   public static final TCMessageType BROADCAST_TRANSACTION_MESSAGE = new TCMessageType();
69   public static final TCMessageType OBJECT_ID_BATCH_REQUEST_MESSAGE = new TCMessageType();
70   public static final TCMessageType OBJECT_ID_BATCH_REQUEST_RESPONSE_MESSAGE = new TCMessageType();
71   public static final TCMessageType ACKNOWLEDGE_TRANSACTION_MESSAGE = new TCMessageType();
72   public static final TCMessageType CLIENT_HANDSHAKE_MESSAGE = new TCMessageType();
73   public static final TCMessageType CLIENT_HANDSHAKE_ACK_MESSAGE = new TCMessageType();
74   public static final TCMessageType BATCH_TRANSACTION_ACK_MESSAGE = new TCMessageType();
75   public static final TCMessageType CONFIG_PUSH_MESSAGE = new TCMessageType();
76   public static final TCMessageType OVERRIDE_APPLICATION_CONFIG_MESSAGE = new TCMessageType();
77   public static final TCMessageType JMX_MESSAGE = new TCMessageType();
78   public static final TCMessageType JMXREMOTE_MESSAGE_CONNECTION_MESSAGE = new TCMessageType();
79   public static final TCMessageType MEMORY_DATA_STORE_REQUEST_MESSAGE = new TCMessageType();
80   public static final TCMessageType MEMORY_DATA_STORE_RESPONSE_MESSAGE = new TCMessageType();
81   public static final TCMessageType CLUSTER_MEMBERSHIP_EVENT_MESSAGE = new TCMessageType();
82   public static final TCMessageType CLIENT_JMX_READY_MESSAGE = new TCMessageType();
83
84   public static TCMessageType getInstance(int i) {
85     return (TCMessageType) typeMap.get(i);
86   }
87
88   public static TCMessageType[] getAllMessageTypes() {
89     return (TCMessageType[]) allTypes.clone();
90   }
91
92   public int getType() {
93     return type;
94   }
95
96   public String JavaDoc getTypeName() {
97     return typeName;
98   }
99
100   public String JavaDoc toString() {
101     return typeName + " (" + type + ")";
102   }
103
104   // //////////////////////////////////////////////////////
105
//
106
// ******** You need not modify anything below *********
107
//
108
// //////////////////////////////////////////////////////
109
private static final TCLogger logger = TCLogging.getLogger(TCMessageType.class);
110   private static final TIntObjectHashMap typeMap = new TIntObjectHashMap();
111   private static final TCMessageType[] allTypes;
112   private static final String JavaDoc typePrefix = "TYPE_";
113
114   private int type;
115   private String JavaDoc typeName;
116
117   private TCMessageType() {
118     // type and name are set automagically in init()
119
}
120
121   private void setType(int type) {
122     this.type = type;
123   }
124
125   private void setTypeName(String JavaDoc typeName) {
126     this.typeName = typeName;
127   }
128
129   public int hashCode() {
130     return this.typeName.hashCode() + type;
131   }
132
133   public boolean equals(Object JavaDoc obj) {
134     if (obj instanceof TCMessageType) {
135       TCMessageType other = (TCMessageType) obj;
136       return this.typeName.equals(other.typeName) && (this.type == other.type);
137     }
138     return false;
139   }
140
141   /**
142    * do some sanity checking on all the defined message types. Hopefully we'll catch duplicate message numbers early at
143    * development time. If somehow a compiler introduces funny public static final fields in this class, then one can add
144    * additional checks for naming conventions for the message types. Or maybe we shouldn't try to be smart about
145    * validating the message type numbers dynamically. Maybe I need more some coffee
146    */

147   private static TCMessageType[] init() throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc {
148     Field JavaDoc[] fields = TCMessageType.class.getDeclaredFields();
149
150     Map mtFields = new HashMap();
151     Map intFields = new HashMap();
152
153     for (int i = 0; i < fields.length; i++) {
154       final Field JavaDoc field = fields[i];
155       final String JavaDoc fName = field.getName();
156
157       int modifiers = field.getModifiers();
158
159       // disallow public non-final fields
160
if (Modifier.isPublic(modifiers) && !Modifier.isFinal(modifiers)) { throw new RuntimeException JavaDoc(
161                                                                                                      "TCMessageType: "
162                                                                                                          + fName
163                                                                                                          + " must be final if public"); }
164
165       boolean shouldInspect = Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
166                               && Modifier.isFinal(modifiers);
167
168       if (!shouldInspect) {
169         continue;
170       }
171
172       if (field.getType().equals(TCMessageType.class)) {
173         if (!fName.toUpperCase().equals(fName)) {
174           // make formatter sane
175
throw new RuntimeException JavaDoc("TCMessageType: Message type names must be all UPPER CASE: " + fName);
176         }
177
178         if (fName.startsWith("_")) {
179           // make formatter sane
180
throw new RuntimeException JavaDoc("TCMessageType: Message type cannot start with underscore: " + fName);
181         }
182
183         mtFields.put(fName, field);
184       } else if (field.getType().equals(Integer.TYPE)) {
185         if (!fName.startsWith(typePrefix)) {
186           // <Hi there> If this is being thrown, you probably added an oddly
187
// named static final integer to this class
188
throw new RuntimeException JavaDoc("TCMessageType: Illegal integer field name: " + fName);
189         }
190
191         Integer JavaDoc value = (Integer JavaDoc) field.get(TCMessageType.class);
192
193         intFields.put(fName, value);
194       }
195     }
196
197     for (final Iterator JavaDoc iter = mtFields.values().iterator(); iter.hasNext();) {
198       final Field JavaDoc field = (Field JavaDoc) iter.next();
199       final String JavaDoc name = field.getName();
200       final TCMessageType type = (TCMessageType) field.get(TCMessageType.class);
201
202       final String JavaDoc intName = typePrefix + name;
203       if (!intFields.containsKey(intName)) {
204         // make formatter sane
205
throw new RuntimeException JavaDoc("TCMessageType: Missing " + intName + " integer constant");
206       }
207
208       final int val = ((Integer JavaDoc) intFields.remove(intName)).intValue();
209
210       type.setType(val);
211       type.setTypeName(name);
212
213       Object JavaDoc prev = typeMap.put(type.getType(), type);
214       if (prev != null) {
215         // make formatter sane
216
throw new RuntimeException JavaDoc("TCMessageType: Duplicate message types defined for message number: "
217                                    + type.getType());
218       }
219
220       iter.remove();
221     }
222
223     if (!mtFields.isEmpty()) {
224       // make formatter sane
225
throw new RuntimeException JavaDoc("TCMessageType: internal error - not all message types filled in");
226     }
227
228     if (!intFields.isEmpty()) {
229       String JavaDoc unused = Util.enumerateArray(intFields.keySet().toArray());
230       throw new RuntimeException JavaDoc("TCMessageType: Unused integer constants (please remove): " + unused);
231     }
232
233     final TCMessageType[] rv = new TCMessageType[typeMap.size()];
234     System.arraycopy(typeMap.getValues(), 0, rv, 0, rv.length);
235
236     Arrays.sort(rv, new Comparator JavaDoc() {
237       public int compare(Object JavaDoc o1, Object JavaDoc o2) {
238         int i1 = ((TCMessageType) o1).getType();
239         int i2 = ((TCMessageType) o2).getType();
240
241         if (i1 < i2) {
242           return -1;
243         } else if (i1 == i2) {
244           return 0;
245         } else if (i1 > i2) {
246           return 1;
247         } else {
248           throw new RuntimeException JavaDoc("internal error");
249         }
250       }
251     });
252
253     if (logger.isDebugEnabled()) {
254       logger.debug(Util.enumerateArray(rv));
255     }
256
257     return rv;
258   }
259
260   static {
261     try {
262       allTypes = init();
263     } catch (Exception JavaDoc e) {
264       e.printStackTrace();
265       throw new TCRuntimeException(e);
266     }
267   }
268
269 }
270
Popular Tags