KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > BaseRpcInterceptor


1 /**
2  *
3  */

4 package org.jboss.cache.interceptors;
5
6 import org.jboss.cache.CacheSPI;
7 import org.jboss.cache.buddyreplication.BuddyManager;
8 import org.jboss.cache.marshall.MethodCall;
9 import org.jboss.cache.marshall.MethodCallFactory;
10 import org.jboss.cache.marshall.MethodDeclarations;
11 import org.jgroups.Address;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Acts as a base for all RPC calls - subclassed by {@see ReplicationInterceptor} and {@see OptimisticReplicationInterceptor}.
18  *
19  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
20  */

21 public abstract class BaseRpcInterceptor extends Interceptor
22 {
23
24    private BuddyManager buddyManager;
25    private boolean usingBuddyReplication;
26
27    public void setCache(CacheSPI cache)
28    {
29       super.setCache(cache);
30       buddyManager = cache.getBuddyManager();
31       usingBuddyReplication = buddyManager != null;
32    }
33
34    /**
35     * Checks whether any of the responses are exceptions. If yes, re-throws
36     * them (as exceptions or runtime exceptions).
37     *
38     * @param rsps
39     * @throws Throwable
40     */

41    protected void checkResponses(List JavaDoc rsps) throws Throwable JavaDoc
42    {
43       Object JavaDoc rsp;
44       if (rsps != null)
45       {
46          for (Iterator JavaDoc it = rsps.iterator(); it.hasNext();)
47          {
48             rsp = it.next();
49             if (rsp != null && rsp instanceof Throwable JavaDoc)
50             {
51                // lets print a stack trace first.
52
if (log.isDebugEnabled())
53                   log.debug("Received Throwable from remote node", (Throwable JavaDoc) rsp);
54                throw (Throwable JavaDoc) rsp;
55             }
56          }
57       }
58    }
59
60    protected void replicateCall(MethodCall call, boolean sync) throws Throwable JavaDoc
61    {
62       replicateCall(null, call, sync);
63    }
64
65    protected void replicateCall(List JavaDoc<Address> recipients, MethodCall call, boolean sync) throws Throwable JavaDoc
66    {
67
68       if (log.isTraceEnabled()) log.trace("Broadcasting call " + call + " to recipient list " + recipients);
69
70       if (!sync && cache.getRPCManager().getReplicationQueue() != null && !usingBuddyReplication)
71       {
72          putCallOnAsyncReplicationQueue(call);
73       }
74       else
75       {
76          if (usingBuddyReplication) call = buddyManager.transformFqns(call);
77
78          List JavaDoc<Address> callRecipients = recipients;
79          if (callRecipients == null)
80          {
81             callRecipients = usingBuddyReplication ? buddyManager.getBuddyAddresses() : cache.getMembers();
82          }
83
84          List JavaDoc rsps = cache.getRPCManager().callRemoteMethods(callRecipients,
85                  MethodDeclarations.replicateMethod,
86                  new Object JavaDoc[]{call},
87                  sync, // is synchronised?
88
true, // ignore self?
89
configuration.getSyncReplTimeout());
90          if (log.isTraceEnabled())
91          {
92             log.trace("responses=" + rsps);
93          }
94          if (sync) checkResponses(rsps);
95       }
96
97    }
98
99    protected void putCallOnAsyncReplicationQueue(MethodCall call)
100    {
101       if (log.isDebugEnabled()) log.debug("Putting call " + call + " on the replication queue.");
102       cache.getRPCManager().getReplicationQueue().add(MethodCallFactory.create(MethodDeclarations.replicateMethod, call));
103    }
104
105    protected boolean containsModifications(MethodCall m)
106    {
107       switch (m.getMethodId())
108       {
109          case MethodDeclarations.prepareMethod_id:
110          case MethodDeclarations.optimisticPrepareMethod_id:
111             List JavaDoc mods = (List JavaDoc) m.getArgs()[1];
112             return mods.size() > 0;
113          case MethodDeclarations.commitMethod_id:
114          case MethodDeclarations.rollbackMethod_id:
115             return cache.getInvocationContext().isTxHasMods();
116          default:
117             return false;
118       }
119    }
120 }
Popular Tags