Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.experiments; 16 : 17 : import com.google.common.collect.ImmutableSet; 18 : import com.google.gerrit.server.config.GerritServerConfig; 19 : import com.google.inject.AbstractModule; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Singleton; 22 : import java.util.Arrays; 23 : import java.util.HashSet; 24 : import java.util.Set; 25 : import org.eclipse.jgit.lib.Config; 26 : 27 : /** 28 : * An implementation of {@link ExperimentFeatures} that uses gerrit.config to evaluate the status of 29 : * the feature. 30 : */ 31 : @Singleton 32 : public class ConfigExperimentFeatures implements ExperimentFeatures { 33 : 34 152 : public static class ConfigExperimentFeaturesModule extends AbstractModule { 35 : @Override 36 : protected void configure() { 37 152 : bind(ExperimentFeatures.class).to(ConfigExperimentFeatures.class); 38 152 : } 39 : } 40 : 41 : private ImmutableSet<String> enabledExperimentFeatures; 42 : 43 : @Inject 44 150 : public ConfigExperimentFeatures(@GerritServerConfig Config gerritServerConfig) { 45 150 : Set<String> enabledExperiments = new HashSet<>(); 46 150 : Arrays.stream(gerritServerConfig.getStringList("experiments", null, "enabled")) 47 150 : .forEach(enabledExperiments::add); 48 150 : ExperimentFeaturesConstants.DEFAULT_ENABLED_FEATURES.forEach(enabledExperiments::add); 49 150 : Arrays.stream(gerritServerConfig.getStringList("experiments", null, "disabled")) 50 150 : .forEach(enabledExperiments::remove); 51 150 : enabledExperimentFeatures = ImmutableSet.copyOf(enabledExperiments); 52 150 : } 53 : 54 : @Override 55 : public boolean isFeatureEnabled(String featureFlag) { 56 3 : return getEnabledExperimentFeatures().contains(featureFlag); 57 : } 58 : 59 : @Override 60 : public ImmutableSet<String> getEnabledExperimentFeatures() { 61 4 : return enabledExperimentFeatures; 62 : } 63 : }