Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Aop/Aspects
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv9622
Added Files:
AbstractExceptionHandler.cs AbstractExceptionHandlerAdvice.cs
IExceptionHandler.cs ParsedAdviceExpression.cs RetryAdvice.cs
Log Message:
Update to Common.Logging 1.2
Add Logging advice
Refactoring of ExceptionHandlingAdvice
start of retry advice
misc improvements to spring.aop
--- NEW FILE: IExceptionHandler.cs ---
#region License
/*
* Copyright 2002-2007 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.
*/
#endregion
using System;
using System.Collections;
namespace Spring.Aspects
{
/// <summary>
/// Handles a thrown exception providing calling context.
/// </summary>
/// <author>Mark Pollack</author>
/// <version>$Id: IExceptionHandler.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public interface IExceptionHandler
{
/// <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>
/// <param name="callContextDictionary">The call context dictionary.</param>
/// <returns>
/// The return value from handling the exception, if not rethrown or a new exception is thrown.
/// </returns>
object HandleException(IDictionary callContextDictionary);
/// <summary>
/// Gets the source exception names.
/// </summary>
/// <value>The source exception names.</value>
IList SourceExceptionNames
{
get; set;
}
/// <summary>
/// Gets the source exception types.
/// </summary>
/// <value>The source exception types.</value>
IList SourceExceptionTypes
{
get; set;
}
/// <summary>
/// Gets the translation expression text
/// </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;
}
/// <summary>
/// Gets a value indicating whether to continue processing.
/// </summary>
/// <value><c>true</c> if continue processing; otherwise, <c>false</c>.</value>
bool ContinueProcessing
{
get; set;
}
}
}
--- NEW FILE: AbstractExceptionHandlerAdvice.cs ---
#region License
/*
* Copyright 2002-2007 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.
*/
#endregion
using System.Text.RegularExpressions;
using AopAlliance.Intercept;
using Spring.Objects.Factory;
using Spring.Util;
namespace Spring.Aspects
{
/// <summary>
/// This is
/// </summary>
/// <remarks>
///
/// </remarks>
/// <author>Mark Pollack</author>
/// <version>$Id: AbstractExceptionHandlerAdvice.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public abstract class AbstractExceptionHandlerAdvice : IMethodInterceptor, IInitializingObject
{
/// <summary>
/// Gets or sets the Regex string used to parse advice expressions starting with 'on exception name' and subclass specific actions.
/// </summary>
/// <value>The regex string to parse advice expressions starting with 'on exception name' and subclass specific actions.</value>
public abstract string OnExceptionNameRegex
{
get; set;
}
/// <summary>
/// Gets or sets the Regex string used to parse advice expressions starting with 'on exception (constraint)' and subclass specific actions.
/// </summary>
/// <value>The regex string to parse advice expressions starting with 'on exception (constraint)' and subclass specific actions.</value>
public abstract string OnExceptionRegex
{
get; set;
}
/// <summary>
/// Implement this method to perform extra treatments before and after
/// the call to the supplied <paramref name="invocation"/>.
/// </summary>
/// <remarks>
/// <p>
/// Polite implementations would certainly like to invoke
/// <see cref="AopAlliance.Intercept.IJoinpoint.Proceed"/>.
/// </p>
/// </remarks>
/// <param name="invocation">
/// The method invocation that is being intercepted.
/// </param>
/// <returns>
/// The result of the call to the
/// <see cref="AopAlliance.Intercept.IJoinpoint.Proceed"/> method of
/// the supplied <paramref name="invocation"/>; this return value may
/// well have been intercepted by the interceptor.
/// </returns>
/// <exception cref="System.Exception">
/// If any of the interceptors in the chain or the target object itself
/// throws an exception.
/// </exception>
public abstract object Invoke(IMethodInvocation invocation);
/// <summary>
/// Invoked by an <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// after it has injected all of an object's dependencies.
/// </summary>
/// <remarks>
/// <p>
/// This method allows the object instance to perform the kind of
/// initialization only possible when all of it's dependencies have
/// been injected (set), and to throw an appropriate exception in the
/// event of misconfiguration.
/// </p>
/// <p>
/// Please do consult the class level documentation for the
/// <see cref="Spring.Objects.Factory.IObjectFactory"/> interface for a
/// description of exactly <i>when</i> this method is invoked. In
/// particular, it is worth noting that the
/// <see cref="Spring.Objects.Factory.IObjectFactoryAware"/>
/// and <see cref="Spring.Context.IApplicationContextAware"/>
/// callbacks will have been invoked <i>prior</i> to this method being
/// called.
/// </p>
/// </remarks>
/// <exception cref="System.Exception">
/// In the event of misconfiguration (such as the failure to set a
/// required property) or if initialization fails.
/// </exception>
public abstract void AfterPropertiesSet();
/// <summary>
/// Parses the advice expression.
/// </summary>
/// <param name="adviceExpression">The advice expression.</param>
/// <returns>An instance of ParsedAdviceExpression</returns>
protected virtual ParsedAdviceExpression ParseAdviceExpression(string adviceExpression)
{
ParsedAdviceExpression parsedAdviceExpression = new ParsedAdviceExpression(adviceExpression);
Match match = GetMatch(adviceExpression, OnExceptionNameRegex);
if (match.Success)
{
parsedAdviceExpression.Success = true;
//using exception names for exception filter
parsedAdviceExpression.ExceptionNames = StringUtils.CommaDelimitedListToStringArray(match.Groups[2].Value.Trim());
parsedAdviceExpression.ActionText = match.Groups[3].Value.Trim();
parsedAdviceExpression.ActionExpressionText = match.Groups[4].Value.Trim();
}
else
{
match = GetMatch(adviceExpression, OnExceptionRegex);
if (match.Success)
{
parsedAdviceExpression.Success = true;
//using constratin expression for exception filter
string constraintExpression = match.Groups[2].Value.Trim().Remove(0, 1);
parsedAdviceExpression.ConstraintExpression = constraintExpression.Substring(0, constraintExpression.Length - 1);
parsedAdviceExpression.ActionText = match.Groups[3].Value.Trim();
parsedAdviceExpression.ActionExpressionText = match.Groups[4].Value.Trim();
}
}
return parsedAdviceExpression;
}
/// <summary>
/// Gets the match using exception constraint expression.
/// </summary>
/// <param name="adviceExpressionString">The advice expression string.</param>
/// <param name="regexString">The regex string.</param>
/// <returns>The Match object resulting from the regular expression match.</returns>
protected virtual Match GetMatch(string adviceExpressionString, string regexString)
{
RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
Regex reg = new Regex(regexString, options);
return reg.Match(adviceExpressionString);
}
}
}
--- NEW FILE: RetryAdvice.cs ---
#region License
/*
* Copyright 2002-2007 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.
*/
#endregion
using System;
using System.Collections;
using System.Text.RegularExpressions;
using AopAlliance.Intercept;
using Common.Logging;
using Spring.Objects.Factory;
namespace Spring.Aspects
{
/// <summary>
/// AOP Advice to retry a method invocation on an exception
/// </summary>
/// <remarks>
///
/// </remarks>
/// <author>Mark Pollack</author>
/// <version>$Id: RetryAdvice.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public class RetryAdvice : IMethodInterceptor, IInitializingObject
{
#region Fields
private static readonly ILog log = LogManager.GetLogger(typeof (RetryAdvice));
private IExceptionHandler exceptionHandler;
private string retryExpression;
private int maxRetries;
#endregion
#region Properties
/// <summary>
/// Gets or sets the max retries.
/// </summary>
/// <value>The max retries.</value>
public int MaxRetries
{
get { return maxRetries; }
set { maxRetries = value; }
}
/// <summary>
/// Gets or sets the retry expression.
/// </summary>
/// <value>The retry expression.</value>
public string RetryExpression
{
get { return retryExpression; }
set { retryExpression = value; }
}
#endregion
#region IMethodInterceptor implementation
/// <summary>
/// Implement this method to perform extra treatments before and after
/// the call to the supplied <paramref name="invocation"/>.
/// </summary>
/// <param name="invocation">The method invocation that is being intercepted.</param>
/// <returns>
/// The result of the call to the
/// <see cref="AopAlliance.Intercept.IJoinpoint.Proceed"/> method of
/// the supplied <paramref name="invocation"/>; this return value may
/// well have been intercepted by the interceptor.
/// </returns>
/// <remarks>
/// <p>
/// Polite implementations would certainly like to invoke
/// <see cref="AopAlliance.Intercept.IJoinpoint.Proceed"/>.
/// </p>
/// </remarks>
/// <exception cref="System.Exception">
/// If any of the interceptors in the chain or the target object itself
/// throws an exception.
/// </exception>
public object Invoke(IMethodInvocation invocation)
{
IDictionary callContextDictionary = new Hashtable();
callContextDictionary.Add("method", invocation.Method);
callContextDictionary.Add("args", invocation.Arguments);
callContextDictionary.Add("target", invocation.Target);
int numAttempts = 0;
/*
Exception failureException;
do
{
numAttempts++;
try
{
return invocation.Proceed();
}
catch (Exception ex)
{
failureException = ex;
}
} while (numAttempts <= this.maxRetries);
throw failureException;
*/
object returnVal = null;
do
{
try
{
returnVal = invocation.Proceed();
break;
}
catch (Exception ex)
{
callContextDictionary.Add("e", ex);
if (exceptionHandler.CanHandleException(ex, callContextDictionary))
{
numAttempts++;
if (numAttempts == MaxRetries)
{
throw;
}
else
{
//Sleep()
}
}
}
} while (numAttempts <= MaxRetries);
log.Info("Invoked successfully after " + numAttempts + " attempt(s)");
return returnVal;
}
#endregion
#region IInitializingObject implementation
/// <summary>
/// Invoked by an <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// after it has injected all of an object's dependencies.
/// </summary>
/// <remarks>
/// <p>
/// This method allows the object instance to perform the kind of
/// initialization only possible when all of it's dependencies have
/// been injected (set), and to throw an appropriate exception in the
/// event of misconfiguration.
/// </p>
/// <p>
/// Please do consult the class level documentation for the
/// <see cref="Spring.Objects.Factory.IObjectFactory"/> interface for a
/// description of exactly <i>when</i> this method is invoked. In
/// particular, it is worth noting that the
/// <see cref="Spring.Objects.Factory.IObjectFactoryAware"/>
/// and <see cref="Spring.Context.IApplicationContextAware"/>
/// callbacks will have been invoked <i>prior</i> to this method being
/// called.
/// </p>
/// </remarks>
/// <exception cref="System.Exception">
/// In the event of misconfiguration (such as the failure to set a
/// required property) or if initialization fails.
/// </exception>
public void AfterPropertiesSet()
{
if (exceptionHandler == null)
{
throw new ArgumentException("Must specify retry expression.");
}
IExceptionHandler handler = Parse(retryExpression);
if (handler == null)
{
throw new ArgumentException("Was not able to parse exception handler string [" + retryExpression + "]");
}
exceptionHandler = handler;
}
#endregion
/// <summary>
/// Parses the specified handler string.
/// </summary>
/// <param name="handlerString">The handler string.</param>
/// <returns></returns>
protected virtual IExceptionHandler Parse(string handlerString)
{
return null;
}
}
}
--- NEW FILE: AbstractExceptionHandler.cs ---
#region License
/*
* Copyright 2002-2007 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.
*/
#endregion
using System;
using System.Collections;
using System.Reflection;
using Common.Logging;
using Spring.Expressions;
namespace Spring.Aspects
{
/// <summary>
/// An abstract base class providing all necessary functionality for typical IExceptionHandler implementations.
/// </summary>
/// <author>Mark Pollack</author>
/// <version>$Id: AbstractExceptionHandler.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public abstract class AbstractExceptionHandler : IExceptionHandler
{
#region Fields
/// <summary>
/// The logging instance
/// </summary>
protected readonly ILog log = LogManager.GetLogger(MethodInfo.GetCurrentMethod().DeclaringType);
private IList sourceExceptionNames = new ArrayList();
private IList sourceExceptionTypes = new ArrayList();
private string actionExpressionText;
private bool continueProcessing = false;
private string constraintExpressionText;
#endregion
#region Constructor(s)
/// <summary>
/// Initializes a new instance of the <see cref="AbstractExceptionHandler"/> class.
/// </summary>
public AbstractExceptionHandler()
{
log = LogManager.GetLogger(GetType());
}
/// <summary>
/// Initializes a new instance of the <see cref="AbstractExceptionHandler"/> class.
/// </summary>
/// <param name="exceptionNames">The exception names.</param>
public AbstractExceptionHandler(string[] exceptionNames)
{
log = LogManager.GetLogger(GetType());
foreach (string exceptionName in exceptionNames)
{
SourceExceptionNames.Add(exceptionName);
}
}
#endregion
#region Implementation of IExceptionHandler
#region Properties
/// <summary>
/// Gets the source exception names.
/// </summary>
/// <value>The source exception names.</value>
public IList SourceExceptionNames
{
get { return sourceExceptionNames; }
set { sourceExceptionNames = value; }
}
/// <summary>
/// Gets the source exception types.
/// </summary>
/// <value>The source exception types.</value>
public IList SourceExceptionTypes
{
get { return sourceExceptionTypes; }
set { sourceExceptionTypes = value; }
}
/// <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; }
}
/// <summary>
/// Gets a value indicating whether to continue processing.
/// </summary>
/// <value><c>true</c> if continue processing; otherwise, <c>false</c>.</value>
public bool ContinueProcessing
{
get { return continueProcessing; }
set { continueProcessing = value; }
}
#endregion
/// <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 e)
{
log.Warn("Was not able to unbox constraint expression to boolean [" + ConstraintExpressionText + "]", e);
return false;
} catch (Exception e)
{
log.Warn("Was not able to evaluate constraint expression [" + ConstraintExpressionText + "]",e);
return false;
}
return canProcess;
}
return false;
}
/// <summary>
/// Handles the exception.
/// </summary>
/// <returns>The return value from handling the exception, if not rethrown or a new exception is thrown.</returns>
public abstract object HandleException(IDictionary callContextDictionary);
#endregion
}
}
--- NEW FILE: ParsedAdviceExpression.cs ---
#region License
/*
* Copyright 2002-2007 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.
*/
#endregion
namespace Spring.Aspects
{
/// <summary>
/// This class contains the results of parsing an advice expresion of the form
/// on exception name [ExceptionName1,ExceptionName2,...] [action] [action expression]
/// or
/// on exception [constraint expression] [action] [action expression]
/// </summary>
/// <remarks>
///
/// </remarks>
/// <author>Mark Pollack</author>
/// <version>$Id: ParsedAdviceExpression.cs,v 1.1 2007/10/08 22:05:16 markpollack Exp $</version>
public class ParsedAdviceExpression
{
private string adviceExpression;
private string[] exceptionNames = new string[0];
private string constraintExpression = null;
private string actionExpressionText = null;
private string actionText = null;
private bool success;
/// <summary>
/// Initializes a new instance of the <see cref="ParsedAdviceExpression"/> class.
/// </summary>
/// <param name="adviceExpression">The advice expression.</param>
public ParsedAdviceExpression(string adviceExpression)
{
this.adviceExpression = adviceExpression;
}
/// <summary>
/// Gets or sets the advice expression.
/// </summary>
/// <value>The advice expression.</value>
public string AdviceExpression
{
get { return adviceExpression; }
set { adviceExpression = value; }
}
/// <summary>
/// Gets or sets the exception names.
/// </summary>
/// <value>The exception names.</value>
public string[] ExceptionNames
{
get { return exceptionNames; }
set { exceptionNames = value; }
}
/// <summary>
/// Gets or sets the constraint expression.
/// </summary>
/// <value>The constraint expression.</value>
public string ConstraintExpression
{
get { return constraintExpression; }
set { constraintExpression = value; }
}
/// <summary>
/// Gets or sets the action expression text.
/// </summary>
/// <value>The action expression text.</value>
public string ActionExpressionText
{
get { return actionExpressionText; }
set { actionExpressionText = value; }
}
/// <summary>
/// Gets or sets the action text.
/// </summary>
/// <value>The action text.</value>
public string ActionText
{
get { return actionText; }
set { actionText = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="ParsedAdviceExpression"/> is success.
/// </summary>
/// <value><c>true</c> if success; otherwise, <c>false</c>.</value>
public bool Success
{
get { return success; }
set { success = value; }
}
}
}
|