예제 #1
0
        private static bool TryGet(Dictionary <string, string> argsDict, string key, ConfigurationSources sources, out string value)
        {
            bool   got = false;
            string val = string.Empty;

            if (sources.HasFlag(ConfigurationSources.CommandLine))
            {
                got = argsDict.TryGetValue(key, out val);
            }

            if (!got && sources.HasFlag(ConfigurationSources.AppConfig))
            {
                val = ConfigurationManager.AppSettings[key];
                if (!string.IsNullOrWhiteSpace(val))
                {
                    got = true;
                }
            }

            if (!got && sources.HasFlag(ConfigurationSources.Environment))
            {
                val = Environment.GetEnvironmentVariable(key);
                got = !string.IsNullOrWhiteSpace(val);
            }

            value = val;
            return(got);
        }
예제 #2
0
 static IEnumerable <ConfigurationSources> GetLocations(ConfigurationSources input)
 {
     foreach (ConfigurationSources value in Enum.GetValues(input.GetType()))
     {
         if (input.HasFlag(value))
         {
             yield return(value);
         }
     }
 }
예제 #3
0
        public static string GetMessage(string configKey, Type type, ConfigurationSources sources)
        {
            var message =
                $"Configuration for key '{configKey}' was not found. Please supply a {type} at one of the following sources:";

            foreach (var configurationLocation in GetLocations(sources))
            {
                message += $"\n{configurationLocation}";
            }

            return(message);
        }
예제 #4
0
        protected override IConfigurationManager CreateManager(Dictionary <int, IConfigurationSource> sources)
        {
            Dictionary <int, IConfigurationSource> sourceList = new Dictionary <int, IConfigurationSource>(sources);
            ConfigurationSourceConfig      config             = ConfigurationSources.NewConfig("labeled-source");
            TestLabeledConfigurationSource source             = CreateLabeledSource(config);

            sourceList[int.MaxValue - 1] = source;
            config = ConfigurationSources.NewConfig("dynamic-labeled-source");
            source = CreateDynamicLabeledSource(config);
            sourceList[int.MaxValue] = source;

            return(CreateLabeledManager(sourceList));
        }
예제 #5
0
        protected virtual TestDynamicConfigurationSource CreateDynamicSource()
        {
            ConfigurationSourceConfig   sourceConfig = ConfigurationSources.NewConfig("test-source");
            Dictionary <string, string> properties   = new Dictionary <string, string>();

            properties["exist"]  = "ok.2";
            properties["exist2"] = "ok2.2";
            properties["exist3"] = "ok3.2";
            properties["exist4"] = "ok4.2";
            TestDynamicConfigurationSource source = new TestDynamicConfigurationSource(sourceConfig, properties);

            Console.WriteLine("source config: " + sourceConfig + "\n");
            return(source);
        }
예제 #6
0
        public LargeApp()
        {
            // OwnManagerComponent both users config and defines how to config properties itself
            _ownManagerComponent = new OwnManagerComponent();

            // OuterManagerComponent only uses config, let the component's user to define how to config
            // each user can define different config way
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("properties-file")
                                                                   .SetFileName("outer-component.properties").Build();
            IConfigurationSource       source        = StringPropertySources.NewPropertiesFileSource(sourceConfig);
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("outer-component")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            _outerManagerComponent = new OuterManagerComponent(manager);

            // LabeledPropertyComponent only uses config, let the component's user to define how to config
            // each user can define different config way
            YamlFileConfigurationSourceConfig labeledSourceConfig = new YamlFileConfigurationSourceConfig.Builder()
                                                                    .SetName("yaml-file").SetFileName("labeled-property-component.yaml").Build();
            IConfigurationSource         labeledSource        = new YamlDcConfigurationSource(labeledSourceConfig);
            DynamicDcConfigurationSource dynamicLabeledSource = new DynamicDcConfigurationSource(
                ConfigurationSources.NewConfig("dynamic-labeled-source"));
            ConfigurationManagerConfig managerConfig2 = ConfigurationManagers.NewConfigBuilder()
                                                        .SetName("labeled-property-component").AddSource(1, labeledSource).AddSource(2, dynamicLabeledSource)
                                                        .Build();
            ILabeledConfigurationManager manager2 = LabeledConfigurationManagers.NewManager(managerConfig2);

            _labeledPropertyComponent = new LabeledPropertyComponent(manager2);

            // large app's config
            PropertiesFileConfigurationSourceConfig sourceConfig2 = StringPropertySources
                                                                    .NewPropertiesFileSourceConfigBuilder().SetName("properties-file").SetFileName("app.properties")
                                                                    .Build();
            IConfigurationSource       source2        = StringPropertySources.NewPropertiesFileSource(sourceConfig2);
            ConfigurationManagerConfig managerConfig3 = ConfigurationManagers.NewConfigBuilder().SetName("outer-component")
                                                        .AddSource(1, source2).Build();
            IConfigurationManager manager3 = ConfigurationManagers.NewManager(managerConfig3);

            _appProperties = new StringProperties(manager3);
        }
예제 #7
0
        public virtual void TestGetLabeledPropertyValuePerf()
        {
            ConfigurationSourceConfig      config        = ConfigurationSources.NewConfig("labeled-source");
            TestLabeledConfigurationSource labeledSource = CreateLabeledSource(config);

            config = ConfigurationSources.NewConfig("dynamic-labeled-source");
            TestDynamicLabeledConfigurationSource dynamicLabeledSource = CreateDynamicLabeledSource(config);
            ILabeledConfigurationManager          manager = CreateLabeledManager(
                new Dictionary <int, IConfigurationSource>()
            {
                { 1, labeledSource }, { 2, dynamicLabeledSource }
            });

            List <IPropertyLabel> labels = new List <IPropertyLabel>();

            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1"));
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1"));
            PropertyLabels      propertyLabels = LabeledConfigurationProperties.NewLabels(labels);
            LabeledKey <string> key            = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1")
                                                 .SetPropertyLabels(propertyLabels).Build();
            PropertyConfig <LabeledKey <string>, string> propertyConfig = ConfigurationProperties
                                                                          .NewConfigBuilder <LabeledKey <string>, string>().SetKey(key).SetDefaultValue("default-value-1").Build();
            String propertyValue = manager.GetPropertyValue(propertyConfig);

            Assert.Equal("v-1-2", propertyValue);

            int      times = 100 * 1000;
            DateTime start = DateTime.Now;

            for (int i = 0; i < times; i++)
            {
                propertyValue = manager.GetPropertyValue(propertyConfig);
            }
            DateTime end     = DateTime.Now;
            TimeSpan elipsed = end - start;

            Console.WriteLine("{0} times GetPropertyValue, total time: {1}, avg time: {2}",
                              times, elipsed.TotalMilliseconds, (double)elipsed.TotalMilliseconds / times);
        }
        public ConfigurationValue this[string key, string defaultValue = null, bool callConfigService = false]
        {
            get
            {
                ConfigurationSources source = ConfigurationSources.NotFound;

                string value = NetCoreConfiguration?[key];
                if (!string.IsNullOrEmpty(value))
                {
                    source = ConfigurationSources.NetCoreConfiguration;
                }
                else
                {
                    value  = DefaultConfiguration[key];
                    source = ConfigurationSources.DefaultConfiguration;
                }

                if (string.IsNullOrEmpty(value) && AppSettings.ContainsKey(key))
                {
                    value  = AppSettings[key];
                    source = ConfigurationSources.ConfigAppSettings;
                }

                if (string.IsNullOrEmpty(value))
                {
                    value  = BamEnvironmentVariables.GetBamVariable(key);
                    source = ConfigurationSources.BamEnvironmentVariable;
                }

                if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(defaultValue))
                {
                    value  = defaultValue;
                    source = ConfigurationSources.DefaultValue;
                }

                if (string.IsNullOrEmpty(value) && callConfigService)
                {
                    value  = FromService(key);
                    source = ConfigurationSources.ConfigurationService;
                }

                if (string.IsNullOrEmpty(value))
                {
                    FireEvent(ConfigurationValueNotFound, new ConfigurationEventArgs {
                        Key = key
                    });
                }

                if (!string.IsNullOrEmpty(value))
                {
                    Config.AppSettings.AddMissing(key, value);
                    Config.Save();
                }

                return(new ConfigurationValue(value)
                {
                    Key = key,
                    DefaultValue = defaultValue,
                    CallConfigService = callConfigService,
                    ConfigurationSource = source
                });
            }
        }
예제 #9
0
        public virtual void TestGetLabeledProperty()
        {
            ConfigurationSourceConfig      config        = ConfigurationSources.NewConfig("labeled-source");
            TestLabeledConfigurationSource labeledSource = CreateLabeledSource(config);

            config = ConfigurationSources.NewConfig("dynamic-labeled-source");
            TestDynamicLabeledConfigurationSource dynamicLabeledSource = CreateDynamicLabeledSource(config);
            ILabeledConfigurationManager          manager = CreateLabeledManager(
                new Dictionary <int, IConfigurationSource>()
            {
                { 1, labeledSource }, { 2, dynamicLabeledSource }
            });

            List <IPropertyLabel> labels = new List <IPropertyLabel>();

            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1"));
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1"));
            PropertyLabels      propertyLabels = LabeledConfigurationProperties.NewLabels(labels);
            LabeledKey <string> key            = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1")
                                                 .SetPropertyLabels(propertyLabels).Build();
            PropertyConfig <LabeledKey <string>, string> propertyConfig = ConfigurationProperties
                                                                          .NewConfigBuilder <LabeledKey <string>, string>().SetKey(key).SetDefaultValue("default-value-1").Build();
            IProperty <LabeledKey <string>, string> property = manager.GetProperty(propertyConfig);

            Console.WriteLine(property);
            Assert.Equal("v-1-2", property.Value);

            labels = new List <IPropertyLabel>();
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist"));
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1"));
            propertyLabels = LabeledConfigurationProperties.NewLabels(labels);
            key            = LabeledConfigurationProperties.NewKeyBuilder <String>().SetKey("labeled-key-1")
                             .SetPropertyLabels(propertyLabels).Build();
            propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, String>().SetKey(key)
                             .SetDefaultValue("default-value-1").Build();
            property = manager.GetProperty(propertyConfig);
            Console.WriteLine(property);
            Assert.Equal("default-value-1", property.Value);

            propertyLabels = LabeledConfigurationProperties.NewLabels(
                LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1"),
                LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1"));
            labels = new List <IPropertyLabel>();
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist"));
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1"));
            propertyLabels = LabeledConfigurationProperties.NewLabels(labels, propertyLabels);
            key            = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1")
                             .SetPropertyLabels(propertyLabels).Build();
            propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, string>().SetKey(key)
                             .SetDefaultValue("default-value-1").Build();
            property = manager.GetProperty(propertyConfig);
            Console.WriteLine(property);
            Assert.Equal("v-1-2", property.Value);

            TestDataCenterSetting Setting = new TestDataCenterSetting("labeled-key-1", "v-4-2", "sh-1-not-exist", "app-1");

            dynamicLabeledSource.updateSetting(Setting);
            Thread.Sleep(10);
            Console.WriteLine(property);
            Assert.Equal("v-4-2", property.Value);

            dynamicLabeledSource.removeSetting(Setting);
            Thread.Sleep(10);
            Console.WriteLine(property);
            Assert.Equal("v-1-2", property.Value);

            labels = new List <IPropertyLabel>();
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist"));
            labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1"));
            propertyLabels = LabeledConfigurationProperties.NewLabels(labels, PropertyLabels.Empty);
            key            = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1")
                             .SetPropertyLabels(propertyLabels).Build();
            propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, string>().SetKey(key)
                             .SetDefaultValue("default-value-1").Build();
            property = manager.GetProperty(propertyConfig);
            Console.WriteLine(property);
            Assert.Equal("v-0-2", property.Value);
        }
예제 #10
0
 public ConfigurationMissingException(string configKey, Type type, ConfigurationSources sources)
     : base(GetMessage(configKey, type, sources))
 {
 }
예제 #11
0
 public ConfigurationAttribute(string key, ConfigurationSources configurationSources)
 {
     ConfigurationSources = configurationSources;
     Key = key;
 }