1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
#region Using directives
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.Workflow.ComponentModel;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Workflow.Runtime;
using System.Xml;
#endregion
namespace System.Workflow.Activities
{
internal static class CorrelationResolver
{
static Dictionary<Type, CorrelationMethodResolver> cachedTypeResolver = new Dictionary<Type, CorrelationMethodResolver>();
static object mutex = new object();
internal static bool IsInitializingMember(Type interfaceType, string memberName, object[] methodArgs)
{
if (interfaceType == null)
throw new ArgumentNullException("interfaceType");
if (memberName == null)
throw new ArgumentNullException("memberName");
if (memberName.Length == 0)
throw new ArgumentException(SR.GetString(SR.Error_EventNameMissing));
ICorrelationProvider correlationProvider = CorrelationResolver.GetCorrelationProvider(interfaceType);
return correlationProvider.IsInitializingMember(interfaceType, memberName, methodArgs);
}
internal static ICollection<CorrelationProperty> ResolveCorrelationValues(Type interfaceType, string eventName, object[] eventArgs, bool provideInitializerTokens)
{
if (interfaceType == null)
throw new ArgumentNullException("interfaceType");
if (eventName == null)
throw new ArgumentNullException("eventName");
if (eventName.Length == 0)
throw new ArgumentException(SR.GetString(SR.Error_EventNameMissing));
ICorrelationProvider correlationProvider = CorrelationResolver.GetCorrelationProvider(interfaceType);
return correlationProvider.ResolveCorrelationPropertyValues(interfaceType, eventName, eventArgs, provideInitializerTokens);
}
internal static ICorrelationProvider GetCorrelationProvider(Type interfaceType)
{
CorrelationMethodResolver resolver = GetResolver(interfaceType);
return resolver.CorrelationProvider;
}
private static CorrelationMethodResolver GetResolver(Type interfaceType)
{
CorrelationMethodResolver resolver = null;
cachedTypeResolver.TryGetValue(interfaceType, out resolver);
if (resolver == null)
{
lock (mutex)
{
cachedTypeResolver.TryGetValue(interfaceType, out resolver);
if (resolver == null)
{
resolver = new CorrelationMethodResolver(interfaceType);
cachedTypeResolver.Add(interfaceType, resolver);
}
}
}
return resolver;
}
}
internal sealed class CorrelationMethodResolver
{
Type interfaceType;
// a correlation provider for each interface type
ICorrelationProvider correlationProvider;
object corrProviderSync = new object();
internal CorrelationMethodResolver(Type interfaceType)
{
this.interfaceType = interfaceType;
}
internal ICorrelationProvider CorrelationProvider
{
get
{
if (this.correlationProvider == null)
{
lock (this.corrProviderSync)
{
if (this.correlationProvider == null)
{
ICorrelationProvider provider = null;
object[] corrProviderAttribs = this.interfaceType.GetCustomAttributes(typeof(CorrelationProviderAttribute), true);
if (corrProviderAttribs.Length == 0)
{
corrProviderAttribs = this.interfaceType.GetCustomAttributes(typeof(ExternalDataExchangeAttribute), true);
object[] corrParameterAttribs = this.interfaceType.GetCustomAttributes(typeof(CorrelationParameterAttribute), true);
if (corrProviderAttribs.Length != 0 && corrParameterAttribs.Length != 0)
{
// no provider specified but it is a data exchange correlation service
// hence use our default correlation
provider = new DefaultCorrelationProvider(this.interfaceType);
}
else
{
// opaque interface with no correlation
provider = new NonCorrelatedProvider();
}
}
else
{
CorrelationProviderAttribute cpattrib = corrProviderAttribs[0] as CorrelationProviderAttribute;
Type providerType = cpattrib.CorrelationProviderType;
provider = Activator.CreateInstance(providerType) as ICorrelationProvider;
}
System.Threading.Thread.MemoryBarrier();
this.correlationProvider = provider;
}
}
}
return this.correlationProvider;
}
}
}
internal sealed class CorrelationPropertyValue
{
string name;
string locationPath;
int signaturePosition;
internal CorrelationPropertyValue(string name, string locationPath, int signaturePosition)
{
this.name = name;
this.locationPath = locationPath;
this.signaturePosition = signaturePosition;
}
internal object GetValue(object[] args)
{
if (args.Length <= this.signaturePosition)
throw new ArgumentOutOfRangeException("args");
object arg = args[this.signaturePosition];
if (arg == null)
return arg;
Type type = arg.GetType();
object val = arg;
if (this.locationPath.Length != 0)
{
string[] split = locationPath.Split(new Char[] { '.' });
for (int i = 1; i < split.Length; i++)
{
string s = split[i];
if (null == arg)
break;
val = type.InvokeMember(s, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty, null, arg, null, null);
MemberInfo[] mInfos = type.GetMember(s, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty);
type = GetMemberType(mInfos[0]);
arg = val;
}
}
return val;
}
internal string Name
{
get
{
return this.name;
}
}
private Type GetMemberType(MemberInfo mInfo)
{
Type type = null;
switch (mInfo.MemberType)
{
case MemberTypes.Field:
type = ((FieldInfo)mInfo).FieldType;
break;
case MemberTypes.Property:
type = ((PropertyInfo)mInfo).PropertyType;
break;
default:
Debug.Assert(false, "locationPath points to something other than a Field/Property");
return null;
}
return type;
}
}
internal sealed class DefaultCorrelationProvider : ICorrelationProvider
{
Type interfaceType;
// map of method name to correlation properties
Dictionary<string, CorrelationPropertyValue[]> cachedCorrelationProperties;
object cachedCorrelationPropertiesSync = new object();
// cached initializers
// map operation to bool flag to indicate events
Dictionary<string, bool> initializerCorrelationPropertys = null;
object initializerCorrelationPropertysSync = new object();
internal DefaultCorrelationProvider(Type interfaceType)
{
this.cachedCorrelationProperties = new Dictionary<string, CorrelationPropertyValue[]>();
this.interfaceType = interfaceType;
}
ICollection<CorrelationProperty> ICorrelationProvider.ResolveCorrelationPropertyValues(Type interfaceType, string methodName, object[] methodArgs, bool provideInitializerTokens)
{
CorrelationPropertyValue[] correlationProperties = null;
if (methodArgs == null || provideInitializerTokens)
{
return null; // no initializer specific token to return
}
this.cachedCorrelationProperties.TryGetValue(methodName, out correlationProperties);
if (correlationProperties == null)
{
lock (this.cachedCorrelationPropertiesSync)
{
this.cachedCorrelationProperties.TryGetValue(methodName, out correlationProperties);
if (correlationProperties == null)
{
correlationProperties = GetCorrelationProperties(interfaceType, methodName);
this.cachedCorrelationProperties.Add(methodName, correlationProperties);
}
}
}
List<CorrelationProperty> predicates = new List<CorrelationProperty>();
for (int i = 0; i < correlationProperties.Length; i++)
{
predicates.Add(new CorrelationProperty(correlationProperties[i].Name, correlationProperties[i].GetValue(methodArgs)));
}
return predicates;
}
private Dictionary<string, bool> InitializerCorrelationPropertys
{
get
{
if (this.initializerCorrelationPropertys == null)
{
lock (this.initializerCorrelationPropertysSync)
{
if (this.initializerCorrelationPropertys == null)
{
Dictionary<string, bool> members = new Dictionary<string, bool>();
// note this is separated out since we may need to distinguish between events & methods
foreach (EventInfo member in this.interfaceType.GetEvents())
{
if ((member.GetCustomAttributes(typeof(CorrelationInitializerAttribute), true)).Length > 0)
{
members.Add(member.Name, true);
}
}
foreach (MethodInfo member in this.interfaceType.GetMethods())
{
if ((member.GetCustomAttributes(typeof(CorrelationInitializerAttribute), true)).Length > 0)
{
members.Add(member.Name, false);
}
}
this.initializerCorrelationPropertys = members;
}
}
}
return this.initializerCorrelationPropertys;
}
}
bool ICorrelationProvider.IsInitializingMember(Type interfaceType, string memberName, object[] methodArgs)
{
return InitializerCorrelationPropertys.ContainsKey(memberName);
}
private CorrelationPropertyValue[] GetCorrelationProperties(Type interfaceType, string methodName)
{
CorrelationPropertyValue[] correlationProperties = null;
if (interfaceType.GetCustomAttributes(typeof(ExternalDataExchangeAttribute), true).Length == 0)
throw new InvalidOperationException(SR.GetString(SR.Error_ExternalDataExchangeException, interfaceType.AssemblyQualifiedName));
List<Object> correlationParamAttributes = new List<Object>();
correlationParamAttributes.AddRange(GetCorrelationParameterAttributes(interfaceType));
if (correlationParamAttributes.Count == 0)
throw new InvalidOperationException(SR.GetString(SR.Error_CorrelationParameterException, interfaceType.AssemblyQualifiedName));
correlationProperties = new CorrelationPropertyValue[correlationParamAttributes.Count];
Dictionary<String, CorrelationAliasAttribute> corrAliases = null;
MethodInfo methodInfo = null;
GetMethodInfo(interfaceType, methodName, out methodInfo, out corrAliases);
if (methodInfo == null)
{
throw new MissingMethodException(interfaceType.AssemblyQualifiedName, methodName);
}
ParameterInfo[] parameters = methodInfo.GetParameters();
int i = 0;
foreach (CorrelationParameterAttribute paramAttribute in correlationParamAttributes)
{
String location = paramAttribute.Name;
CorrelationAliasAttribute aliasAttribute = GetMatchingCorrelationAlias(paramAttribute, corrAliases, correlationParamAttributes.Count == 1);
if (aliasAttribute != null)
location = aliasAttribute.Path;
CorrelationPropertyValue value = GetCorrelationProperty(parameters, paramAttribute.Name, location);
if (value == null)
throw new InvalidOperationException(SR.GetString(SR.Error_CorrelationParameterException, interfaceType.AssemblyQualifiedName, paramAttribute.Name, methodName));
correlationProperties[i++] = value;
}
return correlationProperties;
}
private CorrelationAliasAttribute GetMatchingCorrelationAlias(CorrelationParameterAttribute paramAttribute, Dictionary<String, CorrelationAliasAttribute> correlationAliases, bool defaultParameter)
{
CorrelationAliasAttribute corrAlias = null;
if (correlationAliases == null) return null;
if (defaultParameter)
{
if (correlationAliases.TryGetValue("", out corrAlias))
{
return corrAlias;
}
}
correlationAliases.TryGetValue(paramAttribute.Name, out corrAlias);
return corrAlias;
}
private CorrelationPropertyValue GetCorrelationProperty(ParameterInfo[] parameters, String propertyName, String location)
{
string[] split = location.Split(new Char[] { '.' });
if (split.Length == 1 && parameters.Length == 2)
{
if (typeof(ExternalDataEventArgs).IsAssignableFrom(parameters[1].ParameterType))
{
string aliasedLocation = "e." + location;
return GetCorrelationProperty(parameters, propertyName, "e", aliasedLocation);
}
}
string parameterName = split[0];
return GetCorrelationProperty(parameters, propertyName, parameterName, location);
}
private void GetMethodInfo(Type interfaceType, string methodName, out MethodInfo methodInfo, out Dictionary<String, CorrelationAliasAttribute> correlationAliases)
{
correlationAliases = new Dictionary<String, CorrelationAliasAttribute>();
Object[] customAttrs = null;
methodInfo = null;
// check events
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
EventInfo eventInfo = interfaceType.GetEvent(methodName, bindingFlags);
if (eventInfo != null)
{
customAttrs = eventInfo.GetCustomAttributes(typeof(CorrelationAliasAttribute), true);
if (customAttrs == null || customAttrs.Length == 0)
{
customAttrs = eventInfo.EventHandlerType.GetCustomAttributes(typeof(CorrelationAliasAttribute), true);
}
MethodInfo[] methInfo = eventInfo.EventHandlerType.GetMethods();
methodInfo = methInfo[0];
}
else
{
// check methods
methodInfo = interfaceType.GetMethod(methodName, bindingFlags);
if (methodInfo == null)
{
throw new MissingMethodException(interfaceType.AssemblyQualifiedName, methodName);
}
customAttrs = methodInfo.GetCustomAttributes(typeof(CorrelationAliasAttribute), true);
}
foreach (CorrelationAliasAttribute aliasAttribute in customAttrs)
{
if (customAttrs.Length > 1)
{
Debug.Assert(aliasAttribute.Name != null);
if (aliasAttribute.Name == null)
throw new ArgumentNullException("ParameterName");
}
correlationAliases.Add(aliasAttribute.Name == null ? "" : aliasAttribute.Name, aliasAttribute);
}
}
private CorrelationPropertyValue GetCorrelationProperty(ParameterInfo[] parameters, string propertyName, string parameterName, string location)
{
for (int j = 0; parameters != null && j < parameters.Length; j++)
{
ParameterInfo param = parameters[j];
if (param.Name == parameterName)
{
// parameter match
return new CorrelationPropertyValue(propertyName, location, param.Position);
}
}
return null;
}
private object[] GetCorrelationParameterAttributes(Type type)
{
return type.GetCustomAttributes(typeof(CorrelationParameterAttribute), true);
}
}
internal sealed class NonCorrelatedProvider : ICorrelationProvider
{
internal NonCorrelatedProvider()
{
}
ICollection<CorrelationProperty> ICorrelationProvider.ResolveCorrelationPropertyValues(Type interfaceType, string methodName, object[] methodArgs, bool provideInitializerTokens)
{
// non correlated
// no values to return
return null;
}
bool ICorrelationProvider.IsInitializingMember(Type interfaceType, string memberName, object[] methodArgs)
{
return true;
}
}
}
|