Class AbstractPropertyDrivenActivationStrategy
- All Implemented Interfaces:
ActivationStrategy
- Direct Known Subclasses:
SystemPropertyActivationStrategy
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:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the list of configuration parameter definitions for the strategy.protected StringgetPropertyName(FeatureState featureState, String parameterName) Returns the name of the property on which to base the activation of the feature.protected Stringprotected abstract StringgetPropertyValue(FeatureState featureState, FeatureUser user, String name) Returns the value of the property with the specifiednameon which to base the activation of the feature.final booleanisActive(FeatureState featureState, FeatureUser user) This method is responsible to decide whether a feature is active or not.protected booleanisActive(FeatureState featureState, FeatureUser user, String propertyName, String propertyValue) This method is called byisActive(FeatureState, FeatureUser)with the property name and value to make the decision as to whether the feature is active.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.togglz.core.spi.ActivationStrategy
getId, getName
-
Field Details
-
PARAM_NAME
- See Also:
-
PARAM_PROPERTY_VALUE
- See Also:
-
-
Constructor Details
-
AbstractPropertyDrivenActivationStrategy
public AbstractPropertyDrivenActivationStrategy()
-
-
Method Details
-
getPropertyName
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- theFeatureStatewhich represents the current configuration of the featureparameterName- 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
nameon which to base the activation of the feature.- Parameters:
featureState- theFeatureStatewhich represents the current configuration of the featureuser- theuserfor 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
nameor null if none could be found.
-
isActive
Description copied from interface:ActivationStrategyThis 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:
isActivein interfaceActivationStrategy- Parameters:
featureState- The feature state which represents the current configuration of the feature. The implementation of the method typically usesFeatureState.getParameter(String)to access custom configuration parameter values.user- The user for which to decide whether the feature is active. May benullif the user could not be identified by theUserProvider.- Returns:
trueif the feature should be active, elsefalse
-
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
propertyValueinto a boolean usingStrings.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 anIllegalArgumentExceptionifpropertyValuedoes not match any of the predefined boolean representations.This method should never return true if
propertyValueis null.- Parameters:
featureState- theFeatureStatewhich represents the current configuration of the featureuser- theuserfor 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 featurepropertyValue- 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- IfpropertyValueis non-null and does not match any of the predefined boolean representations.
-
getParameters
Description copied from interface:ActivationStrategyReturns the list of configuration parameter definitions for the strategy. Parameters are typically built using a
ParameterBuilderclass but users can also create custom implementations of theParameterinterface.Example:
public Parameter[] getParameters() { return new Parameter[] { ParameterBuilder.create("country").label("Country Code").matching("[A-Z]+") }; }- Specified by:
getParametersin interfaceActivationStrategy- See Also:
-