1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.io.Serializable ; 20 import java.lang.reflect.Method ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.util.ObjectUtils; 30 import org.springframework.util.PatternMatchUtils; 31 32 41 public class NameMatchTransactionAttributeSource implements TransactionAttributeSource, Serializable { 42 43 47 protected static final Log logger = LogFactory.getLog(NameMatchTransactionAttributeSource.class); 48 49 50 private Map nameMap = new HashMap (); 51 52 53 60 public void setNameMap(Map nameMap) { 61 Iterator it = nameMap.entrySet().iterator(); 62 while (it.hasNext()) { 63 Map.Entry entry = (Map.Entry ) it.next(); 64 String name = (String ) entry.getKey(); 65 66 TransactionAttribute attr = null; 68 if (entry.getValue() instanceof TransactionAttribute) { 69 attr = (TransactionAttribute) entry.getValue(); 70 } 71 else { 72 TransactionAttributeEditor editor = new TransactionAttributeEditor(); 73 editor.setAsText(entry.getValue().toString()); 74 attr = (TransactionAttribute) editor.getValue(); 75 } 76 77 addTransactionalMethod(name, attr); 78 } 79 } 80 81 88 public void setProperties(Properties transactionAttributes) { 89 TransactionAttributeEditor tae = new TransactionAttributeEditor(); 90 for (Iterator it = transactionAttributes.keySet().iterator(); it.hasNext(); ) { 91 String methodName = (String ) it.next(); 92 String value = transactionAttributes.getProperty(methodName); 93 tae.setAsText(value); 94 TransactionAttribute attr = (TransactionAttribute) tae.getValue(); 95 addTransactionalMethod(methodName, attr); 96 } 97 } 98 99 106 public void addTransactionalMethod(String methodName, TransactionAttribute attr) { 107 if (logger.isDebugEnabled()) { 108 logger.debug("Adding transactional method [" + methodName + "] with attribute [" + attr + "]"); 109 } 110 this.nameMap.put(methodName, attr); 111 } 112 113 114 public TransactionAttribute getTransactionAttribute(Method method, Class targetClass) { 115 String methodName = method.getName(); 117 TransactionAttribute attr = (TransactionAttribute) this.nameMap.get(methodName); 118 119 if (attr == null) { 120 String bestNameMatch = null; 122 for (Iterator it = this.nameMap.keySet().iterator(); it.hasNext();) { 123 String mappedName = (String ) it.next(); 124 if (isMatch(methodName, mappedName) && 125 (bestNameMatch == null || bestNameMatch.length() <= mappedName.length())) { 126 attr = (TransactionAttribute) this.nameMap.get(mappedName); 127 bestNameMatch = mappedName; 128 } 129 } 130 } 131 132 return attr; 133 } 134 135 144 protected boolean isMatch(String methodName, String mappedName) { 145 return PatternMatchUtils.simpleMatch(mappedName, methodName); 146 } 147 148 149 public boolean equals(Object other) { 150 if (this == other) { 151 return true; 152 } 153 if (!(other instanceof NameMatchTransactionAttributeSource)) { 154 return false; 155 } 156 NameMatchTransactionAttributeSource otherTas = (NameMatchTransactionAttributeSource) other; 157 return ObjectUtils.nullSafeEquals(this.nameMap, otherTas.nameMap); 158 } 159 160 public int hashCode() { 161 return NameMatchTransactionAttributeSource.class.hashCode(); 162 } 163 164 public String toString() { 165 return getClass().getName() + ": " + this.nameMap; 166 } 167 168 } 169 | Popular Tags |