1 7 package org.jboss.cache.interceptors; 8 9 import org.jboss.cache.CacheSPI; 10 import org.jboss.cache.loader.CacheLoader; 11 12 import java.util.ConcurrentModificationException ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.concurrent.ConcurrentHashMap ; 17 18 23 public class BaseCacheLoaderInterceptor extends Interceptor 24 { 25 protected CacheLoader loader = null; 26 private Map lockMap = new ConcurrentHashMap (); 27 28 public void setCache(CacheSPI cache) 29 { 30 super.setCache(cache); 31 this.loader = cache.getCacheLoaderManager().getCacheLoader(); 32 } 33 34 39 protected void obtainLoaderLock(Object lock) throws InterruptedException 40 { 41 Thread current = Thread.currentThread(); 42 43 synchronized (this) 44 { 45 while (lockMap.containsKey(lock) && !lockMap.get(lock).equals(current)) 46 { 47 this.wait(); 49 } 50 51 if (lockMap.containsKey(lock) && !lockMap.get(lock).equals(current)) 52 { 53 throw new ConcurrentModificationException ("Loader lock " + lock + " is already held by someone else."); 54 } 55 56 lockMap.put(lock, current); 57 } 58 } 59 60 63 protected void releaseLoaderLock(Object lock) 64 { 65 synchronized (this) 66 { 67 lockMap.remove(lock); 68 this.notify(); 69 } 70 } 71 72 protected void releaseLoaderLocks(List locks) 73 { 74 Iterator it = locks.iterator(); 75 while (it.hasNext()) 76 { 77 releaseLoaderLock(it.next()); 78 } 79 } 80 81 protected void obtainLoaderLocks(List locks) throws InterruptedException 82 { 83 Iterator it = locks.iterator(); 84 while (it.hasNext()) 85 { 86 obtainLoaderLock(it.next()); 87 } 88 } 89 } 90 | Popular Tags |