KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.interceptors;
2
3 import org.jboss.cache.CacheSPI;
4 import org.jboss.cache.InvocationContext;
5 import org.jboss.cache.lock.IdentityLock;
6 import org.jboss.cache.marshall.MethodCall;
7
8 import javax.transaction.Transaction JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.ListIterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 /**
14  * When a call returns, unlocks all locks held by the current thread in the
15  * LockTable. This is a no-op if a transaction is used.
16  *
17  * @author Bela Ban
18  * @version $Id: UnlockInterceptor.java,v 1.14 2006/08/25 14:10:07 msurtani Exp $
19  */

20 public class UnlockInterceptor extends Interceptor {
21
22    Map JavaDoc lock_table = null;
23    boolean trace = log.isTraceEnabled();
24
25    public void setCache(CacheSPI cache)
26    {
27       super.setCache(cache);
28       lock_table = cache.getLockTable();
29    }
30
31    public Object JavaDoc invoke(MethodCall m) throws Throwable JavaDoc {
32       try {
33          return super.invoke(m);
34       }
35       finally
36       {
37          InvocationContext ctx = cache.getInvocationContext();
38          if (ctx.getOptionOverrides() == null || !ctx.getOptionOverrides().isSuppressLocking())
39          {
40              Transaction JavaDoc tx = ctx.getTransaction();
41              if (tx != null && isValid(tx))
42              {
43                  // if (trace) log.trace("Do not do anything; we have a transaction running or node locking is optimistic.");
44
}
45              else { // no TX
46
Thread JavaDoc currentThread = Thread.currentThread();
47                 List JavaDoc locks = (List JavaDoc)lock_table.get(currentThread);
48                 if (trace) log.trace("Attempting to release locks on current thread. Lock table is " + lock_table);
49
50                 if (locks != null && locks.size() > 0) {
51                    releaseLocks(locks, currentThread);
52                    lock_table.remove(currentThread);
53                 }
54              }
55          }
56       }
57    }
58
59    private void releaseLocks(List JavaDoc locks, Thread JavaDoc currentThread) {
60       IdentityLock lock;
61       for (ListIterator JavaDoc it=locks.listIterator(locks.size()); it.hasPrevious();) {
62             lock=(IdentityLock)it.previous();
63          if (trace)
64                log.trace("releasing lock for " + lock.getFqn() + ": " + lock);
65          lock.release(currentThread);
66       }
67    }
68
69
70 }
71
Popular Tags