1 22 package org.jboss.cache.interceptors; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.jboss.cache.CacheSPI; 27 import org.jboss.cache.config.Configuration; 28 import org.jboss.cache.marshall.MethodCall; 29 import org.jboss.cache.marshall.MethodDeclarations; 30 31 import javax.transaction.Status ; 32 import javax.transaction.SystemException ; 33 import javax.transaction.Transaction ; 34 import java.util.Collections ; 35 import java.util.Map ; 36 37 44 public abstract class Interceptor implements InterceptorMBean 45 { 46 protected Interceptor next = null, last = null; 47 protected CacheSPI cache; 48 protected Log log = null; 49 protected Configuration configuration; 50 private boolean statsEnabled = false; 51 52 public Interceptor() 53 { 54 log = LogFactory.getLog(getClass()); 55 } 56 57 public void setNext(Interceptor i) 58 { 59 next = i; 60 } 61 62 public Interceptor getNext() 63 { 64 return next; 65 } 66 67 public void setCache(CacheSPI cache) 68 { 69 this.cache = cache; 70 this.configuration = cache.getConfiguration(); 71 } 72 73 public Object invoke(MethodCall m) throws Throwable 74 { 75 return next.invoke(m); 76 } 77 78 public boolean getStatisticsEnabled() 79 { 80 return statsEnabled; 81 } 82 83 public void setStatisticsEnabled(boolean enabled) 84 { 85 statsEnabled = enabled; 86 } 87 88 public Interceptor getLast() 89 { 90 return last; 91 } 92 93 public void setLast(Interceptor last) 94 { 95 this.last = last; 96 } 97 98 102 public Map <String , Object > dumpStatistics() 103 { 104 return Collections.emptyMap(); 105 } 106 107 111 public void resetStatistics() 112 { 113 } 114 115 118 protected boolean isActive(Transaction tx) 119 { 120 if (tx == null) return false; 121 int status = -1; 122 try 123 { 124 status = tx.getStatus(); 125 return status == Status.STATUS_ACTIVE; 126 } 127 catch (SystemException e) 128 { 129 log.error("failed getting transaction status", e); 130 return false; 131 } 132 } 133 134 137 protected boolean isPreparing(Transaction tx) 138 { 139 if (tx == null) return false; 140 int status = -1; 141 try 142 { 143 status = tx.getStatus(); 144 return status == Status.STATUS_PREPARING; 145 } 146 catch (SystemException e) 147 { 148 log.error("failed getting transaction status", e); 149 return false; 150 } 151 } 152 153 159 protected boolean isValid(Transaction tx) 160 { 161 return isActive(tx) || isPreparing(tx); 162 } 163 164 169 protected boolean isOnePhaseCommitPrepareMehod(MethodCall m) 170 { 171 switch (m.getMethodId()) 172 { 173 case MethodDeclarations.prepareMethod_id: 174 return (Boolean ) m.getArgs()[3]; 175 case MethodDeclarations.optimisticPrepareMethod_id: 176 return (Boolean ) m.getArgs()[4]; 177 default: 178 return false; 179 } 180 } 181 182 public String toString() 183 { 184 return getClass() 185 + "{next: " 186 + (getNext() == null ? null : getNext().getClass()) 187 + "; last: " 188 + (getLast() == null ? null : getLast().getClass()) 189 + "}"; 190 } 191 } 192 | Popular Tags |