1 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 ; 15 import java.lang.reflect.Modifier ; 16 import java.util.Arrays ; 17 import java.util.Comparator ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 25 26 public final class TCMessageType { 27 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 getTypeName() { 97 return typeName; 98 } 99 100 public String toString() { 101 return typeName + " (" + type + ")"; 102 } 103 104 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 typePrefix = "TYPE_"; 113 114 private int type; 115 private String typeName; 116 117 private TCMessageType() { 118 } 120 121 private void setType(int type) { 122 this.type = type; 123 } 124 125 private void setTypeName(String typeName) { 126 this.typeName = typeName; 127 } 128 129 public int hashCode() { 130 return this.typeName.hashCode() + type; 131 } 132 133 public boolean equals(Object 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 147 private static TCMessageType[] init() throws IllegalArgumentException , IllegalAccessException { 148 Field [] 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 field = fields[i]; 155 final String fName = field.getName(); 156 157 int modifiers = field.getModifiers(); 158 159 if (Modifier.isPublic(modifiers) && !Modifier.isFinal(modifiers)) { throw new RuntimeException ( 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 throw new RuntimeException ("TCMessageType: Message type names must be all UPPER CASE: " + fName); 176 } 177 178 if (fName.startsWith("_")) { 179 throw new RuntimeException ("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 throw new RuntimeException ("TCMessageType: Illegal integer field name: " + fName); 189 } 190 191 Integer value = (Integer ) field.get(TCMessageType.class); 192 193 intFields.put(fName, value); 194 } 195 } 196 197 for (final Iterator iter = mtFields.values().iterator(); iter.hasNext();) { 198 final Field field = (Field ) iter.next(); 199 final String name = field.getName(); 200 final TCMessageType type = (TCMessageType) field.get(TCMessageType.class); 201 202 final String intName = typePrefix + name; 203 if (!intFields.containsKey(intName)) { 204 throw new RuntimeException ("TCMessageType: Missing " + intName + " integer constant"); 206 } 207 208 final int val = ((Integer ) intFields.remove(intName)).intValue(); 209 210 type.setType(val); 211 type.setTypeName(name); 212 213 Object prev = typeMap.put(type.getType(), type); 214 if (prev != null) { 215 throw new RuntimeException ("TCMessageType: Duplicate message types defined for message number: " 217 + type.getType()); 218 } 219 220 iter.remove(); 221 } 222 223 if (!mtFields.isEmpty()) { 224 throw new RuntimeException ("TCMessageType: internal error - not all message types filled in"); 226 } 227 228 if (!intFields.isEmpty()) { 229 String unused = Util.enumerateArray(intFields.keySet().toArray()); 230 throw new RuntimeException ("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 () { 237 public int compare(Object o1, Object 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 ("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 e) { 264 e.printStackTrace(); 265 throw new TCRuntimeException(e); 266 } 267 } 268 269 } 270 | Popular Tags |