Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv27574
Modified Files:
AbstractExceptionHandler.cs ExceptionHandlerAdvice.cs
IExceptionHandler.cs LogExceptionHandler.cs
ReturnValueExceptionHandler.cs SwallowExceptionHandler.cs
TranslationExceptionHandler.cs
Log Message:
SPRNET-742 - Exception handling aspect supports using SpEL expression as filtering condition as alternative to use of exception name.
Index: ReturnValueExceptionHandler.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/ReturnValueExceptionHandler.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ReturnValueExceptionHandler.cs 8 Aug 2007 07:28:07 -0000 1.1
--- ReturnValueExceptionHandler.cs 2 Oct 2007 21:56:53 -0000 1.2
***************
*** 31,34 ****
--- 31,52 ----
public class ReturnValueExceptionHandler : AbstractExceptionHandler
{
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ReturnValueExceptionHandler"/> class.
+ /// </summary>
+ public ReturnValueExceptionHandler()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ReturnValueExceptionHandler"/> class.
+ /// </summary>
+ /// <param name="exceptionNames">The exception names.</param>
+ public ReturnValueExceptionHandler(string[] exceptionNames) : base(exceptionNames)
+ {
+ }
+
+
+
/// <summary>
/// Returns the result of evaluating the translation expression.
***************
*** 37,41 ****
public override object HandleException(IDictionary callContextDictionary)
{
! IExpression expression = Expression.Parse(ExpressionText);
return expression.GetValue(null, callContextDictionary);
}
--- 55,59 ----
public override object HandleException(IDictionary callContextDictionary)
{
! IExpression expression = Expression.Parse(ActionExpressionText);
return expression.GetValue(null, callContextDictionary);
}
Index: TranslationExceptionHandler.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/TranslationExceptionHandler.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** TranslationExceptionHandler.cs 8 Aug 2007 07:28:07 -0000 1.1
--- TranslationExceptionHandler.cs 2 Oct 2007 21:56:53 -0000 1.2
***************
*** 33,36 ****
--- 33,53 ----
{
/// <summary>
+ /// Initializes a new instance of the <see cref="TranslationExceptionHandler"/> class.
+ /// </summary>
+ public TranslationExceptionHandler()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="TranslationExceptionHandler"/> class.
+ /// </summary>
+ /// <param name="exceptionNames">The exception names.</param>
+ public TranslationExceptionHandler(string[] exceptionNames) : base(exceptionNames)
+ {
+ }
+
+
+
+ /// <summary>
/// Handles the exception.
/// </summary>
***************
*** 38,42 ****
public override object HandleException(IDictionary callContextDictionary)
{
! IExpression expression = Expression.Parse(ExpressionText);
object o = expression.GetValue(null, callContextDictionary);
Exception translatedException = o as Exception;
--- 55,59 ----
public override object HandleException(IDictionary callContextDictionary)
{
! IExpression expression = Expression.Parse(ActionExpressionText);
object o = expression.GetValue(null, callContextDictionary);
Exception translatedException = o as Exception;
Index: SwallowExceptionHandler.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/SwallowExceptionHandler.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** SwallowExceptionHandler.cs 8 Aug 2007 07:28:07 -0000 1.1
--- SwallowExceptionHandler.cs 2 Oct 2007 21:56:53 -0000 1.2
***************
*** 31,34 ****
--- 31,51 ----
{
/// <summary>
+ /// Initializes a new instance of the <see cref="SwallowExceptionHandler"/> class.
+ /// </summary>
+ public SwallowExceptionHandler()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SwallowExceptionHandler"/> class.
+ /// </summary>
+ /// <param name="exceptionNames">The exception names.</param>
+ public SwallowExceptionHandler(string[] exceptionNames) : base(exceptionNames)
+ {
+ }
+
+
+
+ /// <summary>
/// Handles the exception.
/// </summary>
Index: ExceptionHandlerAdvice.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/ExceptionHandlerAdvice.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** ExceptionHandlerAdvice.cs 9 Aug 2007 04:03:02 -0000 1.4
--- ExceptionHandlerAdvice.cs 2 Oct 2007 21:56:53 -0000 1.5
***************
*** 22,25 ****
--- 22,26 ----
using System.Collections;
using System.Reflection;
+ using System.Text.RegularExpressions;
using AopAlliance.Intercept;
using Common.Logging;
***************
*** 233,254 ****
foreach (IExceptionHandler handler in exceptionHandlers)
{
! bool match = false;
! foreach (string exceptionName in handler.SourceExceptionNames)
! {
! if (ex.GetType().Name.IndexOf(exceptionName) >= 0)
! {
! match = true;
! break;
! }
! }
! if (match)
{
! retValue = handler.HandleException(callContextDictionary);
! if (!handler.ContinueProcessing)
{
! return retValue;
}
}
-
}
return retValue;
--- 234,248 ----
foreach (IExceptionHandler handler in exceptionHandlers)
{
! if (handler != null)
{
! if (handler.CanHandleException(ex, callContextDictionary))
{
! retValue = handler.HandleException(callContextDictionary);
! if (!handler.ContinueProcessing)
! {
! return retValue;
! }
}
}
}
return retValue;
***************
*** 262,324 ****
protected virtual IExceptionHandler Parse(string handlerString)
{
! string[] parts = StringUtils.DelimitedListToStringArray(handlerString, " ");
! string[] exceptionNames = StringUtils.CommaDelimitedListToStringArray(parts[1]);
! string handlerType = parts[2];
! //TODO refactor calls to handler.SourceExceptionNames.Add
! if (handlerType.IndexOf("log") >= 0)
{
! //TODO support user selection of level, log.Debug , log.Info etc.
! LogExceptionHandler handler = new LogExceptionHandler();
! foreach (string exceptionName in exceptionNames)
{
! handler.SourceExceptionNames.Add(exceptionName);
}
! string expressionTextPart = handlerString.Substring(handlerString.IndexOf("log") + "log".Length).Trim();
! handler.ExpressionText = "#log.Debug(" + expressionTextPart + ")";
return handler;
}
! else if (handlerType.IndexOf("translate") >= 0)
{
! TranslationExceptionHandler handler = CreateTranslationExceptionHandler(exceptionNames);
! string expressionTextPart =
! handlerString.Substring(handlerString.IndexOf("translate") + "translate".Length);
! handler.ExpressionText = expressionTextPart;
return handler;
}
! else if (handlerType.IndexOf("wrap") >= 0)
{
!
! TranslationExceptionHandler handler = CreateTranslationExceptionHandler(exceptionNames);
! handler.ExpressionText = ParseWrappedExceptionExpression("wrap", handlerString);
return handler;
}
! else if (handlerType.IndexOf("replace") >= 0)
{
! TranslationExceptionHandler handler = CreateTranslationExceptionHandler(exceptionNames);
! handler.ExpressionText = ParseWrappedExceptionExpression("replace", handlerString);
return handler;
}
! else if (handlerType.IndexOf("swallow") >= 0)
{
! SwallowExceptionHandler handler = new SwallowExceptionHandler();
! foreach (string exceptionName in exceptionNames)
! {
! handler.SourceExceptionNames.Add(exceptionName);
! }
return handler;
}
! else if (handlerType.IndexOf("return") >= 0)
{
! ReturnValueExceptionHandler handler = new ReturnValueExceptionHandler();
!
! foreach (string exceptionName in exceptionNames)
! {
! handler.SourceExceptionNames.Add(exceptionName);
! }
!
! int endOfReturnIndex = handlerString.IndexOf("return") + "return".Length;
! string returnExpression = handlerString.Substring(endOfReturnIndex).Trim();
! handler.ExpressionText = returnExpression;
return handler;
}
--- 256,343 ----
protected virtual IExceptionHandler Parse(string handlerString)
{
! // parts to parse out of string
! string[] exceptionNames = new string[0];
! string constraintExpression = null;
! string actionExpressionText = null;
! string actionText = null;
! // is string of expected format?
! bool success = false;
!
! Match match = GetMatchUsingExceptionNames(handlerString);
! if (match.Success)
{
! success = true;
! //using exception names for exception filter
! exceptionNames = StringUtils.CommaDelimitedListToStringArray(match.Groups[2].Value.Trim());
! actionText = match.Groups[3].Value.Trim();
! actionExpressionText = match.Groups[4].Value.Trim();
! }
! else
! {
! match = GetMatchUsingExceptionConstraintExpression(handlerString);
! if (match.Success)
{
! success = true;
! //using constratin expression for exception filter
! constraintExpression = match.Groups[2].Value.Trim().Remove(0, 1);
! constraintExpression = constraintExpression.Substring(0, constraintExpression.Length - 1);
! actionText = match.Groups[3].Value.Trim();
! actionExpressionText = match.Groups[4].Value.Trim();
}
! }
!
! if (!success)
! {
! log.Warn("Could not parse exception hander statement " + handlerString);
! return null;
! }
!
!
! return CreateExceptionHandler(handlerString, exceptionNames, constraintExpression, actionText, actionExpressionText);
! }
!
! private static IExceptionHandler CreateExceptionHandler(string handlerString, string[] exceptionNames, string constraintExpression, string actionText, string actionExpressionText)
! {
! if (actionText.IndexOf("log") >= 0)
! {
! //TODO support user selection of level, log.Debug , log.Info etc.
! LogExceptionHandler handler = new LogExceptionHandler(exceptionNames);
! handler.ConstraintExpressionText = constraintExpression;
! handler.ActionExpressionText = "#log.Debug(" + actionExpressionText + ")";
return handler;
}
! else if (actionText.IndexOf("translate") >= 0)
{
! TranslationExceptionHandler handler = new TranslationExceptionHandler(exceptionNames);
! handler.ConstraintExpressionText = constraintExpression;
! handler.ActionExpressionText = actionExpressionText;
return handler;
}
! else if (actionText.IndexOf("wrap") >= 0)
{
! TranslationExceptionHandler handler = new TranslationExceptionHandler(exceptionNames);
! handler.ConstraintExpressionText = constraintExpression;
! handler.ActionExpressionText = ParseWrappedExceptionExpression("wrap", handlerString);
return handler;
}
! else if (actionText.IndexOf("replace") >= 0)
{
! TranslationExceptionHandler handler = new TranslationExceptionHandler(exceptionNames);
! handler.ConstraintExpressionText = constraintExpression;
! handler.ActionExpressionText = ParseWrappedExceptionExpression("replace", handlerString);
return handler;
}
! else if (actionText.IndexOf("swallow") >= 0)
{
! SwallowExceptionHandler handler = new SwallowExceptionHandler(exceptionNames);
! handler.ConstraintExpressionText = constraintExpression;
return handler;
}
! else if (actionText.IndexOf("return") >= 0)
{
! ReturnValueExceptionHandler handler = new ReturnValueExceptionHandler(exceptionNames);
! handler.ConstraintExpressionText = constraintExpression;
! handler.ActionExpressionText = actionExpressionText;
return handler;
}
***************
*** 365,376 ****
}
! private static TranslationExceptionHandler CreateTranslationExceptionHandler(string[] exceptionNames)
{
! TranslationExceptionHandler handler = new TranslationExceptionHandler();
! foreach (string exceptionName in exceptionNames)
! {
! handler.SourceExceptionNames.Add(exceptionName);
! }
! return handler;
}
--- 384,401 ----
}
! private Match GetMatchUsingExceptionNames(string handlerString)
{
! string regex = @"^(on\s+exception\s+name)\s+(.*?)\s+(log|translate|wrap|replace|return|swallow)\s*(.*?)$";
! RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
! Regex reg = new Regex(regex, options);
! return reg.Match(handlerString);
! }
!
! private Match GetMatchUsingExceptionConstraintExpression(string handlerString)
! {
! string regex = @"^(on\s+exception\s+)(\(.*?\))\s+(log|translate|wrap|replace|return|swallow)\s*(.*?)$";
! RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
! Regex reg = new Regex(regex, options);
! return reg.Match(handlerString);
}
Index: AbstractExceptionHandler.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/AbstractExceptionHandler.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** AbstractExceptionHandler.cs 8 Aug 2007 07:28:07 -0000 1.1
--- AbstractExceptionHandler.cs 2 Oct 2007 21:56:53 -0000 1.2
***************
*** 19,23 ****
--- 19,25 ----
#endregion
+ using System;
using System.Collections;
+ using Spring.Expressions;
namespace Spring.Aspects.Exceptions
***************
*** 34,39 ****
private IList sourceExceptionNames = new ArrayList();
private IList sourceExceptionTypes = new ArrayList();
! private string expressionText;
private bool continueProcessing = false;
#endregion
--- 36,42 ----
private IList sourceExceptionNames = new ArrayList();
private IList sourceExceptionTypes = new ArrayList();
! private string actionExpressionText;
private bool continueProcessing = false;
+ private string constraintExpressionText;
#endregion
***************
*** 49,54 ****
--- 52,71 ----
}
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AbstractExceptionHandler"/> class.
+ /// </summary>
+ /// <param name="exceptionNames">The exception names.</param>
+ public AbstractExceptionHandler(string[] exceptionNames)
+ {
+ foreach (string exceptionName in exceptionNames)
+ {
+ SourceExceptionNames.Add(exceptionName);
+ }
+ }
+
#endregion
+ #region Implementation of IExceptionHandler
+
#region Properties
***************
*** 74,84 ****
/// <summary>
! /// Gets the translation expression text
/// </summary>
! /// <value>The translation expression.</value>
! public string ExpressionText
{
! get { return expressionText; }
! set { expressionText = value; }
}
--- 91,112 ----
/// <summary>
! /// Gets the action translation expression text
/// </summary>
! /// <value>The action translation expression.</value>
! public string ActionExpressionText
{
! get { return actionExpressionText; }
! set { actionExpressionText = value; }
! }
!
!
! /// <summary>
! /// Gets or sets the constraint expression text.
! /// </summary>
! /// <value>The constraint expression text.</value>
! public string ConstraintExpressionText
! {
! get { return constraintExpressionText; }
! set { constraintExpressionText = value; }
}
***************
*** 96,100 ****
! #region Implementation of IExceptionHandler
/// <summary>
--- 124,164 ----
!
! /// <summary>
! /// Determines whether this instance can handle the exception the specified exception.
! /// </summary>
! /// <param name="ex">The exception.</param>
! /// <param name="callContextDictionary">The call context dictionary.</param>
! /// <returns>
! /// <c>true</c> if this instance can handle the specified exception; otherwise, <c>false</c>.
! /// </returns>
! public bool CanHandleException(Exception ex, IDictionary callContextDictionary)
! {
! if (SourceExceptionNames != null)
! {
! foreach (string exceptionName in SourceExceptionNames)
! {
! if (ex.GetType().Name.IndexOf(exceptionName) >= 0)
! {
! return true;
! }
! }
! }
! if (ConstraintExpressionText != null)
! {
! IExpression expression = Expression.Parse(ConstraintExpressionText);
! bool canProcess;
! try
! {
! canProcess = (bool) expression.GetValue(null, callContextDictionary);
! } catch (InvalidCastException)
! {
! return false;
! }
! return canProcess;
! }
!
! return false;
! }
/// <summary>
Index: IExceptionHandler.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/IExceptionHandler.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** IExceptionHandler.cs 8 Aug 2007 07:28:07 -0000 1.2
--- IExceptionHandler.cs 2 Oct 2007 21:56:53 -0000 1.3
***************
*** 19,22 ****
--- 19,23 ----
#endregion
+ using System;
using System.Collections;
***************
*** 31,34 ****
--- 32,45 ----
{
/// <summary>
+ /// Determines whether this instance can handle the exception the specified exception.
+ /// </summary>
+ /// <param name="ex">The exception.</param>
+ /// <param name="callContextDictionary">The call context dictionary.</param>
+ /// <returns>
+ /// <c>true</c> if this instance can handle the specified exception; otherwise, <c>false</c>.
+ /// </returns>
+ bool CanHandleException(Exception ex, IDictionary callContextDictionary);
+
+ /// <summary>
/// Handles the exception.
/// </summary>
***************
*** 61,65 ****
/// </summary>
/// <value>The translation expression text</value>
! string ExpressionText
{
get; set;
--- 72,85 ----
/// </summary>
/// <value>The translation expression text</value>
! string ActionExpressionText
! {
! get; set;
! }
!
! /// <summary>
! /// Gets or sets the constraint expression text.
! /// </summary>
! /// <value>The constraint expression text.</value>
! string ConstraintExpressionText
{
get; set;
Index: LogExceptionHandler.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects/Exceptions/LogExceptionHandler.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** LogExceptionHandler.cs 9 Aug 2007 04:03:02 -0000 1.3
--- LogExceptionHandler.cs 2 Oct 2007 21:56:53 -0000 1.4
***************
*** 36,39 ****
--- 36,56 ----
/// <summary>
+ /// Initializes a new instance of the <see cref="LogExceptionHandler"/> class.
+ /// </summary>
+ public LogExceptionHandler()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="LogExceptionHandler"/> class.
+ /// </summary>
+ /// <param name="exceptionNames">The exception names.</param>
+ public LogExceptionHandler(string[] exceptionNames) : base(exceptionNames)
+ {
+ }
+
+
+
+ /// <summary>
/// Gets or sets the name of the log.
/// </summary>
***************
*** 56,60 ****
ILog log = LogManager.GetLogger(logName);
callContextDictionary.Add("log", log);
! IExpression expression = Expression.Parse(ExpressionText);
expression.GetValue(null, callContextDictionary);
--- 73,77 ----
ILog log = LogManager.GetLogger(logName);
callContextDictionary.Add("log", log);
! IExpression expression = Expression.Parse(ActionExpressionText);
expression.GetValue(null, callContextDictionary);
|