Пример #1
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));
                            }
                        }
                    }
                }
            }
        }
Пример #2
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));
        }