Skip to content

Instantly share code, notes, and snippets.

@gugacavalieri
Last active September 23, 2019 13:31
Show Gist options
  • Save gugacavalieri/ba8f84b1ce9942ed6e444ccc3b91fb6d to your computer and use it in GitHub Desktop.
Save gugacavalieri/ba8f84b1ce9942ed6e444ccc3b91fb6d to your computer and use it in GitHub Desktop.
Togglz User Feature
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.togglz.core.Feature;
import org.togglz.core.context.FeatureContext;
import org.togglz.core.manager.FeatureManager;
import org.togglz.core.repository.FeatureState;
import org.togglz.core.spi.ActivationStrategy;
import org.togglz.core.user.SimpleFeatureUser;
public enum TogglzUserFeatureImpl implements Feature {
SOME_TOGGLE_NAME;
private static FeatureManager featureManager;
private static final Logger LOGGER = LoggerFactory.getLogger(TogglzUserFeatureImpl.class);
public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}
/**
* check if a toggle is active for a certain user
* @param uCode User UCode
* @return boolean flag
*/
public boolean isActiveForUser(String uCode) {
try {
SimpleFeatureUser simpleFeatureUser = new SimpleFeatureUser(uCode);
FeatureState featureState = FeatureContext.getFeatureManager().getFeatureState(this);
if (featureState.isEnabled()) {
// if no strategy is selected, the decision is simple
String strategyId = featureState.getStrategyId();
if (strategyId == null || strategyId.isEmpty()) {
return true;
}
// check the selected strategy
for (ActivationStrategy strategy : FeatureContext.getFeatureManager().getActivationStrategies()) {
if (strategy.getId().equalsIgnoreCase(strategyId)) {
return strategy.isActive(featureState, simpleFeatureUser);
}
}
}
} catch (Exception e) {
LOGGER.error("error while checking toggle for user", e);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment