Skip to content
Browse files

Interception

  • Loading branch information...
1 parent 0e8b4e7 commit a49b3507f29035fc281b2452a524e6e42675c7b8 @adrianaisemberg adrianaisemberg committed Oct 26, 2011
Showing with 78 additions and 11 deletions.
  1. +28 −0 CLAP/Exceptions.cs
  2. +50 −11 CLAP/Parser.cs
View
28 CLAP/Exceptions.cs
@@ -175,6 +175,34 @@ public MoreThanOneErrorHandlerException()
}
[Serializable]
+ public class MoreThanOnePreVerbInterceptorException : CommandLineException
+ {
+ public MoreThanOnePreVerbInterceptorException()
+ : base("More than one pre-verb interceptor was defined. Only a single method can be marked with [PreVerbExecutionAttribute] in a type and only a single action can be registered as a pre-interceptor.")
+ {
+ }
+
+ protected MoreThanOnePreVerbInterceptorException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context) { }
+ }
+
+ [Serializable]
+ public class MoreThanOnePostVerbInterceptorException : CommandLineException
+ {
+ public MoreThanOnePostVerbInterceptorException()
+ : base("More than one post-verb interceptor was defined. Only a single method can be marked with [PostVerbExecutionAttribute] in a type and only a single action can be registered as a post-interceptor.")
+ {
+ }
+
+ protected MoreThanOnePostVerbInterceptorException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context)
+ : base(info, context) { }
+ }
+
+ [Serializable]
public class ArgumentMismatchException : CommandLineException
{
public ArgumentMismatchException() { }
View
61 CLAP/Parser.cs
@@ -359,7 +359,7 @@ private void RunInternal(string[] args, object obj)
Execute(obj, method, inputArgs, parameterValues);
}
- private static void Execute(
+ private void Execute(
object target,
Method method,
Dictionary<string, string> inputArgs,
@@ -371,21 +371,45 @@ private void RunInternal(string[] args, object obj)
//
var preVerbExecutionContext = new PreVerbExecutionContext(method, target, arguments);
- // validate the arguments so they weren't changed after the interception
- //
+ var preInterceptionMethods = typeof(T).GetMethodsWith<PreVerbExecutionAttribute>();
+
+ if (preInterceptionMethods.Any())
+ {
+ Debug.Assert(preInterceptionMethods.Count() == 1);
+
+ var preInterceptionMethod = preInterceptionMethods.First();
+
+ preInterceptionMethod.Invoke(target, new[] { preVerbExecutionContext });
+ }
- // invoke the method with the list of parameters
+ // actual verb execution
//
- method.MethodInfo.Invoke(target, values.ToArray());
+ if (!preVerbExecutionContext.Cancel)
+ {
+ // invoke the method with the list of parameters
+ //
+ method.MethodInfo.Invoke(target, values.ToArray());
+ }
// post-interception
//
- var postVerbExecutionContext = new PostVerbExecutionContext(
- method,
- target,
- arguments,
- preVerbExecutionContext.Cancel,
- preVerbExecutionContext.UserContext);
+ var postInterceptionMethods = typeof(T).GetMethodsWith<PostVerbExecutionAttribute>();
+
+ if (postInterceptionMethods.Any())
+ {
+ Debug.Assert(postInterceptionMethods.Count() == 1);
+
+ var postInterceptionMethod = postInterceptionMethods.First();
+
+ var postVerbExecutionContext = new PostVerbExecutionContext(
+ method,
+ target,
+ arguments,
+ preVerbExecutionContext.Cancel,
+ preVerbExecutionContext.UserContext);
+
+ postInterceptionMethod.Invoke(target, new[] { postVerbExecutionContext });
+ }
}
private static void ValidateVerbInput(Method method, ParameterInfo[] methodParameters, List<object> parameterValues)
@@ -519,6 +543,21 @@ private void Validate()
throw new MoreThanOneEmptyHandlerException();
}
+ // no more that one pre/post interception methods
+ //
+ var preInterceptionMethods = typeof(T).GetMethodsWith<PreVerbExecutionAttribute>();
+
+ if (preInterceptionMethods.Count() > 1)
+ {
+ throw new MoreThanOnePreVerbInterceptorException();
+ }
+
+ var postInterceptionMethods = typeof(T).GetMethodsWith<PostVerbExecutionAttribute>();
+
+ if (postInterceptionMethods.Count() > 1)
+ {
+ throw new MoreThanOnePostVerbInterceptorException();
+ }
}
private void RegisterParameterHandlerInternal<TParameter>(string names, Action<TParameter> action, string description)

0 comments on commit a49b350

Please sign in to comment.
Something went wrong with that request. Please try again.