com.netflix.config
Class DynamicContextualProperty<T>

java.lang.Object
  extended by com.netflix.config.PropertyWrapper<T>
      extended by com.netflix.config.DynamicContextualProperty<T>
Type Parameters:
T - Data type of the property, e.g., Integer, Boolean
All Implemented Interfaces:
Property<T>

public class DynamicContextualProperty<T>
extends PropertyWrapper<T>

A property that has multiple possible values associated with it and determines the value according to the runtime context, which can include deployment context, values of other properties or attributes of user input.

The value is defined as a JSON blob that consists of multiple conditions and value associated with each condition. The following is an example:

[
    {
       "if":  
              {"@environment":["prod"],
               "@region":["us-east-1"]
              },
       "value":5
    },
     {
        "if":
             {"@environment":["test", "dev"]},
        "value":10
     },
    {
        "value":2
    }
]
 
This blob means that
  • If "@enviornment" is "prod" and "@region" is "us-east-1", value of the property is integer 5; (Note: if you use ConfigurationManager, "@enviornment" and "@region" are automatically exported as properties from DeploymentContext)
  • Else if "@environment" is either "test" or "dev", the value is 10;
  • Otherwise, the default value of the property is 2

    In order to make this work, a Predicate is needed to determine if the current runtime context matches any of the conditions described in the JSON blob. The predicate can be passed in as a parameter of the constructor, otherwise the default one will interpret each key in the "dimensions" as a key of a DynamicProperty, and the list of values are the acceptable values of the DynamicProperty.

    For example:

    String json = ... // string as the above JSON blob
          ConfigurationManager.getConfigInstance().setProperty("@environment", "test"); // no need to do this in real application as property is automatically set 
            ConfigurationManager.getConfigInstance().setProperty("contextualProp", json);
            DynamicContextualProperty<Integer> prop = new DynamicContextualProperty<Integer>("contextualProp", 0);
            prop.get(); // returns 10 as "@environment" == "test" matches the second condition 
     


    Nested Class Summary
    static class DynamicContextualProperty.Value<T>
               
     
    Field Summary
     
    Fields inherited from class com.netflix.config.PropertyWrapper
    defaultValue, prop
     
    Constructor Summary
    DynamicContextualProperty(java.lang.String propName, T defaultValue)
               
    DynamicContextualProperty(java.lang.String propName, T defaultValue, com.google.common.base.Predicate<java.util.Map<java.lang.String,java.util.Collection<java.lang.String>>> predicate)
               
     
    Method Summary
     T getValue()
              Get current typed value of the property.
    protected  void propertyChanged()
              Called when the property value is updated.
     
    Methods inherited from class com.netflix.config.PropertyWrapper
    addCallback, addValidator, getChangedTimestamp, getDefaultValue, getDynamicProperty, getName, propertyChanged, registerSubClassWithNoCallback, removeAllCallbacks, toString, validate
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     

    Constructor Detail

    DynamicContextualProperty

    public DynamicContextualProperty(java.lang.String propName,
                                     T defaultValue,
                                     com.google.common.base.Predicate<java.util.Map<java.lang.String,java.util.Collection<java.lang.String>>> predicate)

    DynamicContextualProperty

    public DynamicContextualProperty(java.lang.String propName,
                                     T defaultValue)
    Method Detail

    propertyChanged

    protected final void propertyChanged()
    Description copied from class: PropertyWrapper
    Called when the property value is updated. The default does nothing. Subclasses are free to override this if desired.

    Overrides:
    propertyChanged in class PropertyWrapper<T>

    getValue

    public T getValue()
    Description copied from class: PropertyWrapper
    Get current typed value of the property.

    Specified by:
    getValue in interface Property<T>
    Specified by:
    getValue in class PropertyWrapper<T>
    Returns:
    the latest property value