KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > MethodDeclarations


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.marshall;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.GlobalTransaction;
14 import org.jboss.cache.Node;
15 import org.jboss.cache.buddyreplication.BuddyGroup;
16 import org.jboss.cache.lock.NodeLock;
17 import org.jboss.cache.optimistic.DataVersion;
18 import org.jboss.cache.rpc.RpcTreeCache;
19 import org.jgroups.Address;
20 import org.jgroups.stack.IpAddress;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 /**
30  * Class containing Method and Method id definitions as well methods
31  * allowing lookup operations both ways.
32  *
33  * @author <a HREF="galder.zamarreno@jboss.com">Galder Zamarreno</a>
34  * @version $Revision: 1.30 $
35  */

36 public class MethodDeclarations
37 {
38    private static Log log = LogFactory.getLog(MethodDeclarations.class);
39
40    static final Set JavaDoc<Integer JavaDoc> crudMethodIds = new HashSet JavaDoc<Integer JavaDoc>();
41    static final Set JavaDoc<Integer JavaDoc> transactionLifecycleMethodIds = new HashSet JavaDoc<Integer JavaDoc>();
42    static final Set JavaDoc<Integer JavaDoc> buddyGroupOrganisationMethodIds = new HashSet JavaDoc<Integer JavaDoc>();
43    static final Set JavaDoc<Integer JavaDoc> putMethodIds = new HashSet JavaDoc<Integer JavaDoc>();
44
45    // maintain a list of method IDs that correspond to Methods in CacheImpl
46
static final Map JavaDoc<Integer JavaDoc, Method JavaDoc> methods = new HashMap JavaDoc<Integer JavaDoc, Method JavaDoc>();
47
48    // and for reverse lookup
49
static final Map JavaDoc<Method JavaDoc, Integer JavaDoc> methodIds = new HashMap JavaDoc<Method JavaDoc, Integer JavaDoc>();
50
51    public static final Method JavaDoc putDataMethodLocal;
52
53    public static final Method JavaDoc putDataEraseMethodLocal;
54
55    public static final Method JavaDoc putKeyValMethodLocal;
56
57    public static final Method JavaDoc removeNodeMethodLocal;
58
59    public static final Method JavaDoc removeKeyMethodLocal;
60
61    public static final Method JavaDoc removeDataMethodLocal;
62
63    public static final Method JavaDoc evictNodeMethodLocal;
64
65    public static final Method JavaDoc evictVersionedNodeMethodLocal;
66
67    public static final Method JavaDoc prepareMethod;
68
69    public static final Method JavaDoc commitMethod;
70
71    public static final Method JavaDoc rollbackMethod;
72
73    public static final Method JavaDoc replicateMethod;
74
75    public static final Method JavaDoc replicateAllMethod;
76
77    public static final Method JavaDoc addChildMethodLocal;
78
79    public static final Method JavaDoc getKeyValueMethodLocal;
80
81    public static final Method JavaDoc getNodeMethodLocal;
82
83    public static final Method JavaDoc getKeysMethodLocal;
84
85    public static final Method JavaDoc getChildrenNamesMethodLocal;
86
87    public static final Method JavaDoc getDataMapMethodLocal;
88
89    public static final Method JavaDoc existsMethod;
90
91    public static final Method JavaDoc releaseAllLocksMethodLocal;
92
93    public static final Method JavaDoc printMethodLocal;
94
95    public static final Method JavaDoc lockMethodLocal;
96
97    public static final Method JavaDoc optimisticPrepareMethod;
98
99    public static final Method JavaDoc clusteredGetMethod;
100
101    public static final Method JavaDoc remoteAssignToBuddyGroupMethod;
102
103    public static final Method JavaDoc remoteRemoveFromBuddyGroupMethod;
104
105    public static final Method JavaDoc remoteAnnounceBuddyPoolNameMethod;
106
107    public static final Method JavaDoc dataGravitationCleanupMethod;
108
109    public static final Method JavaDoc dataGravitationMethod;
110
111    public static final Method JavaDoc moveMethodLocal;
112
113    public static final Method JavaDoc blockChannelLocal;
114
115    public static final Method JavaDoc unblockChannelLocal;
116
117    // these are basic crud methods that are version-aware - JBCACHE-843.
118

119    public static final Method JavaDoc putDataVersionedMethodLocal;
120
121    public static final Method JavaDoc putDataEraseVersionedMethodLocal;
122
123    public static final Method JavaDoc putKeyValVersionedMethodLocal;
124
125    public static final Method JavaDoc removeNodeVersionedMethodLocal;
126
127    public static final Method JavaDoc removeKeyVersionedMethodLocal;
128
129    public static final Method JavaDoc removeDataVersionedMethodLocal;
130
131    //not all of these are used for RPC - trim accordingly.
132
public static final int putDataMethodLocal_id = 1;
133
134    public static final int putDataEraseMethodLocal_id = 2;
135
136    public static final int putKeyValMethodLocal_id = 3;
137
138    // public static final int putFailFastKeyValueMethodLocal_id = 4; // this method is no longer needed.
139

140    public static final int removeNodeMethodLocal_id = 5;
141
142    public static final int removeKeyMethodLocal_id = 6;
143
144    public static final int removeDataMethodLocal_id = 7;
145
146    public static final int evictNodeMethodLocal_id = 8;
147
148    public static final int evictVersionedNodeMethodLocal_id = 9;
149
150    public static final int prepareMethod_id = 10;
151
152    public static final int commitMethod_id = 11;
153
154    public static final int rollbackMethod_id = 12;
155
156    public static final int replicateMethod_id = 13;
157
158    public static final int replicateAllMethod_id = 14;
159
160    public static final int addChildMethodLocal_id = 15;
161
162    public static final int existsMethod_id = 16;
163
164    public static final int releaseAllLocksMethodLocal_id = 17;
165
166    public static final int optimisticPrepareMethod_id = 18;
167
168    public static final int getPartialStateMethod_id = 19;
169
170    public static final int clusteredGetMethod_id = 22;
171
172    public static final int getChildrenNamesMethodLocal_id = 23;
173
174    public static final int getDataMapMethodLocal_id = 24;
175
176    public static final int getKeysMethodLocal_id = 25;
177
178    public static final int getKeyValueMethodLocal_id = 26;
179
180    public static final int dispatchRpcCallMethod_id = 27;
181
182    public static final int remoteAnnounceBuddyPoolNameMethod_id = 28;
183
184    public static final int remoteAssignToBuddyGroupMethod_id = 29;
185
186    public static final int remoteRemoveFromBuddyGroupMethod_id = 30;
187
188    /* Method id added as they did not exist before refactoring */
189    public static final int getNodeMethodLocal_id = 31;
190
191    public static final int printMethodLocal_id = 32;
192
193    public static final int lockMethodLocal_id = 33;
194
195    public static final int dataGravitationCleanupMethod_id = 34;
196
197    public static final int dataGravitationMethod_id = 35;
198
199    public static final int moveMethodLocal_id = 36;
200
201    // these are basic crud methods that are version-aware - JBCACHE-843.
202

203    public static final int putDataVersionedMethodLocal_id = 37;
204
205    public static final int putDataEraseVersionedMethodLocal_id = 38;
206
207    public static final int putKeyValVersionedMethodLocal_id = 39;
208
209    public static final int removeNodeVersionedMethodLocal_id = 40;
210
211    public static final int removeKeyVersionedMethodLocal_id = 41;
212
213    public static final int removeDataVersionedMethodLocal_id = 42;
214
215    public static final int blockChannelMethodLocal_id = 43;
216
217    public static final int unblockChannelMethodLocal_id = 44;
218
219
220    static
221    {
222       try
223       {
224          getDataMapMethodLocal = CacheImpl.class.getDeclaredMethod("_getData", Fqn.class);
225          existsMethod = CacheImpl.class.getDeclaredMethod("exists", Fqn.class);
226          putDataMethodLocal = CacheImpl.class.getDeclaredMethod("_put", GlobalTransaction.class, Fqn.class, Map JavaDoc.class, boolean.class);
227          putDataEraseMethodLocal = CacheImpl.class.getDeclaredMethod("_put", GlobalTransaction.class, Fqn.class, Map JavaDoc.class, boolean.class, boolean.class);
228          putKeyValMethodLocal = CacheImpl.class.getDeclaredMethod("_put", GlobalTransaction.class, Fqn.class, Object JavaDoc.class, Object JavaDoc.class, boolean.class);
229          removeNodeMethodLocal = CacheImpl.class.getDeclaredMethod("_remove", GlobalTransaction.class, Fqn.class, boolean.class);
230          removeKeyMethodLocal = CacheImpl.class.getDeclaredMethod("_remove", GlobalTransaction.class, Fqn.class, Object JavaDoc.class, boolean.class);
231          removeDataMethodLocal = CacheImpl.class.getDeclaredMethod("_removeData", GlobalTransaction.class, Fqn.class, boolean.class);
232          evictNodeMethodLocal = CacheImpl.class.getDeclaredMethod("_evict", Fqn.class);
233          evictVersionedNodeMethodLocal = CacheImpl.class.getDeclaredMethod("_evict", Fqn.class, DataVersion.class);
234
235          // evictKeyValueMethodLocal=CacheImpl.class.getDeclaredMethod("_evict", new Class[]{Fqn.class, Object.class});
236
prepareMethod = CacheImpl.class.getDeclaredMethod("prepare", GlobalTransaction.class, List JavaDoc.class, Address.class, boolean.class);
237          commitMethod = CacheImpl.class.getDeclaredMethod("commit", GlobalTransaction.class);
238          rollbackMethod = CacheImpl.class.getDeclaredMethod("rollback", GlobalTransaction.class);
239          addChildMethodLocal = CacheImpl.class.getDeclaredMethod("_addChild", GlobalTransaction.class, Fqn.class, Object JavaDoc.class, Node.class, boolean.class);
240          getKeyValueMethodLocal = CacheImpl.class.getDeclaredMethod("_get", Fqn.class, Object JavaDoc.class, boolean.class);
241          getNodeMethodLocal = CacheImpl.class.getDeclaredMethod("_get", Fqn.class);
242          getKeysMethodLocal = CacheImpl.class.getDeclaredMethod("_getKeys", Fqn.class);
243          getChildrenNamesMethodLocal = CacheImpl.class.getDeclaredMethod("_getChildrenNames", Fqn.class);
244          replicateMethod = CacheImpl.class.getDeclaredMethod("_replicate", MethodCall.class);
245          replicateAllMethod = CacheImpl.class.getDeclaredMethod("_replicate", List JavaDoc.class);
246          releaseAllLocksMethodLocal = CacheImpl.class.getDeclaredMethod("_releaseAllLocks", Fqn.class);
247          printMethodLocal = CacheImpl.class.getDeclaredMethod("_print", Fqn.class);
248          lockMethodLocal = CacheImpl.class.getDeclaredMethod("_lock", Fqn.class, NodeLock.LockType.class, boolean.class);
249
250          optimisticPrepareMethod = CacheImpl.class.getDeclaredMethod("optimisticPrepare", GlobalTransaction.class, List JavaDoc.class, Map JavaDoc.class, Address.class, boolean.class);
251
252          clusteredGetMethod = CacheImpl.class.getDeclaredMethod("_clusteredGet", MethodCall.class, Boolean JavaDoc.class);
253
254          // ------------ buddy replication
255

256          remoteAnnounceBuddyPoolNameMethod = CacheImpl.class.getDeclaredMethod("_remoteAnnounceBuddyPoolName", IpAddress.class, String JavaDoc.class);
257          remoteRemoveFromBuddyGroupMethod = CacheImpl.class.getDeclaredMethod("_remoteRemoveFromBuddyGroup", String JavaDoc.class);
258          remoteAssignToBuddyGroupMethod = CacheImpl.class.getDeclaredMethod("_remoteAssignToBuddyGroup", BuddyGroup.class, Map JavaDoc.class);
259
260          dataGravitationCleanupMethod = CacheImpl.class.getDeclaredMethod("_dataGravitationCleanup", GlobalTransaction.class, Fqn.class, Fqn.class);
261          dataGravitationMethod = CacheImpl.class.getDeclaredMethod("_gravitateData", Fqn.class, boolean.class, boolean.class);
262
263          // ------------ move() api
264
moveMethodLocal = CacheImpl.class.getDeclaredMethod("_move", Fqn.class, Fqn.class);
265
266          // ------------ Channel BLOCK event
267
blockChannelLocal = CacheImpl.class.getDeclaredMethod("_block");
268          unblockChannelLocal = CacheImpl.class.getDeclaredMethod("_unblock");
269
270          // ------------ version-aware methods - see JBCACHE-843
271
putDataVersionedMethodLocal = CacheImpl.class.getDeclaredMethod("_put", GlobalTransaction.class, Fqn.class, Map JavaDoc.class, boolean.class, DataVersion.class);
272          putDataEraseVersionedMethodLocal = CacheImpl.class.getDeclaredMethod("_put", GlobalTransaction.class, Fqn.class, Map JavaDoc.class, boolean.class, boolean.class, DataVersion.class);
273          putKeyValVersionedMethodLocal = CacheImpl.class.getDeclaredMethod("_put", GlobalTransaction.class, Fqn.class, Object JavaDoc.class, Object JavaDoc.class, boolean.class, DataVersion.class);
274          removeNodeVersionedMethodLocal = CacheImpl.class.getDeclaredMethod("_remove", GlobalTransaction.class, Fqn.class, boolean.class, DataVersion.class);
275          removeKeyVersionedMethodLocal = CacheImpl.class.getDeclaredMethod("_remove", GlobalTransaction.class, Fqn.class, Object JavaDoc.class, boolean.class, DataVersion.class);
276          removeDataVersionedMethodLocal = CacheImpl.class.getDeclaredMethod("_removeData", GlobalTransaction.class, Fqn.class, boolean.class, DataVersion.class);
277
278       }
279       catch (NoSuchMethodException JavaDoc ex)
280       {
281          ex.printStackTrace();
282          throw new ExceptionInInitializerError JavaDoc(ex.toString());
283       }
284
285       methods.put(putDataMethodLocal_id, putDataMethodLocal);
286       methods.put(putDataEraseMethodLocal_id, putDataEraseMethodLocal);
287       methods.put(putKeyValMethodLocal_id, putKeyValMethodLocal);
288       methods.put(removeNodeMethodLocal_id, removeNodeMethodLocal);
289       methods.put(removeKeyMethodLocal_id, removeKeyMethodLocal);
290       methods.put(removeDataMethodLocal_id, removeDataMethodLocal);
291       methods.put(evictNodeMethodLocal_id, evictNodeMethodLocal);
292       methods.put(evictVersionedNodeMethodLocal_id, evictVersionedNodeMethodLocal);
293       methods.put(prepareMethod_id, prepareMethod);
294       methods.put(commitMethod_id, commitMethod);
295       methods.put(rollbackMethod_id, rollbackMethod);
296       methods.put(replicateMethod_id, replicateMethod);
297       methods.put(replicateAllMethod_id, replicateAllMethod);
298       methods.put(addChildMethodLocal_id, addChildMethodLocal);
299       methods.put(existsMethod_id, existsMethod);
300       methods.put(releaseAllLocksMethodLocal_id, releaseAllLocksMethodLocal);
301       methods.put(optimisticPrepareMethod_id, optimisticPrepareMethod);
302       methods.put(clusteredGetMethod_id, clusteredGetMethod);
303       methods.put(getChildrenNamesMethodLocal_id, getChildrenNamesMethodLocal);
304       methods.put(getDataMapMethodLocal_id, getDataMapMethodLocal);
305       methods.put(getKeysMethodLocal_id, getKeysMethodLocal);
306       methods.put(getKeyValueMethodLocal_id, getKeyValueMethodLocal);
307       methods.put(dispatchRpcCallMethod_id, RpcTreeCache.dispatchRpcCallMethod);
308       methods.put(remoteAnnounceBuddyPoolNameMethod_id, remoteAnnounceBuddyPoolNameMethod);
309       methods.put(remoteAssignToBuddyGroupMethod_id, remoteAssignToBuddyGroupMethod);
310       methods.put(remoteRemoveFromBuddyGroupMethod_id, remoteRemoveFromBuddyGroupMethod);
311       /* Mappings added as they did not exist before refactoring */
312       methods.put(getNodeMethodLocal_id, getNodeMethodLocal);
313       methods.put(printMethodLocal_id, printMethodLocal);
314       methods.put(lockMethodLocal_id, lockMethodLocal);
315
316       methods.put(dataGravitationCleanupMethod_id, dataGravitationCleanupMethod);
317       methods.put(dataGravitationMethod_id, dataGravitationMethod);
318
319       methods.put(moveMethodLocal_id, moveMethodLocal);
320       methods.put(blockChannelMethodLocal_id, blockChannelLocal);
321       methods.put(unblockChannelMethodLocal_id, unblockChannelLocal);
322
323       methods.put(putDataVersionedMethodLocal_id, putDataVersionedMethodLocal);
324       methods.put(putDataEraseVersionedMethodLocal_id, putDataEraseVersionedMethodLocal);
325       methods.put(putKeyValVersionedMethodLocal_id, putKeyValVersionedMethodLocal);
326       methods.put(removeDataVersionedMethodLocal_id, removeDataVersionedMethodLocal);
327       methods.put(removeKeyVersionedMethodLocal_id, removeKeyVersionedMethodLocal);
328       methods.put(removeNodeVersionedMethodLocal_id, removeNodeVersionedMethodLocal);
329
330       for (Integer JavaDoc id : methods.keySet())
331       {
332          methodIds.put(methods.get(id), id);
333       }
334
335
336       putMethodIds.add(putDataMethodLocal_id);
337       putMethodIds.add(putDataEraseMethodLocal_id);
338       putMethodIds.add(putKeyValMethodLocal_id);
339       putMethodIds.add(putDataEraseVersionedMethodLocal_id);
340       putMethodIds.add(putDataVersionedMethodLocal_id);
341       putMethodIds.add(putKeyValVersionedMethodLocal_id);
342
343       crudMethodIds.addAll(putMethodIds);
344       crudMethodIds.add(removeNodeMethodLocal_id);
345       crudMethodIds.add(removeKeyMethodLocal_id);
346       crudMethodIds.add(removeDataMethodLocal_id);
347       crudMethodIds.add(dataGravitationCleanupMethod_id);
348       crudMethodIds.add(moveMethodLocal_id);
349       crudMethodIds.add(putDataVersionedMethodLocal_id);
350       crudMethodIds.add(putDataEraseVersionedMethodLocal_id);
351       crudMethodIds.add(putKeyValVersionedMethodLocal_id);
352       crudMethodIds.add(removeNodeVersionedMethodLocal_id);
353       crudMethodIds.add(removeKeyVersionedMethodLocal_id);
354       crudMethodIds.add(removeDataVersionedMethodLocal_id);
355
356
357       transactionLifecycleMethodIds.add(commitMethod_id);
358       transactionLifecycleMethodIds.add(rollbackMethod_id);
359       transactionLifecycleMethodIds.add(prepareMethod_id);
360       transactionLifecycleMethodIds.add(optimisticPrepareMethod_id);
361
362       buddyGroupOrganisationMethodIds.add(remoteAnnounceBuddyPoolNameMethod_id);
363       buddyGroupOrganisationMethodIds.add(remoteAssignToBuddyGroupMethod_id);
364       buddyGroupOrganisationMethodIds.add(remoteRemoveFromBuddyGroupMethod_id);
365
366    }
367
368    protected static int lookupMethodId(Method JavaDoc method)
369    {
370       Integer JavaDoc methodIdInteger = methodIds.get(method);
371       int methodId = -1;
372
373       if (methodIdInteger != null)
374       {
375          methodId = methodIdInteger;
376       }
377       else
378       {
379          if (log.isWarnEnabled())
380          {
381             log.warn("Method " + method + " is not registered with " + CacheMarshaller200.class);
382          }
383       }
384
385       return methodId;
386    }
387
388    protected static Method JavaDoc lookupMethod(int id)
389    {
390       Method JavaDoc method = methods.get(id);
391       if (method == null)
392       {
393          if (log.isErrorEnabled())
394          {
395             log.error("Method id " + id + " is not registered");
396          }
397          throw new RuntimeException JavaDoc("Method id " + id + " is not registered with " + CacheMarshaller200.class);
398       }
399       return method;
400    }
401
402    /**
403     * Returns true if the method is a CRUD method.
404     */

405    public static boolean isCrudMethod(int id)
406    {
407       return crudMethodIds.contains(Integer.valueOf(id));
408    }
409
410    public static boolean isTransactionLifecycleMethod(int id)
411    {
412       return transactionLifecycleMethodIds.contains(id);
413    }
414
415    public static boolean isBuddyGroupOrganisationMethod(int id)
416    {
417       return buddyGroupOrganisationMethodIds.contains(id);
418    }
419
420    public static boolean isPutMethod(int id)
421    {
422       return putMethodIds.contains(id);
423    }
424
425    /**
426     * Returns the versioned equivalent of a crud method.
427     */

428    public static Method JavaDoc getVersionedMethod(int methodId)
429    {
430       if (isCrudMethod(methodId))
431       {
432          switch (methodId)
433          {
434             case putDataEraseMethodLocal_id:
435                return putDataEraseVersionedMethodLocal;
436             case putDataMethodLocal_id:
437                return putDataVersionedMethodLocal;
438             case putKeyValMethodLocal_id:
439                return putKeyValVersionedMethodLocal;
440             case removeDataMethodLocal_id:
441                return removeDataVersionedMethodLocal;
442             case removeKeyMethodLocal_id:
443                return removeKeyVersionedMethodLocal;
444             case removeNodeMethodLocal_id:
445                return removeNodeVersionedMethodLocal;
446             case moveMethodLocal_id:
447                return moveMethodLocal;
448             default:
449                throw new RuntimeException JavaDoc("Unrecognised method id " + methodId);
450          }
451       }
452       else
453       {
454          throw new RuntimeException JavaDoc("Attempting to look up a versioned equivalent of a non-crud method");
455       }
456    }
457
458    /**
459     * Counterpart to {@link #getVersionedMethod(int)}
460     */

461    public static Method JavaDoc getUnversionedMethod(int methodId)
462    {
463       if (isCrudMethod(methodId))
464       {
465          switch (methodId)
466          {
467             case putDataEraseVersionedMethodLocal_id:
468                return putDataEraseMethodLocal;
469             case putDataVersionedMethodLocal_id:
470                return putDataMethodLocal;
471             case putKeyValVersionedMethodLocal_id:
472                return putKeyValMethodLocal;
473             case removeDataVersionedMethodLocal_id:
474                return removeDataMethodLocal;
475             case removeKeyVersionedMethodLocal_id:
476                return removeKeyMethodLocal;
477             case removeNodeVersionedMethodLocal_id:
478                return removeNodeMethodLocal;
479             case moveMethodLocal_id:
480                return moveMethodLocal;
481             default:
482                throw new RuntimeException JavaDoc("Unrecognised method id " + methodId);
483          }
484       }
485       else
486       {
487          throw new RuntimeException JavaDoc("Attempting to look up a versioned equivalent of a non-crud method");
488       }
489    }
490
491
492    public static boolean isDataGravitationMethod(int methodId)
493    {
494       return methodId == MethodDeclarations.dataGravitationCleanupMethod_id || methodId == MethodDeclarations.dataGravitationMethod_id;
495    }
496 }
497
498
Popular Tags