|
| 1 | +package com.launchdarkly.client; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.net.URI; |
| 7 | + |
| 8 | +/** |
| 9 | + * Provides factories for the standard implementations of LaunchDarkly component interfaces. |
| 10 | + * @since 4.0.0 |
| 11 | + */ |
| 12 | +public abstract class Components { |
| 13 | + private static final FeatureStoreFactory inMemoryFeatureStoreFactory = new InMemoryFeatureStoreFactory(); |
| 14 | + private static final EventProcessorFactory defaultEventProcessorFactory = new DefaultEventProcessorFactory(); |
| 15 | + private static final EventProcessorFactory nullEventProcessorFactory = new NullEventProcessorFactory(); |
| 16 | + private static final UpdateProcessorFactory defaultUpdateProcessorFactory = new DefaultUpdateProcessorFactory(); |
| 17 | + private static final UpdateProcessorFactory nullUpdateProcessorFactory = new NullUpdateProcessorFactory(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Returns a factory for the default in-memory implementation of {@link FeatureStore}. |
| 21 | + */ |
| 22 | + public static FeatureStoreFactory inMemoryFeatureStore() { |
| 23 | + return inMemoryFeatureStoreFactory; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns a factory with builder methods for creating a Redis-backed implementation of {@link FeatureStore}, |
| 28 | + * using {@link RedisFeatureStoreBuilder#DEFAULT_URI}. |
| 29 | + */ |
| 30 | + public static RedisFeatureStoreBuilder redisFeatureStore() { |
| 31 | + return new RedisFeatureStoreBuilder(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns a factory with builder methods for creating a Redis-backed implementation of {@link FeatureStore}, |
| 36 | + * specifying the Redis URI. |
| 37 | + */ |
| 38 | + public static RedisFeatureStoreBuilder redisFeatureStore(URI redisUri) { |
| 39 | + return new RedisFeatureStoreBuilder(redisUri); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns a factory for the default implementation of {@link EventProcessor}, which |
| 44 | + * forwards all analytics events to LaunchDarkly (unless the client is offline or you have |
| 45 | + * set {@link LDConfig.Builder#sendEvents(boolean)} to {@code false}). |
| 46 | + */ |
| 47 | + public static EventProcessorFactory defaultEventProcessor() { |
| 48 | + return defaultEventProcessorFactory; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns a factory for a null implementation of {@link EventProcessor}, which will discard |
| 53 | + * all analytics events and not send them to LaunchDarkly, regardless of any other configuration. |
| 54 | + */ |
| 55 | + public static EventProcessorFactory nullEventProcessor() { |
| 56 | + return nullEventProcessorFactory; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns a factory for the default implementation of {@link UpdateProcessor}, which receives |
| 61 | + * feature flag data from LaunchDarkly using either streaming or polling as configured (or does |
| 62 | + * nothing if the client is offline, or in LDD mode). |
| 63 | + */ |
| 64 | + public static UpdateProcessorFactory defaultUpdateProcessor() { |
| 65 | + return defaultUpdateProcessorFactory; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a factory for a null implementation of {@link UpdateProcessor}, which does not |
| 70 | + * connect to LaunchDarkly, regardless of any other configuration. |
| 71 | + */ |
| 72 | + public static UpdateProcessorFactory nullUpdateProcessor() { |
| 73 | + return nullUpdateProcessorFactory; |
| 74 | + } |
| 75 | + |
| 76 | + private static final class InMemoryFeatureStoreFactory implements FeatureStoreFactory { |
| 77 | + @Override |
| 78 | + public FeatureStore createFeatureStore() { |
| 79 | + return new InMemoryFeatureStore(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static final class DefaultEventProcessorFactory implements EventProcessorFactory { |
| 84 | + @Override |
| 85 | + public EventProcessor createEventProcessor(String sdkKey, LDConfig config) { |
| 86 | + if (config.offline || !config.sendEvents) { |
| 87 | + return new EventProcessor.NullEventProcessor(); |
| 88 | + } else { |
| 89 | + return new DefaultEventProcessor(sdkKey, config); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static final class NullEventProcessorFactory implements EventProcessorFactory { |
| 95 | + public EventProcessor createEventProcessor(String sdkKey, LDConfig config) { |
| 96 | + return new EventProcessor.NullEventProcessor(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static final class DefaultUpdateProcessorFactory implements UpdateProcessorFactory { |
| 101 | + // Note, logger uses LDClient class name for backward compatibility |
| 102 | + private static final Logger logger = LoggerFactory.getLogger(LDClient.class); |
| 103 | + |
| 104 | + @Override |
| 105 | + public UpdateProcessor createUpdateProcessor(String sdkKey, LDConfig config, FeatureStore featureStore) { |
| 106 | + if (config.offline) { |
| 107 | + logger.info("Starting LaunchDarkly client in offline mode"); |
| 108 | + return new UpdateProcessor.NullUpdateProcessor(); |
| 109 | + } else if (config.useLdd) { |
| 110 | + logger.info("Starting LaunchDarkly in LDD mode. Skipping direct feature retrieval."); |
| 111 | + return new UpdateProcessor.NullUpdateProcessor(); |
| 112 | + } else { |
| 113 | + FeatureRequestor requestor = new FeatureRequestor(sdkKey, config); |
| 114 | + if (config.stream) { |
| 115 | + logger.info("Enabling streaming API"); |
| 116 | + return new StreamProcessor(sdkKey, config, requestor, featureStore); |
| 117 | + } else { |
| 118 | + logger.info("Disabling streaming API"); |
| 119 | + logger.warn("You should only disable the streaming API if instructed to do so by LaunchDarkly support"); |
| 120 | + return new PollingProcessor(config, requestor, featureStore); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static final class NullUpdateProcessorFactory implements UpdateProcessorFactory { |
| 127 | + @Override |
| 128 | + public UpdateProcessor createUpdateProcessor(String sdkKey, LDConfig config, FeatureStore featureStore) { |
| 129 | + return new UpdateProcessor.NullUpdateProcessor(); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments