示例#1
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // create a configuration manager with single source
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);

            // default to null
            _appId = _properties.GetStringProperty("app.id");

            // default to "unknown"
            _appName = _properties.GetStringProperty("app.name", "unknown");

            // default to empty list
            _userList = _properties.GetListProperty("user.list", new List <string>());

            // default to empty map
            _userData = _properties.GetDictionaryProperty("user.data", new Dictionary <string, string>());

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            _sleepTime = _properties.GetIntProperty("sleep.time", 1000, v => v < 0 ? null : v);

            // custom type property
            _myCustomData = _properties.GetProperty("my-custom-type-property", MyCustomType.Converter);
        }
        public virtual void TestDemo()
        {
            // create an apollo config
            IConfig appConfig = ApolloConfigurationManager.GetAppConfig().Result;

            // create a scf apollo configuration source
            ApolloConfigurationSourceConfig sourceConfig = new ApolloConfigurationSourceConfig.Builder()
                                                           .SetName("apollo-source").SetApolloConfig(appConfig).Build();
            ApolloConfigurationSource source = new ApolloConfigurationSource(sourceConfig);

            // create scf manager & properties facade tool
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("my-app")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager    = ConfigurationManagers.NewManager(managerConfig);
            StringProperties      properties = new StringProperties(manager);

            // use properties
            IProperty <string, string> appName = properties.GetStringProperty("app.name", "unknown");

            Console.WriteLine("app name: " + appName.Value);

            // add listener for dynamic property
            IProperty <string, int?> requestTimeout = properties.GetIntProperty("request.timeout", 1000);

            Console.WriteLine("request timeout: " + requestTimeout.Value);
            requestTimeout.OnChange += (o, e) => Console.WriteLine("do something");
        }
示例#3
0
        public virtual void TestDuplicatePrioritySource()
        {
            TestConfigurationSource        source1 = CreateSource();
            TestDynamicConfigurationSource source2 = CreateDynamicSource();

            Assert.Throws <ArgumentException>(
                () => ConfigurationManagers.NewConfigBuilder().AddSource(1, source1).AddSource(1, source2));
        }
示例#4
0
        protected virtual IConfigurationManager CreateManager(Dictionary <int, IConfigurationSource> sources)
        {
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                       .AddSources(sources).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
示例#5
0
        protected virtual IConfigurationManager CreateManager()
        {
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                       .AddSource(1, StringPropertySources.NewEnvironmentVariableSource("environment-variable")).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
        protected virtual IConfigurationManager CreateManager(MemoryDictionaryConfigurationSource source)
        {
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                       .AddSource(1, source).SetTaskExecutor(new TaskExecutor().Run).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
示例#7
0
        public virtual void TestDemo()
        {
            // create a scf yaml configuration source
            YamlFileConfigurationSourceConfig sourceConfig = new YamlFileConfigurationSourceConfig.Builder()
                                                             .SetName("yaml-file").SetFileName("test.yaml").Build();
            YamlFileConfigurationSource source = new YamlFileConfigurationSource(sourceConfig);

            // create scf manager & properties facade tool
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("my-app")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            PropertyConfig <string, bool?> propertyConfig1 = ConfigurationProperties.NewConfigBuilder <string, bool?>()
                                                             .SetKey("booleanProperty").SetDefaultValue(false).Build();
            bool?boolValue = manager.GetPropertyValue(propertyConfig1);

            Console.WriteLine(boolValue);

            PropertyConfig <string, int?> propertyConfig2 = ConfigurationProperties.NewConfigBuilder <string, int?>()
                                                            .SetKey("intProperty").SetDefaultValue(0).Build();
            int?intValue = manager.GetPropertyValue(propertyConfig2);

            Console.WriteLine(intValue);

            PropertyConfig <string, long?> propertyConfig3 = ConfigurationProperties.NewConfigBuilder <string, long?>()
                                                             .SetKey("longProperty").SetDefaultValue(0L).Build();
            long?longValue = manager.GetPropertyValue(propertyConfig3);

            Console.WriteLine(longValue);

            PropertyConfig <string, string> propertyConfig4 = ConfigurationProperties.NewConfigBuilder <string, string>()
                                                              .SetKey("stringProperty").Build();
            string stringValue = manager.GetPropertyValue(propertyConfig4);

            Console.WriteLine(stringValue);

            PropertyConfig <string, List <string> > propertyConfig5 = ConfigurationProperties
                                                                      .NewConfigBuilder <string, List <string> >().SetKey("listProperty").Build();
            List <string> listValue = manager.GetPropertyValue(propertyConfig5);

            Console.WriteLine(listValue == null ? null : string.Join(", ", listValue));

            PropertyConfig <string, Dictionary <string, string> > propertyConfig6 = ConfigurationProperties
                                                                                    .NewConfigBuilder <string, Dictionary <string, string> >().SetKey("mapProperty").Build();
            Dictionary <string, string> mapValue = manager.GetPropertyValue(propertyConfig6);

            Console.WriteLine(mapValue == null ? null :
                              (string.Join(", ", mapValue.Select(p => string.Format("{0}: {1}", p.Key, p.Value)).ToList())));

            PropertyConfig <string, TestPojo> propertyConfig7 = ConfigurationProperties.NewConfigBuilder <string, TestPojo>()
                                                                .SetKey("objProperty").Build();
            TestPojo objValue = manager.GetPropertyValue(propertyConfig7);

            Console.WriteLine(objValue);
        }
示例#8
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // create a configuration manager with single source
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).Build();

            _manager = ConfigurationManagers.NewManager(managerConfig);

            // default to null
            PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>()
                                                             .SetKey("app.id").Build();

            _appId = _manager.GetProperty(propertyConfig);

            // default to "unknown"
            PropertyConfig <string, string> propertyConfig2 = ConfigurationProperties.NewConfigBuilder <string, string>()
                                                              .SetKey("app.name").SetDefaultValue("unknown").Build();

            _appName = _manager.GetProperty(propertyConfig2);

            // default to empty list
            PropertyConfig <string, List <string> > propertyConfig3 = ConfigurationProperties
                                                                      .NewConfigBuilder <string, List <string> >().SetKey("user.list")
                                                                      .SetDefaultValue(new List <string>()).AddValueConverter(StringToListConverter.Default).Build();

            _userList = _manager.GetProperty(propertyConfig3);

            // default to empty map
            PropertyConfig <string, Dictionary <string, string> > propertyConfig4 = ConfigurationProperties
                                                                                    .NewConfigBuilder <string, Dictionary <string, string> >().SetKey("user.data")
                                                                                    .SetDefaultValue(new Dictionary <string, string>()).AddValueConverter(StringToDictionaryConverter.Default).Build();

            _userData = _manager.GetProperty(propertyConfig4);

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            PropertyConfig <string, int?> propertyConfig5 = ConfigurationProperties.NewConfigBuilder <string, int?>()
                                                            .SetKey("sleep.time").SetDefaultValue(1000)
                                                            .AddValueConverter(StringToIntConverter.Default).SetValueFilter(v => v < 0 ? null : v).Build();

            _sleepTime = _manager.GetProperty(propertyConfig5);

            // custom type property
            PropertyConfig <string, MyCustomType> propertyConfig6 = ConfigurationProperties
                                                                    .NewConfigBuilder <string, MyCustomType>().SetKey("my-custom-type-property")
                                                                    .AddValueConverter(MyCustomType.Converter).Build();

            _myCustomData = _manager.GetProperty(propertyConfig6);
        }
示例#9
0
        protected virtual IConfigurationManager CreateManager(AppSettingsType type)
        {
            AppSettingsConfigurationSourceConfig sourceConfig = StringPropertySources.NewAppSettingsSourceConfigBuilder()
                                                                .SetName("appsettings").SetType(type).Build();
            AppSettingsConfigurationSource source        = StringPropertySources.NewAppSettingsSource(sourceConfig);
            ConfigurationManagerConfig     managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                           .AddSource(1, source).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
        protected virtual IConfigurationManager CreateManager(string[] commandLineArgs)
        {
            CommandLineConfigurationSourceConfig sourceConfig = StringPropertySources.NewCommandLineSourceConfigBuilder()
                                                                .SetName("commandline").SetCommandLineArgs(commandLineArgs).Build();
            CommandLineConfigurationSource source        = StringPropertySources.NewCommandLineSource(sourceConfig);
            ConfigurationManagerConfig     managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                           .AddSource(1, source).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
        protected virtual IConfigurationManager CreateKeyCachedManager(MemoryDictionaryConfigurationSource source)
        {
            CascadedConfigurationSourceConfig <ConfigurationSourceConfig> sourceConfig = StringPropertySources.NewCascadedSourceConfigBuilder <ConfigurationSourceConfig>()
                                                                                         .SetName("cascaded-memory-map").SetKeySeparator(".").AddCascadedFactor("part1")
                                                                                         .AddCascadedFactor("part2").SetSource(source).Build();
            CascadedConfigurationSource <ConfigurationSourceConfig> cascadedSource = new KeyCachedCascadedConfigurationSource <ConfigurationSourceConfig>(sourceConfig);
            TaskExecutor taskExecutor = new TaskExecutor();
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("test")
                                                       .AddSource(1, cascadedSource).SetTaskExecutor(taskExecutor.Run).Build();

            Console.WriteLine("manager config: " + managerConfig + "\n");
            return(ConfigurationManagers.NewManager(managerConfig));
        }
示例#12
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // crate a dynamic K/V (string/string) configuration source
            _systemPropertiesSource = StringPropertySources.NewMemoryDictionarySource("system-property");

            // create a configuration manager with 2 sources, system property has higher priority then app.properties
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).AddSource(2, _systemPropertiesSource).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // Add a log listener
            manager.OnChange += (o, e) =>
                                Console.WriteLine("\nproperty {0} changed, old value: {1}, new value: {2}, changeTime: {3}",
                                                  e.Property, e.OldValue, e.NewValue, e.ChangeTime);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);

            // default to null
            _appId = _properties.GetStringProperty("app.id");

            // default to "unknown"
            _appName = _properties.GetStringProperty("app.name", "unknown");
            // Add change listener to app.name
            _appName.OnChange += (o, e) => Console.WriteLine("\napp.name changed, maybe we need do something");

            // default to empty list
            _userList = _properties.GetListProperty("user.list", new List <string>());

            // default to empty map
            _userData = _properties.GetDictionaryProperty("user.data", new Dictionary <string, string>());

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            _sleepTime = _properties.GetIntProperty("sleep.time", 1000, v => v < 0 ? null : v);

            // custom type property
            _myCustomData = _properties.GetProperty("my-custom-type-property", MyCustomType.Converter);
        }
示例#13
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);
        }
示例#14
0
        private void InitConfig()
        {
            // create a non-dynamic K/V (String/String) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("properties-file")
                                                                   .SetFileName("own-component.properties").Build();
            IConfigurationSource source1 = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // create other dynamic or non-dynamic sources
            // ...

            // create a configuration manager with single source
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("own-component")
                                                       .AddSource(1, source1)
                                                       // Add other sources
                                                       .Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);
        }