Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create/Get an instance of an autowired object.
        /// </summary>
        /// <typeparam name="T">Type of Object</typeparam>
        /// <param name="updated">Autowired Index for the updated type</param>
        private void UpdateAutowireType <T>(AutowiredIndexStruct updated)
        {
            Preconditions.CheckArgument(updated);

            Type   type = typeof(T);
            string key  = GetTypeKey(type, updated.RelativePath, updated.ConfigName);

            if (!String.IsNullOrWhiteSpace(key))
            {
                if (!autowiredObjects.ContainsKey(key))
                {
                    lock (autowiredObjects)
                    {
                        if (autowiredObjects.ContainsKey(key))
                        {
                            Configuration config = ReadLockConfig(updated.ConfigName, DEFAULT_READ_LOCK_TIMEOUT);
                            if (config != null)
                            {
                                try
                                {
                                    T value = (T)autowiredObjects[key];

                                    List <string> valuePaths = null;
                                    if (!String.IsNullOrWhiteSpace(updated.RelativePath))
                                    {
                                        AbstractConfigNode node = config.Find(updated.RelativePath);
                                        if (node == null)
                                        {
                                            throw new ConfigurationException(
                                                      String.Format("Specified configuration node not found. [config={0}][path={1}]",
                                                                    updated.ConfigName, updated.RelativePath));
                                        }
                                        ConfigurationAnnotationProcessor.Process <T>(node, value, out valuePaths);
                                    }
                                    else
                                    {
                                        ConfigurationAnnotationProcessor.Process <T>(config, value, out valuePaths);
                                    }
                                }
                                finally
                                {
                                    ConfigReleaseRead(config.Header.Name);
                                }
                            }
                            else
                            {
                                throw new ConfigurationException(
                                          String.Format("Error getting confguration. (Might be a lock timeout) [name={0}]", updated.ConfigName));
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create and setup a new node instance handle.
        /// </summary>
        /// <typeparam name="T">Instance Type.</typeparam>
        /// <returns>Node Instance Handle.</returns>
        protected T SetupInstance <T>() where T : ZConfigInstance
        {
            T instance = Activator.CreateInstance <T>();

            instance.ID        = Guid.NewGuid().ToString();
            instance.StartTime = DateTime.Now;

            ConfigurationAnnotationProcessor.Process <T>(Configuration, instance);
            instance.IP               = NetUtils.GetIpAddress();
            instance.Hostname         = NetUtils.GetHostName();
            instance.ApplicationGroup = Configuration.Header.ApplicationGroup;
            instance.ApplicationName  = Configuration.Header.Application;

            return(instance);
        }
Ejemplo n.º 4
0
        /// <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;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the complete search path.
        /// </summary>
        /// <param name="type">Object type</param>
        /// <param name="path">Appended Search Path</param>
        /// <returns>Search path</returns>
        private string GetSearchPath(Type type, string path)
        {
            string p = ConfigurationAnnotationProcessor.GetAnnotationPath(type);

            if (!String.IsNullOrWhiteSpace(p))
            {
                if (String.IsNullOrWhiteSpace(path))
                {
                    return(p);
                }
                else
                {
                    return(String.Format("{0}/{1}", path, p));
                }
            }
            return(null);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Create/Get an instance of an autowired object.
        /// </summary>
        /// <typeparam name="T">Type of Object</typeparam>
        /// <param name="configName">Configuration Name (should be loaded)</param>
        /// <param name="path">Path in the configuration</param>
        /// <param name="update">Node path updated</param>
        /// <returns>Object Instance</returns>
        private T AutowireType <T>(string configName, string path)
        {
            Preconditions.CheckArgument(configName);

            Type   type = typeof(T);
            string key  = GetTypeKey(type, path, configName);

            if (!String.IsNullOrWhiteSpace(key))
            {
                if (!autowiredObjects.ContainsKey(key))
                {
                    lock (autowiredObjects)
                    {
                        if (!autowiredObjects.ContainsKey(key))
                        {
                            Configuration config = ReadLockConfig(configName, DEFAULT_READ_LOCK_TIMEOUT);
                            if (config != null)
                            {
                                try
                                {
                                    T value = default(T);

                                    value = Activator.CreateInstance <T>();
                                    autowiredObjects[key] = value;

                                    List <string> valuePaths = null;
                                    if (!String.IsNullOrWhiteSpace(path))
                                    {
                                        AbstractConfigNode node = config.Find(path);
                                        if (node == null)
                                        {
                                            throw new ConfigurationException(
                                                      String.Format("Specified configuration node not found. [config={0}][path={1}]", configName, path));
                                        }
                                        ConfigurationAnnotationProcessor.Process <T>(node, value, out valuePaths);
                                    }
                                    else
                                    {
                                        ConfigurationAnnotationProcessor.Process <T>(config, value, out valuePaths);
                                    }
                                    if (valuePaths != null && valuePaths.Count > 0 && (config.SyncMode == ESyncMode.BATCH || config.SyncMode == ESyncMode.EVENTS))
                                    {
                                        AutowiredIndexStruct ais = new AutowiredIndexStruct();
                                        ais.ConfigName   = configName;
                                        ais.Type         = type;
                                        ais.RelativePath = path;
                                        ais.Instance     = value;

                                        foreach (string vp in valuePaths)
                                        {
                                            string vk = GetTypeIndexKey(vp, configName);
                                            autowiredIndex.Add(vk, ais);
                                        }
                                    }
                                }
                                finally
                                {
                                    ConfigReleaseRead(config.Header.Name);
                                }
                            }
                            else
                            {
                                throw new ConfigurationException(
                                          String.Format("Error getting confguration. (Might be a lock timeout) [name={0}]", configName));
                            }
                        }
                    }
                }
                return((T)autowiredObjects[key]);
            }
            throw new ConfigurationException(
                      String.Format("Error creating autowired instance. [type={0}][config={1}]", type.FullName, configName));
        }