/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.interceptor;

import java.io.Serializable;

import org.springframework.aop.framework.AopConfigException;

/**
 * Rule determining whether or not a given exception (and any subclasses) should
 * cause a rollback. Multiple such rules can be applied to determine whether a
 * transaction should commit or rollback after an exception has been thrown.
 *
 * @author Rod Johnson
 * @since 09.04.2003
 * @see NoRollbackRuleAttribute
 */
public class RollbackRuleAttribute implements Serializable{
	
	public static final RollbackRuleAttribute ROLLBACK_ON_RUNTIME_EXCEPTIONS =
			new RollbackRuleAttribute(RuntimeException.class);
	
	/**
	 * Could hold exception, resolving class name but would always require FQN.
	 * This way does multiple string comparisons, but how often do we decide
	 * whether to roll back a transaction following an exception?
	 */
	private final String exceptionName;
	
	/**
	 * Preferred way to construct a RollbackRule, matching the exception class and
	 * subclasses. The exception class must be Throwable or a subclass of Throwable.
	 * @param clazz throwable class
	 */
	public RollbackRuleAttribute(Class clazz) {
		if (!Throwable.class.isAssignableFrom(clazz)) {
			throw new AopConfigException("Cannot construct rollback rule from [" + clazz.getName() +
					"]; it's not a Throwable");
		}
		this.exceptionName = clazz.getName();
	}

	/**
	 * Construct a new RollbackRule for the given exception name.
	 * This can be a substring, with no wildcard support at present.
	 * A value of "ServletException" would match
	 * <code>javax.servlet.ServletException</code> and subclasses, for example.
	 * <p><b>NB:</b> Consider carefully how specific the pattern is, and whether
	 * to include package information (which isn't mandatory). For example,
	 * "Exception" will match nearly anything, and will probably hide other rules.
	 * "java.lang.Exception" would be correct if "Exception" was meant to define
	 * a rule for all checked exceptions. With more unusual exception names such
	 * as "BaseBusinessException" there's no need to use a FQN.
	 * @param exceptionName the exception pattern
	 * (can also be a fully qualified class name)
	 */
	public RollbackRuleAttribute(String exceptionName) {
		this.exceptionName = exceptionName;
	}

	/**
	 * Return the pattern for the exception name.
	 */
	public String getExceptionName() {
		return exceptionName;
	}

	/**
	 * Return the depth to the superclass matching.
	 * 0 means ex matches exactly. Returns -1 if there's no match.
	 * Otherwise, returns depth. Lowest depth wins.
	 */
	public int getDepth(Throwable ex) {
		return getDepth(ex.getClass(), 0);
	}

	private int getDepth(Class exceptionClass, int depth) {
		if (exceptionClass.getName().indexOf(this.exceptionName) != -1) {
			// Found it!
			return depth;
		}
		// If we've gone as far as we can go and haven't found it...
		if (exceptionClass.equals(Throwable.class)) {
			return -1;
		}
		return getDepth(exceptionClass.getSuperclass(), depth + 1);
	}
	
	public String toString() {
		return "RollbackRule with pattern [" + this.exceptionName + "]";
	}
	
	public boolean equals(Object o) {
		if (!(o instanceof RollbackRuleAttribute)) {
			return false;
		}

		RollbackRuleAttribute rhs = (RollbackRuleAttribute) o;
		
		// no possibility of null
		return this.exceptionName.equals(rhs.exceptionName);
	}
	
	public int hashCode() {
		return this.exceptionName.hashCode();
	}
	
}

@@ -21,9 +21,9 @@
 -200
-5
+6
  the
@@ -53,17 +53,16 @@
 hors.%0A *
- 
 %0A * Lice
@@ -234,17 +234,16 @@
 se at%0A *
- 
 %0A *     
@@ -288,17 +288,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -703,70 +703,8 @@
 e;%0A%0A
-import org.springframework.aop.framework.AopConfigException;%0A%0A
 /**%0A
@@ -1215,17 +1215,17 @@
 class);%0A
-%09
+%0A
 %0A%09/**%0A%09 
@@ -1478,17 +1478,17 @@
 onName;%0A
-%09
+%0A
 %0A%09/**%0A%09 
@@ -1797,17 +1797,23 @@
 new 
-AopConfig
+IllegalArgument
 Exce
@@ -1818,16 +1818,22 @@
 ception(
+%0A%09%09%09%09%09
 %22Cannot 
@@ -1887,17 +1887,12 @@
 () +
-%0A%09%09%09%09%09%22%5D;
+ %22%5D:
  it'
@@ -2852,32 +2852,33 @@
 eptionName;%0A%09%7D%0A%0A
+%0A
 %09/**%0A%09 * Return 
@@ -3584,25 +3584,25 @@
 th + 1);%0A%09%7D%0A
-%09
+%0A
 %0A%09public Str
@@ -3602,134 +3602,81 @@
 lic 
-String toString() %7B%0A%09%09return %22RollbackRule with pattern %5B%22 + this.exceptionName + %22%5D%22;%0A%09%7D%0A%09%0A%09public boolean equals(Object o) %7B
+boolean equals(Object other) %7B%0A%09%09if (this == other) %7B%0A%09%09%09return true;%0A%09%09%7D
 %0A%09%09i
@@ -3681,16 +3681,20 @@
 %09if (!(o
+ther
  instanc
@@ -3744,17 +3744,16 @@
 se;%0A%09%09%7D%0A
-%0A
 %09%09Rollba
@@ -3803,40 +3803,13 @@
 e) o
-;%0A%09%09%0A%09%09// no possibility of null
+ther;
 %0A%09%09r
@@ -3933,12 +3933,119 @@
 e();%0A%09%7D%0A
-%09
+%0A%09public String toString() %7B%0A%09%09return %22RollbackRuleAttribute with pattern %5B%22 + this.exceptionName + %22%5D%22;%0A%09%7D%0A
 %0A%7D%0A

throw IllegalArgumentException instead of AopConfigException