protected internal override ValidationErrorCollection ValidateChanges(Activity contextActivity) { if (contextActivity == null) { throw new ArgumentNullException("contextActivity"); } ValidationErrorCollection errors = new ValidationErrorCollection(); CompositeActivity ownerActivity = contextActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity; if (ownerActivity != null && WorkflowChanges.IsActivityExecutable(ownerActivity)) { foreach (Validator validator in ComponentDispenser.CreateComponents(ownerActivity.GetType(), typeof(ActivityValidatorAttribute))) { ValidationError error = validator.ValidateActivityChange(ownerActivity, this); if (error != null) { errors.Add(error); } } } return(errors); }
internal static ActivityExecutor[] GetActivityExecutors(Activity activity) { if (activity == null) { throw new ArgumentNullException("activity"); } Type type = activity.GetType(); ActivityExecutor[] executorArray = executors[type] as ActivityExecutor[]; if (executorArray == null) { lock (executors.SyncRoot) { executorArray = executors[type] as ActivityExecutor[]; if (executorArray != null) { return(executorArray); } object[] objArray = null; try { objArray = ComponentDispenser.CreateActivityExecutors(activity); } catch (Exception exception) { throw new InvalidOperationException(SR.GetString("ExecutorCreationFailedErrorMessage", new object[] { type.FullName }), exception); } if ((objArray == null) || (objArray.Length == 0)) { throw new InvalidOperationException(SR.GetString("ExecutorCreationFailedErrorMessage", new object[] { type.FullName })); } executorArray = new ActivityExecutor[objArray.Length]; for (int i = 0; i < objArray.Length; i++) { if (!typeToExecutorMapping.Contains(objArray[i].GetType())) { lock (typeToExecutorMapping.SyncRoot) { if (!typeToExecutorMapping.Contains(objArray[i].GetType())) { Thread.MemoryBarrier(); typeToExecutorMapping[objArray[i].GetType()] = objArray[i]; } } } executorArray[i] = (ActivityExecutor)typeToExecutorMapping[objArray[i].GetType()]; } Thread.MemoryBarrier(); executors[type] = executorArray; } } return(executorArray); }
internal static object[] CreateComponents(Type objectType, Type componentTypeAttribute) { //*******DO NOT CHANGE THE ORDER OF THE EXECUTION AS IT HAS SIGNIFICANCE AT RUNTIME Dictionary <Type, object> components = new Dictionary <Type, object>(); // Goto all the attributes and collect matching attributes and component factories ArrayList supportsTransactionComponents = new ArrayList(); ArrayList supportsCancelHandlerComponents = new ArrayList(); ArrayList supportsSynchronizationComponents = new ArrayList(); ArrayList supportsCompensationHandlerComponents = new ArrayList(); // dummy calls to make sure that attributes are in good shape GetCustomAttributes(objectType, typeof(ActivityCodeGeneratorAttribute), true); GetCustomAttributes(objectType, typeof(ActivityValidatorAttribute), true); GetCustomAttributes(objectType, typeof(System.ComponentModel.DesignerAttribute), true); GetCustomAttributes(objectType, typeof(System.ComponentModel.Design.Serialization.DesignerSerializerAttribute), true); if (objectType.GetCustomAttributes(typeof(SupportsTransactionAttribute), true).Length > 0) { if (componentTypeAttribute == typeof(ActivityValidatorAttribute)) { supportsTransactionComponents.Add(new TransactionContextValidator()); } } if (objectType.GetCustomAttributes(typeof(SupportsSynchronizationAttribute), true).Length > 0) { if (componentTypeAttribute == typeof(ActivityValidatorAttribute)) { supportsSynchronizationComponents.Add(new SynchronizationValidator()); } } // IMPORTANT: sequence of these components is really critical AddComponents(components, supportsSynchronizationComponents.ToArray()); AddComponents(components, supportsTransactionComponents.ToArray()); AddComponents(components, supportsCompensationHandlerComponents.ToArray()); AddComponents(components, supportsCancelHandlerComponents.ToArray()); //Goto all the interfaces and collect matching attributes and component factories ArrayList customAttributes = new ArrayList(); foreach (Type interfaceType in objectType.GetInterfaces()) { customAttributes.AddRange(ComponentDispenser.GetCustomAttributes(interfaceType, componentTypeAttribute, true)); } //Add all the component's attributes customAttributes.AddRange(ComponentDispenser.GetCustomAttributes(objectType, componentTypeAttribute, true)); string typeName = null; foreach (Attribute attribute in customAttributes) { Type expectedBaseType = null; if (componentTypeAttribute == typeof(ActivityCodeGeneratorAttribute)) { typeName = ((ActivityCodeGeneratorAttribute)attribute).CodeGeneratorTypeName; expectedBaseType = typeof(ActivityCodeGenerator); } else if (componentTypeAttribute == typeof(ActivityValidatorAttribute)) { typeName = ((ActivityValidatorAttribute)attribute).ValidatorTypeName; expectedBaseType = typeof(Validator); } else { System.Diagnostics.Debug.Assert(false, "wrong attribute type"); } object component = null; try { if (!String.IsNullOrEmpty(typeName)) { component = ComponentDispenser.CreateComponentInstance(typeName, objectType); } } catch { } if ((component != null && expectedBaseType != null && expectedBaseType.IsAssignableFrom(component.GetType()))) { if (!components.ContainsKey(component.GetType())) { components.Add(component.GetType(), component); } } else { throw new InvalidOperationException(SR.GetString(SR.Error_InvalidAttribute, componentTypeAttribute.Name, objectType.FullName)); } } return(new ArrayList(components.Values).ToArray()); }
internal static ActivityExecutor[] GetActivityExecutors(Activity activity) { if (activity == null) { throw new ArgumentNullException("activity"); } Type activityType = activity.GetType(); ActivityExecutor[] activityExecutors = executors[activityType] as ActivityExecutor[]; if (activityExecutors != null) { return(activityExecutors); } lock (executors.SyncRoot) { activityExecutors = executors[activityType] as ActivityExecutor[]; if (activityExecutors != null) { return(activityExecutors); } object[] activityExecutorsObjects = null; try { //activityExecutorsObjects = ComponentDispenser.CreateComponents(activityType, typeof(ActivityExecutorAttribute)); activityExecutorsObjects = ComponentDispenser.CreateActivityExecutors(activity); } catch (Exception e) { throw new InvalidOperationException(SR.GetString(SR.ExecutorCreationFailedErrorMessage, activityType.FullName), e); } if (activityExecutorsObjects == null || activityExecutorsObjects.Length == 0) { throw new InvalidOperationException(SR.GetString(SR.ExecutorCreationFailedErrorMessage, activityType.FullName)); } activityExecutors = new ActivityExecutor[activityExecutorsObjects.Length]; for (int index = 0; index < activityExecutorsObjects.Length; index++) { if (!typeToExecutorMapping.Contains(activityExecutorsObjects[index].GetType())) { lock (typeToExecutorMapping.SyncRoot) { if (!typeToExecutorMapping.Contains(activityExecutorsObjects[index].GetType())) { System.Threading.Thread.MemoryBarrier(); typeToExecutorMapping[activityExecutorsObjects[index].GetType()] = activityExecutorsObjects[index]; } } } activityExecutors[index] = (ActivityExecutor)typeToExecutorMapping[activityExecutorsObjects[index].GetType()]; } System.Threading.Thread.MemoryBarrier(); executors[activityType] = activityExecutors; } return(activityExecutors); }