1 22 package org.jboss.ejb.plugins.cmp.jdbc2.schema; 23 24 import org.jboss.cache.invalidation.Invalidatable; 25 import org.jboss.cache.invalidation.InvalidationGroup; 26 import org.jboss.logging.Logger; 27 28 import javax.transaction.TransactionManager ; 29 import javax.transaction.Transaction ; 30 import javax.transaction.SystemException ; 31 import java.io.Serializable ; 32 33 37 public class CacheInvalidator 38 implements Invalidatable 39 { 40 private static final Logger log = Logger.getLogger(CacheInvalidator.class); 41 42 private final Cache cache; 43 private final TransactionManager tm; 44 private final InvalidationGroup group; 45 46 public CacheInvalidator(Cache cache, TransactionManager tm, InvalidationGroup group) 47 { 48 this.cache = cache; 49 this.tm = tm; 50 this.group = group; 51 group.register(this); 52 log.debug("registered to group " + group.getGroupName()); 53 } 54 55 public void unregister() 56 { 57 group.unregister(this); 58 log.debug("unregistered from group " + group.getGroupName()); 59 } 60 61 public void isInvalid(Serializable key) 62 { 63 Transaction tx = null; 64 try 65 { 66 tx = tm.getTransaction(); 67 } 68 catch(SystemException e) 69 { 70 log.error("Failed to obtain the current transaction", e); 71 throw new IllegalStateException ("Failed to obtain the current transaction: " + e.getMessage()); 72 } 73 74 if(log.isTraceEnabled()) 75 { 76 log.trace("invalidating key=" + key); 77 } 78 79 cache.lock(key); 80 try 81 { 82 cache.remove(tx, key); 83 } 84 catch(Cache.RemoveException e) 85 { 86 if(log.isTraceEnabled()) 87 { 88 log.trace(e.getMessage()); 89 } 90 } 91 finally 92 { 93 cache.unlock(key); 94 } 95 } 96 97 public void areInvalid(Serializable [] keys) 98 { 99 Transaction tx = null; 100 try 101 { 102 tx = tm.getTransaction(); 103 } 104 catch(SystemException e) 105 { 106 log.error("Failed to obtain the current transaction", e); 107 throw new IllegalStateException ("Failed to obtain the current transaction: " + e.getMessage()); 108 } 109 110 boolean trace = log.isTraceEnabled(); 111 for(int i = 0; i < keys.length; ++i) 112 { 113 if(trace) 114 { 115 log.trace("invalidating key[" + i + "]=" + keys[i]); 116 } 117 118 cache.lock(); 119 try 120 { 121 cache.remove(tx, keys[i]); 122 } 123 catch(Cache.RemoveException e) 124 { 125 if(trace) 126 { 127 log.trace(e.getMessage()); 128 } 129 } 130 finally 131 { 132 cache.unlock(); 133 } 134 } 135 } 136 137 public void invalidateAll() 138 { 139 cache.lock(); 140 try 141 { 142 cache.flush(); 143 } 144 finally 145 { 146 cache.unlock(); 147 } 148 } 149 } 150 | Popular Tags |