com.mageddo.togglefirst.DefaultFeatureManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toggle-first-core Show documentation
Show all versions of toggle-first-core Show documentation
Enable and disable your features in production on a button click
package com.mageddo.togglefirst;
import com.fasterxml.jackson.core.type.TypeReference;
import com.mageddo.common.jackson.JsonUtils;
import com.mageddo.togglefirst.activation.ActivationStrategy;
import com.mageddo.togglefirst.repository.FeatureRepository;
import java.util.*;
import java.util.stream.Collectors;
public class DefaultFeatureManager implements FeatureManager {
private FeatureRepository featureRepository;
private FeatureMetadataProvider featureMetadataProvider;
private Set activationStrategies;
public DefaultFeatureManager() {
this.activationStrategies = Collections.emptySet();
}
@Override
public Set activationStrategies() {
return activationStrategies;
}
@Override
public FeatureRepository repository() {
return featureRepository;
}
@Override
public FeatureMetadataProvider metadataProvider() {
return featureMetadataProvider;
}
@Override
public void activate(Feature feature) {
final FeatureMetadata metadata = metadataOrDefault(feature, null)
.set(FeatureKeys.STATUS, String.valueOf(Status.ACTIVE.getCode()))
;
repository().updateMetadata(metadata, null);
}
@Override
public void activate(Feature feature, String value) {
final FeatureMetadata metadata = metadataOrDefault(feature, null)
.set(FeatureKeys.STATUS, String.valueOf(Status.ACTIVE.getCode()))
.set(FeatureKeys.VALUE, value)
;
repository().updateMetadata(metadata, null);
}
@Override
public void userActivate(Feature feature, String user) {
{
final FeatureMetadata metadata = metadataOrDefault(feature, user)
.set(FeatureKeys.STATUS, String.valueOf(Status.RESTRICTED.getCode()))
;
updateMetadata(feature, metadata.parameters());
}
{
FeatureMetadata metadata = repository().getMetadata(feature, user);
if (metadata == null) {
metadata = new DefaultFeatureMetadata(feature);
}
metadata
.set(FeatureKeys.STATUS, String.valueOf(Status.ACTIVE.getCode()))
;
repository().updateMetadata(metadata, user);
}
}
@Override
public void userActivate(Feature feature, String user, String value) {
{
final FeatureMetadata metadata = metadataOrDefault(feature, null)
.set(FeatureKeys.STATUS, String.valueOf(Status.RESTRICTED.getCode()))
;
repository().updateMetadata(metadata, null);
}
{
FeatureMetadata metadata = repository().getMetadata(feature, user);
if (metadata == null) {
metadata = new DefaultFeatureMetadata(feature);
}
metadata
.set(FeatureKeys.STATUS, String.valueOf(Status.ACTIVE.getCode()))
.set(FeatureKeys.VALUE, value);
repository().updateMetadata(metadata, user);
}
}
@Override
public void deactivate(Feature feature) {
userDeactivate(feature, null);
}
@Override
public void userDeactivate(Feature feature, String user) {
FeatureMetadata metadata = repository().getMetadata(feature, user);
if (metadata == null) {
metadata = new DefaultFeatureMetadata(feature);
}
metadata
.set(FeatureKeys.STATUS, String.valueOf(Status.INACTIVE.getCode()))
;
repository().updateMetadata(metadata, user);
}
@Override
public void updateMetadata(Feature feature, Map parameters) {
updateMetadata(feature, null, parameters);
}
@Override
public void updateMetadata(Feature feature, String user, Map parameters) {
final FeatureMetadata metadata = metadata(feature, user);
for (final String k : parameters.keySet()) {
metadata.set(k, parameters.get(k));
}
repository().updateMetadata(metadata, user);
}
@Override
public FeatureMetadata metadata(Feature feature) {
final FeatureMetadata metadata = repository().getMetadata(feature, null);
if(metadata != null){
return metadata;
}
final FeatureMetadataProvider provider = metadataProvider();
if(provider != null){
return provider.getMetadata(feature);
}
return new DefaultFeatureMetadata(feature);
}
@Override
public FeatureMetadata metadata(Feature feature, String user) {
if(user == null){
return metadata(feature);
}
final FeatureMetadata metadata = metadata(feature);
switch (metadata.status()){
case ACTIVE:
case INACTIVE:
return metadata;
case RESTRICTED:
return Optional
.ofNullable(repository().getMetadata(feature, user))
.orElse(
new DefaultFeatureMetadata(feature)
.set(FeatureKeys.STATUS, String.valueOf(Status.INACTIVE.getCode()))
);
}
return metadata;
}
FeatureMetadata metadataOrDefault(Feature feature, String user) {
return repository().getMetadataOrDefault(feature, user, new DefaultFeatureMetadata(feature));
}
@Override
public boolean isActive(Feature feature) {
return isActive(feature, null);
}
@Override
public boolean isActive(Feature feature, String user) {
final FeatureMetadata featureMetadata = metadata(feature);
final FeatureMetadata userFeatureMetadata = metadata(feature, user);
final Collection strategies = getFeatureActivationStrategies(featureMetadata);
if(strategies.isEmpty() || userFeatureMetadata.isActive()){
return userFeatureMetadata.isActive();
}
for (final ActivationStrategy activationStrategy : strategies) {
if(!activationStrategy.isActive(featureMetadata, user)){
return false;
} else {
activationStrategy.postHandleActive(this, userFeatureMetadata);
}
}
return true;
}
private Collection getFeatureActivationStrategies(FeatureMetadata metadata) {
final Set strategiesIds = JsonUtils.readValue(
metadata.get(FeatureKeys.ACTIVATION_STRATEGIES, "[]"),
new TypeReference>() {}
);
return activationStrategies()
.stream()
.filter(it -> strategiesIds.contains(String.valueOf(it.id())))
.collect(Collectors.toCollection(LinkedHashSet::new))
;
}
@Override
public String value(Feature feature) {
return value(feature , null);
}
@Override
public String value(Feature feature, String user) {
final FeatureMetadata metadata = metadata(feature, user);
return metadata == null ? null : metadata.get(FeatureKeys.VALUE);
}
public DefaultFeatureManager featureRepository(FeatureRepository featureRepository) {
this.featureRepository = featureRepository;
return this;
}
public DefaultFeatureManager featureMetadataProvider(FeatureMetadataProvider featureMetadataProvider) {
this.featureMetadataProvider = featureMetadataProvider;
return this;
}
public DefaultFeatureManager activationStrategies(Set activationStrategies) {
this.activationStrategies = activationStrategies;
return this;
}
}