Class AbstractPropertyDrivenActivationStrategy

  • All Implemented Interfaces:
    ActivationStrategy
    Direct Known Subclasses:
    SystemPropertyActivationStrategy

    public abstract class AbstractPropertyDrivenActivationStrategy
    extends Object
    implements ActivationStrategy

    An abstract activation strategy that is designed to support cases where the activation of a feature is driven based on the value of environmental/contextual properties.

    AbstractPropertyDrivenActivationStrategy allows the name of the property can be passed via the ""name"" parameter and gracefully falls back on a property name that is derived from the Feature itself (e.g. ""togglz."FEATURE_NAME"). It will take care of the majority of the work and only really requires implementations to provide the means to lookup the value of the property:

     @Override
     protected abstract String getPropertyValue(FeatureState featureState, FeatureUser user, String name) {
         return doSomeStuffToGetPropertyValue(name);
     }
     

    By default, the value of the property will be converted into a boolean but implementations are free to override this, where needed, by overriding the isActive(FeatureState, FeatureUser, String, String) method. However, it would be ideal if all implementations tried to support the same value formats to allow for a unified experience. The default conversion will fail fast if the property value does not match any of the predefined boolean representations by throwing an IllegalArgumentException.

    All implementations should honor the rule that the feature should not be activated if no matching property is found.

    Author:
    Alasdair Mercer
    See Also:
    getPropertyValue(FeatureState, FeatureUser, String)
    • Constructor Detail

      • AbstractPropertyDrivenActivationStrategy

        public AbstractPropertyDrivenActivationStrategy()
    • Method Detail

      • getPropertyName

        protected String getPropertyName​(FeatureState featureState,
                                         String parameterName)

        Returns the name of the property on which to base the activation of the feature.

        This method will first attempt to use the value of the parameter with the name provided. If that does not return a valid property name (i.e. non-blank), then a property name will be constructed using a common prefix (""togglz."") and the name of the feature.

        Parameters:
        featureState - the FeatureState which represents the current configuration of the feature
        parameterName - the name of the parameter that potentially contains the property name
        Returns:
        The name of the property.
      • getPropertyValue

        protected abstract String getPropertyValue​(FeatureState featureState,
                                                   FeatureUser user,
                                                   String name)

        Returns the value of the property with the specified name on which to base the activation of the feature.

        Parameters:
        featureState - the FeatureState which represents the current configuration of the feature
        user - the user for which to decide whether the feature is active (may be null)
        name - the name of the property whose value is to be returned
        Returns:
        The (raw) value of the property with the given name or null if none could be found.
      • isActive

        public final boolean isActive​(FeatureState featureState,
                                      FeatureUser user)
        Description copied from interface: ActivationStrategy
        This method is responsible to decide whether a feature is active or not. The implementation can use the custom configuration parameters of the strategy stored in the feature state and information from the currently acting user to find a decision.
        Specified by:
        isActive in interface ActivationStrategy
        Parameters:
        featureState - The feature state which represents the current configuration of the feature. The implementation of the method typically uses FeatureState.getParameter(String) to access custom configuration parameter values.
        user - The user for which to decide whether the feature is active. May be null if the user could not be identified by the UserProvider.
        Returns:
        true if the feature should be active, else false
      • getPropertyNameParam

        protected String getPropertyNameParam()
      • isActive

        protected boolean isActive​(FeatureState featureState,
                                   FeatureUser user,
                                   String propertyName,
                                   String propertyValue)

        This method is called by isActive(FeatureState, FeatureUser) with the property name and value to make the decision as to whether the feature is active.

        By default, this method will convert propertyValue into a boolean using Strings.toBoolean(String) but implementations are free to override this, where needed. However, it would be ideal if all implementations tried to support the same value formats to allow for a unified experience. The default implementation throw an IllegalArgumentException if propertyValue does not match any of the predefined boolean representations.

        This method should never return true if propertyValue is null.

        Parameters:
        featureState - the FeatureState which represents the current configuration of the feature
        user - the user for which to decide whether the feature is active (may be null)
        propertyName - the name of the property on which to base the activation of the feature
        propertyValue - the (raw) value of the property on which to base the activation of the feature (may be null if none was found)
        Returns:
        true if the feature should be active; otherwise false.
        Throws:
        IllegalArgumentException - If propertyValue is non-null and does not match any of the predefined boolean representations.
      • getParameters

        public Parameter[] getParameters()
        Description copied from interface: ActivationStrategy

        Returns the list of configuration parameter definitions for the strategy. Parameters are typically built using a ParameterBuilder class but users can also create custom implementations of the Parameter interface.

        Example:

         public Parameter[] getParameters() {
             return new Parameter[] {
                     ParameterBuilder.create("country").label("Country Code").matching("[A-Z]+")
             };
         }
         
        Specified by:
        getParameters in interface ActivationStrategy
        See Also:
        ParameterBuilder