1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::collections::HashMap;

use lang_extension::any::*;
use lang_extension::ops::function::*;

use crate::property::*;
use crate::source::*;

pub mod default;

pub trait ConfigurationManagerConfig: Value + Send + Sync {

    /// for description use
    fn get_name(&self) -> &str;

    /// key for the source priority, value for the source
    /// 
    /// the greater the key is, the higher the priority is
    /// 
    /// non-null, non-empty
    fn get_sources(&self) -> &Vec<Box<dyn ConfigurationSource>>;

    /// thread pool for property value update and property change listeners
    /// 
    /// by default, property value update and property change listeners
    /// will be done in the source change raising thread
    /// 
    /// if property count is too large, or the property change listeners are too slow,
    /// it's better to use an async thread pool
    fn get_task_executor(&self) -> &dyn Fn(&Box<dyn Fn()>);

as_boxed!(ConfigurationManagerConfig);
}

boxed_value_trait!(ConfigurationManagerConfig);

pub trait ConfigurationManagerConfigBuilder {

    /// required
    fn set_name(&mut self, name: &str) -> &mut dyn ConfigurationManagerConfigBuilder;

    /// required
    fn add_source(&mut self, priority: i32, source: Box<dyn ConfigurationSource>)
        -> &mut dyn ConfigurationManagerConfigBuilder;

    /// required
    fn add_sources(&mut self, sources: HashMap<i32, Box<dyn ConfigurationSource>>)
        -> &mut dyn ConfigurationManagerConfigBuilder;

    /// optional
    fn set_task_executor(&mut self, task_executor: ConsumerRef<Box<dyn Fn()>>)
        -> &mut dyn ConfigurationManagerConfigBuilder;

    fn build(&self) -> Box<dyn ConfigurationManagerConfig>;

}

pub trait ConfigurationManager: Value + Send + Sync {

    fn get_config(&self) -> &dyn ConfigurationManagerConfig;

    /// the properties created by get_property
    fn get_properties(&self) -> Vec<Box<dyn RawProperty>>;

    /// get property value by get_property_value
    /// and return a property with the propertyConfig and value
    /// 
    /// once a property is created, it is kept by the manager and will be auto-update after some configuration source changed
    /// 
    /// same property config in, same property out, 1 key 1 property
    fn get_property(&self, config: &dyn RawPropertyConfig) -> Box<dyn RawProperty>;

    /// get property value in each configuration source by source priority
    /// 
    /// if non-None value is got by a source ConfigurationSource::get_property_value
    /// if PropertyConfig::get_value_filter() is None, return the non-None value,
    /// if PropertyConfig::get_value_filter() is non-None, apply the filter to the non-None value,
    /// if the new value returned by the filter is non-None, return the new value.
    /// otherwise, go to the next lower priority source
    /// 
    /// after handling all sources, no non-None value got, return None
    fn get_property_value(&self, config: &dyn RawPropertyConfig) -> Option<Box<dyn Value>>;

    /// listeners to the property change, notified once property changed
    fn add_raw_change_listener(&self, listener: RawPropertyChangeListener);

as_boxed!(ConfigurationManager);
}

boxed_value_trait!(ConfigurationManager);