Beispiel #1
0
        /// <summary>
        /// Loads the properties from public and private configurations, optionally from a certain configuration defined by
        /// "TEST_CONFIG" environment variable.
        /// </summary>
        /// <param name="type">The type, the fully qualified name (FQN) of which to be used to identify the configuration.</param>
        /// <param name="assembly">The assembly, that contains the configuration resources.</param>
        /// <returns>Bag of properties for the specified <paramref name="type"/>.</returns>
        /// <exception cref="InvalidOperationException">Thrown when the root directory of the private configuration cannot be determined.</exception>
        static PropertyBag LoadProperties(Type type, Assembly assembly)
        {
            string bagName        = type.FullName;
            string configFilePath = string.Empty;
            IDictionary <string, object> properties = null;
            var ret = new Dictionary <string, object>();

            //load the general public properties file
            configFilePath = string.Format(CultureInfo.InvariantCulture, "{0}.config.{1}", bagName, ConfigFileName);
            properties     = LoadConfigurationFromResource(assembly, configFilePath);
            CollectionUtil.AddOrReplaceAll(ret, properties);

            //load the configuration specific public properties file
            string configurationName = Environment.GetEnvironmentVariable(TestConfigEVName);

            if (!StringUtil.IsBlank(configurationName))
            {
                configFilePath = string.Format(CultureInfo.InvariantCulture, "{0}.config.{1}.{2}", bagName,
                                               configurationName, ConfigFileName);
                properties = LoadConfigurationFromResource(assembly, configFilePath);
                CollectionUtil.AddOrReplaceAll(ret, properties);
            }

            //determine the root directory of the private properties files
            string privateConfigRoot = string.Empty;

            if (Environment.GetEnvironmentVariable(PrivateConfigRootEVName) != null)
            {
                privateConfigRoot = Environment.GetEnvironmentVariable(PrivateConfigRootEVName);
            }
            else
            {
                if (Environment.GetEnvironmentVariable(UserProfileEVName) != null)
                {
                    privateConfigRoot = Path.Combine(Environment.GetEnvironmentVariable(UserProfileEVName),
                                                     Path.Combine(".connectors", type.Assembly.GetName().Name));
                }
                else
                {
                    throw new InvalidOperationException(
                              @"Neither the ""PRIVATE_CONFIG_ROOT"" nor the ""USERPROFILE"" environment variable is defined.");
                }
            }

            privateConfigRoot = Path.Combine(privateConfigRoot, "config");

            //load the general private properties file
            configFilePath = Path.Combine(Path.Combine(Path.Combine(privateConfigRoot, bagName), "config-private"), ConfigFileName);
            properties     = LoadConfigurationFromFile(configFilePath);
            CollectionUtil.AddOrReplaceAll(ret, properties);

            // load the configuration specific private properties file
            if (!StringUtil.IsBlank(configurationName))
            {
                configFilePath = Path.Combine(Path.Combine(Path.Combine(Path.Combine(privateConfigRoot, bagName),
                                                                        "config-private"), configurationName), ConfigFileName);
                properties = LoadConfigurationFromFile(configFilePath);
                CollectionUtil.AddOrReplaceAll(ret, properties);
            }
            return(new PropertyBag(ret));
        }