KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.interceptors;
2
3 import org.jboss.cache.InvocationContext;
4 import org.jboss.cache.TransactionEntry;
5 import org.jboss.cache.marshall.MethodCall;
6 import org.jboss.cache.marshall.MethodDeclarations;
7 import org.jboss.cache.notifications.Notifier;
8
9 import java.util.List JavaDoc;
10
11 /**
12  * The interceptor in charge of firing off notifications to cache listeners
13  *
14  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
15  * @since 2.0.0
16  */

17 public class NotificationInterceptor extends BaseTransactionalContextInterceptor
18 {
19    public Object JavaDoc invoke(MethodCall call) throws Throwable JavaDoc
20    {
21       // should only kick in as a call returns.
22
Object JavaDoc retval = super.invoke(call);
23
24       InvocationContext ctx = cache.getInvocationContext();
25       Notifier n = cache.getNotifier();
26       // now, if we are in a transactional context, copy all local-context invocations to the transaction entry.
27
if (ctx.getGlobalTransaction() != null)
28       {
29          if (MethodDeclarations.isTransactionLifecycleMethod(call.getMethodId()))
30          {
31             // only invoke on commit.
32
if (call.getMethodId() == MethodDeclarations.commitMethod_id)
33             {
34                // invoke notifications
35
List JavaDoc<MethodCall> events = ctx.getCacheListenerEvents();
36                TransactionEntry entry = txTable.get(ctx.getGlobalTransaction());
37                if (entry != null)
38                {
39                   entry.addCacheListenerEvents(events);
40
41                   if (log.isTraceEnabled())
42                      log.trace("Invoking notifications for this transaction. Notification set: " + entry.getCacheListenerEvents());
43                   n.invokeQueuedNotifications(entry.getCacheListenerEvents());
44                }
45                else
46                {
47                   log.trace("Committing but cache entry is null.");
48                }
49             }
50          }
51          else
52          {
53             // a "normal" method in the transaction. Add notifications to transaction entry.
54
List JavaDoc<MethodCall> events = ctx.getCacheListenerEvents();
55             TransactionEntry entry = txTable.get(ctx.getGlobalTransaction());
56             if (log.isTraceEnabled()) log.trace("Adding " + events + " to transaction entry's notification list");
57             entry.addCacheListenerEvents(events);
58             ctx.clearCacheListenerEvents();
59          }
60       }
61       else
62       {
63          // fire all queued up notifications now.
64
n.invokeQueuedNotifications();
65       }
66
67       return retval;
68    }
69 }
70
Popular Tags