Netflix / archaius

Library for configuration management API
Apache License 2.0
2.47k stars 485 forks source link

Add ability to change dynamic property value based on context #102

Closed allenxwang closed 11 years ago

allenxwang commented 11 years ago

Currently dynamic property can change value based on changes in configuration and changes in deployment context. There is a need to further extend this capability to make the property value change based on other context at runtime like user input. For example, a web application may want certain dynamic property value changed based on one or more parameters in the http request.

The current proposal consists of the following component:

[
    {
       "dimensions":  
              {"d1":["v1","v2"]},
       "value":5
    },
     {
        "dimensions":
             {"d1":["v3"],"
               d2":["x1"]
             },
        "value":10
     },
    {
        "value":2
    }
]

This blob means that

If dimension "d1" has value of either "v1" or "v2", the value of the property is integer 5; Else if dimension "d1" has value "v3" and dimension "d2" has value "x1", the value of the property is 10; Otherwise, the value of the property is 2

    @Override
    public T getValue() {        
        if (values != null) {
            for (Value<T> v: values) {
                if (v.getDimensions() == null || v.getDimensions().isEmpty()
                        || predicate.apply(v.getDimensions())) {
                    return v.getValue();
                }
            }
        }
        return defaultValue;
    }
allenxwang commented 11 years ago

This type of new PropertyWrapper will also be made to understand single textual property value, same as DynamicIntProperty, DynamicBooleanProperty, etc, with a slight performance penalty compared with the latter on single textual value.

allenxwang commented 11 years ago

This enhancement will also provide solutions for Issue #74, since all possible values of the property are encoded as one JSON blob and a predicate can be used to select the most appropriate value.

allenxwang commented 11 years ago

added in 0.5.9.