/// <summary> /// Load a defined process from the configuration node. /// </summary> /// <param name="pipeline">Parent pipeline</param> /// <param name="node">Configuration node</param> /// <param name="pname">Pipeline name</param> private void LoadProcessor(object pipeline, ConfigPathNode node, string pname) { LogUtils.Debug(String.Format("Loading processor from node. [pipeline={0}][node={1}]", pname, node.GetAbsolutePath())); if (node.Name != CONFIG_NODE_PROCESSOR) { LogUtils.Warn(String.Format("Invalid Processor Node: [path={0}]", node.GetSearchPath())); return; } ProcessConfig def = ConfigurationAnnotationProcessor.Process <ProcessConfig>(node); Conditions.NotNull(def); string typename = def.Type; Type type = null; if (!String.IsNullOrWhiteSpace(def.Assembly)) { string aname = Path.GetFileName(def.Assembly); Assembly asm = AssemblyUtils.GetOrLoadAssembly(aname, def.Assembly); Conditions.NotNull(asm); type = asm.GetType(typename, true); Conditions.NotNull(type); } else { type = Type.GetType(typename, true); Conditions.NotNull(type); } object obj = null; if (def.IsReference) { if (ReflectionUtils.ImplementsGenericInterface(type, typeof(Pipeline <>))) { if (pipelines.ContainsKey(def.Name)) { obj = pipelines[def.Name]; } else { throw new ProcessException(String.Format("Referenced Pipeline not found: [name={0}][type={1}]", def.Name, type.FullName)); } } } else { obj = ConfigurationAnnotationProcessor.CreateInstance(type, node); Conditions.NotNull(obj); if (!ReflectionUtils.IsSubclassOfRawGeneric(obj.GetType(), typeof(Processor <>))) { throw new ProcessException(String.Format("Invalid processor type. [type={0}]", type.FullName)); } PropertyInfo pi = TypeUtils.FindProperty(type, "Name"); Conditions.NotNull(pi); pi.SetValue(obj, def.Name); } AddProcessor(pipeline, obj, def.Condition, def.TypeName); }
/// <summary> /// Load a pipeline definition from the specified node. /// </summary> /// <param name="node">Configuration Node</param> private void LoadPipeline(ConfigPathNode node) { LogUtils.Debug(String.Format("Loading pipeline from node. [node={0}]", node.GetAbsolutePath())); if (node.Name != CONFIG_NODE_PIPELINE) { LogUtils.Warn(String.Format("Invalid Pipeline Node: [path={0}]", node.GetSearchPath())); return; } PipelineConfig def = ConfigurationAnnotationProcessor.Process <PipelineConfig>(node); Conditions.NotNull(def); Type type = null; string typename = def.Type; if (!String.IsNullOrWhiteSpace(def.Assembly)) { string aname = Path.GetFileName(def.Assembly); Assembly asm = AssemblyUtils.GetOrLoadAssembly(aname, def.Assembly); Conditions.NotNull(asm); type = asm.GetType(typename, true); Conditions.NotNull(type); } else { type = Type.GetType(typename, true); Conditions.NotNull(type); } object obj = ConfigurationAnnotationProcessor.CreateInstance(type, node); Conditions.NotNull(obj); if (!ReflectionUtils.ImplementsGenericInterface(obj.GetType(), typeof(Pipeline <>))) { throw new ProcessException(String.Format("Invalid pipeline type. [type={0}]", type.FullName)); } PropertyInfo pi = TypeUtils.FindProperty(type, PROPPERTY_NAME); Conditions.NotNull(pi); pi.SetValue(obj, def.Name); LoadProcessors(obj, node, def.Name); if (pipelines.ContainsKey(def.Name)) { throw new ProcessException(String.Format("Duplicate pipeline name: [name={0}][type={1}]", def.Name, type.FullName)); } pipelines[def.Name] = obj; }
/// <summary> /// Check and Invoke annotated methods for this type. /// </summary> /// <typeparam name="T">Target Instance type</typeparam> /// <param name="node">Configuration node.</param> /// <param name="target">Target Type instance</param> private static void CallMethodInvokes <T>(ConfigPathNode node, T target) { Type type = target.GetType(); MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); if (methods != null) { foreach (MethodInfo method in methods) { MethodInvoke mi = (MethodInvoke)Attribute.GetCustomAttribute(method, typeof(MethodInvoke)); if (mi != null) { bool invoked = false; ParameterInfo[] parameters = method.GetParameters(); ConfigPathNode nnode = node; if (parameters != null && parameters.Length > 0) { if (!String.IsNullOrWhiteSpace(mi.Path)) { AbstractConfigNode cnode = nnode.Find(mi.Path); if (cnode != null && cnode.GetType() == typeof(ConfigPathNode)) { nnode = (ConfigPathNode)cnode; } } if (nnode != null) { ConfigParametersNode pnode = nnode.GetParameters(); if (pnode != null) { List <object> values = FindParameters(pnode, method.Name, parameters); if (values != null && values.Count > 0) { method.Invoke(target, values.ToArray()); invoked = true; } } } } else { method.Invoke(target, null); invoked = true; } if (!invoked) { throw new AnnotationProcessorException(String.Format("Error Invoking Method : [mehtod={0}][node={1}]", method.Name, node.GetSearchPath())); } } } } }
/// <summary> /// Create a new Instance of the specified type. /// /// Type should have an empty constructor or a constructor with annotation. /// </summary> /// <typeparam name="T">Target Instance type</typeparam> /// <param name="type">Type</param> /// <param name="node">Configuration node.</param> /// <returns>Created Instance</returns> public static T CreateInstance <T>(Type type, ConfigPathNode node) { T target = default(T); ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); if (constructors != null && constructors.Length > 0) { foreach (ConstructorInfo ci in constructors) { MethodInvoke mi = (MethodInvoke)Attribute.GetCustomAttribute(ci, typeof(MethodInvoke)); if (mi != null) { ParameterInfo[] parameters = ci.GetParameters(); ConfigPathNode nnode = node; if (parameters != null && parameters.Length > 0) { if (!String.IsNullOrWhiteSpace(mi.Path)) { AbstractConfigNode cnode = nnode.Find(mi.Path); if (cnode != null && cnode.GetType() == typeof(ConfigPathNode)) { nnode = (ConfigPathNode)cnode; } } if (nnode != null) { ConfigParametersNode pnode = nnode.GetParameters(); if (pnode != null) { List <object> values = FindParameters(pnode, ci.Name, parameters); if (values != null && values.Count > 0) { target = (T)Activator.CreateInstance(type, values.ToArray()); break; } } } } else { target = Activator.CreateInstance <T>(); break; } } } } if (ReflectionUtils.IsNull(target)) { target = Activator.CreateInstance <T>(); } if (!ReflectionUtils.IsNull(target)) { target = ReadValues((ConfigPathNode)node, target, null); CallMethodInvokes((ConfigPathNode)node, target); } else { throw new AnnotationProcessorException(String.Format("Error creating instance of Type: [path={0}][type={1}]", node.GetSearchPath(), type.FullName)); } return(target); }