|
| 1 | +package dev.openfeature.contrib.providers.multiprovider; |
| 2 | + |
| 3 | +import dev.openfeature.sdk.EvaluationContext; |
| 4 | +import dev.openfeature.sdk.EventProvider; |
| 5 | +import dev.openfeature.sdk.FeatureProvider; |
| 6 | +import dev.openfeature.sdk.Metadata; |
| 7 | +import dev.openfeature.sdk.ProviderEvaluation; |
| 8 | +import dev.openfeature.sdk.Value; |
| 9 | +import dev.openfeature.sdk.exceptions.GeneralError; |
| 10 | +import lombok.Getter; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.json.JSONObject; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.Callable; |
| 21 | +import java.util.concurrent.ExecutorService; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.Future; |
| 24 | + |
| 25 | +/** |
| 26 | + * Provider implementation for Multi-provider. |
| 27 | + */ |
| 28 | +@Slf4j |
| 29 | +public class MultiProvider extends EventProvider { |
| 30 | + |
| 31 | + @Getter |
| 32 | + private static final String NAME = "multiprovider"; |
| 33 | + public static final int INIT_THREADS_COUNT = 8; |
| 34 | + private final Map<String, FeatureProvider> providers; |
| 35 | + private final Strategy strategy; |
| 36 | + private String metadataName; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructs a MultiProvider with the given list of FeatureProviders, using a default strategy. |
| 40 | + * |
| 41 | + * @param providers the list of FeatureProviders to initialize the MultiProvider with |
| 42 | + */ |
| 43 | + public MultiProvider(List<FeatureProvider> providers) { |
| 44 | + this(providers, null); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Constructs a MultiProvider with the given list of FeatureProviders and a strategy. |
| 49 | + * |
| 50 | + * @param providers the list of FeatureProviders to initialize the MultiProvider with |
| 51 | + * @param strategy the strategy |
| 52 | + */ |
| 53 | + public MultiProvider(List<FeatureProvider> providers, Strategy strategy) { |
| 54 | + this.providers = buildProviders(providers); |
| 55 | + if (strategy != null) { |
| 56 | + this.strategy = strategy; |
| 57 | + } else { |
| 58 | + this.strategy = new FirstMatchStrategy(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected static Map<String, FeatureProvider> buildProviders(List<FeatureProvider> providers) { |
| 63 | + Map<String, FeatureProvider> providersMap = new LinkedHashMap<>(providers.size()); |
| 64 | + for (FeatureProvider provider: providers) { |
| 65 | + FeatureProvider prevProvider = providersMap.put(provider.getMetadata().getName(), provider); |
| 66 | + if (prevProvider != null) { |
| 67 | + log.warn("duplicated provider name: {}", provider.getMetadata().getName()); |
| 68 | + } |
| 69 | + } |
| 70 | + return Collections.unmodifiableMap(providersMap); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Initialize the provider. |
| 75 | + * @param evaluationContext evaluation context |
| 76 | + * @throws Exception on error |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public void initialize(EvaluationContext evaluationContext) throws Exception { |
| 80 | + JSONObject json = new JSONObject(); |
| 81 | + json.put("name", NAME); |
| 82 | + JSONObject providersMetadata = new JSONObject(); |
| 83 | + json.put("originalMetadata", providersMetadata); |
| 84 | + ExecutorService initPool = Executors.newFixedThreadPool(INIT_THREADS_COUNT); |
| 85 | + Collection<Callable<Boolean>> tasks = new ArrayList<>(providers.size()); |
| 86 | + for (FeatureProvider provider: providers.values()) { |
| 87 | + tasks.add(() -> { |
| 88 | + provider.initialize(evaluationContext); |
| 89 | + return true; |
| 90 | + }); |
| 91 | + JSONObject providerMetadata = new JSONObject(); |
| 92 | + providerMetadata.put("name", provider.getMetadata().getName()); |
| 93 | + providersMetadata.put(provider.getMetadata().getName(), providerMetadata); |
| 94 | + } |
| 95 | + List<Future<Boolean>> results = initPool.invokeAll(tasks); |
| 96 | + for (Future<Boolean> result: results) { |
| 97 | + if (!result.get()) { |
| 98 | + throw new GeneralError("init failed"); |
| 99 | + } |
| 100 | + } |
| 101 | + metadataName = json.toString(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Metadata getMetadata() { |
| 106 | + return () -> metadataName; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
| 111 | + return strategy.evaluate(providers, key, defaultValue, ctx, |
| 112 | + p -> p.getBooleanEvaluation(key, defaultValue, ctx)); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
| 117 | + return strategy.evaluate(providers, key, defaultValue, ctx, |
| 118 | + p -> p.getStringEvaluation(key, defaultValue, ctx)); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
| 123 | + return strategy.evaluate(providers, key, defaultValue, ctx, |
| 124 | + p -> p.getIntegerEvaluation(key, defaultValue, ctx)); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
| 129 | + return strategy.evaluate(providers, key, defaultValue, ctx, |
| 130 | + p -> p.getDoubleEvaluation(key, defaultValue, ctx)); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
| 135 | + return strategy.evaluate(providers, key, defaultValue, ctx, |
| 136 | + p -> p.getObjectEvaluation(key, defaultValue, ctx)); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void shutdown() { |
| 141 | + log.debug("shutdown begin"); |
| 142 | + for (FeatureProvider provider: providers.values()) { |
| 143 | + try { |
| 144 | + provider.shutdown(); |
| 145 | + } catch (Exception e) { |
| 146 | + log.error("error shutdown provider {}", provider.getMetadata().getName(), e); |
| 147 | + } |
| 148 | + } |
| 149 | + log.debug("shutdown end"); |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments