1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.io.Serializable ; 20 21 30 public class RollbackRuleAttribute implements Serializable { 31 32 public static final RollbackRuleAttribute ROLLBACK_ON_RUNTIME_EXCEPTIONS = 33 new RollbackRuleAttribute(RuntimeException .class); 34 35 36 41 private final String exceptionName; 42 43 44 49 public RollbackRuleAttribute(Class clazz) { 50 if (!Throwable .class.isAssignableFrom(clazz)) { 51 throw new IllegalArgumentException ( 52 "Cannot construct rollback rule from [" + clazz.getName() + "]: it's not a Throwable"); 53 } 54 this.exceptionName = clazz.getName(); 55 } 56 57 71 public RollbackRuleAttribute(String exceptionName) { 72 this.exceptionName = exceptionName; 73 } 74 75 76 79 public String getExceptionName() { 80 return exceptionName; 81 } 82 83 88 public int getDepth(Throwable ex) { 89 return getDepth(ex.getClass(), 0); 90 } 91 92 private int getDepth(Class exceptionClass, int depth) { 93 if (exceptionClass.getName().indexOf(this.exceptionName) != -1) { 94 return depth; 96 } 97 if (exceptionClass.equals(Throwable .class)) { 99 return -1; 100 } 101 return getDepth(exceptionClass.getSuperclass(), depth + 1); 102 } 103 104 105 public boolean equals(Object other) { 106 if (this == other) { 107 return true; 108 } 109 if (!(other instanceof RollbackRuleAttribute)) { 110 return false; 111 } 112 RollbackRuleAttribute rhs = (RollbackRuleAttribute) other; 113 return this.exceptionName.equals(rhs.exceptionName); 114 } 115 116 public int hashCode() { 117 return this.exceptionName.hashCode(); 118 } 119 120 public String toString() { 121 return "RollbackRuleAttribute with pattern [" + this.exceptionName + "]"; 122 } 123 124 } 125 | Popular Tags |