private static void bindApacheThriftClientConfig(ConfigurationBinding<?> binding, ConfigBinder configBinder) { if (binding.getConfigClass().equals(DriftClientConfig.class)) { configBinder.bindConfig(ApacheThriftClientConfig.class, binding.getKey().getAnnotation(), binding.getPrefix().orElse(null)); } }
public <T> void bindConfig(Key<T> key, Class<T> configClass, String prefix) { binder.bind(new ConfigurationBinding<>(key, configClass, Optional.ofNullable(prefix))); }
private static <T> void bindConfigurationProvider(Binder binder, ConfigurationProvider<T> configurationProvider) { binder.bind(configurationProvider.getConfigurationBinding().getKey()).toProvider(configurationProvider); }
private static <T> void bindConfigurationProvider(Binder binder, ConfigurationProvider<T> configurationProvider) { binder.bind(configurationProvider.getConfigurationBinding().getKey()).toProvider(configurationProvider); }
private static void bindApacheThriftClientConfig(ConfigurationBinding<?> binding, ConfigBinder configBinder) { if (binding.getConfigClass().equals(DriftClientConfig.class)) { configBinder.bindConfig(ApacheThriftClientConfig.class, binding.getKey().getAnnotation(), binding.getPrefix().orElse(null)); } }
@Override public <T> void bind(ConfigurationBinding<T> configurationBinding) { Key<T> key = configurationBinding.getKey(); binder.bind(key).toProvider(new ConfigurationProvider<>(configurationBinding)); createConfigDefaultsBinder(key); }
public <T> void bindConfig(Key<T> key, Class<T> configClass, String prefix) { binder.bind(new ConfigurationBinding<>(key, configClass, Optional.ofNullable(prefix))); }
@Override public void configure(Binder binder) { configBinder(binder).bindConfig(DriftNettyConnectionFactoryConfig.class); configBinder(binder).bindConfigurationBindingListener((binding, configBinder) -> { if (binding.getConfigClass().equals(DriftClientConfig.class)) { configBinder.bindConfig(DriftNettyClientConfig.class, binding.getKey().getAnnotation(), binding.getPrefix().orElse(null)); } }); binder.bind(ByteBufAllocator.class).toInstance(allocator); binder.bind(new TypeLiteral<MethodInvokerFactory<Annotation>>() {}) .toProvider(MethodInvokerFactoryProvider.class) .in(Scopes.SINGLETON); }
@Override public <T> void bind(ConfigurationBinding<T> configurationBinding) { Key<T> key = configurationBinding.getKey(); binder.bind(key).toProvider(new ConfigurationProvider<>(configurationBinding)); createConfigDefaultsBinder(key); }
@Test public void testConfigurationBindingListener() { List<ConfigurationBinding<?>> seenBindings = new ArrayList<>(); Module module = binder -> { ConfigBinder configBinder = configBinder(binder); configBinder.bindConfig(AnotherConfig.class); configBinder.bindConfigurationBindingListener((configurationBinding, callbackConfigBinder) -> { seenBindings.add(configurationBinding); callbackConfigBinder.bindConfig(Config1.class); callbackConfigBinder.bindConfig(Config1.class, MyAnnotation.class); }); }; Injector injector = createInjector(properties, module); verifyConfig(injector.getInstance(Config1.class)); verifyConfig(injector.getInstance(Key.get(Config1.class, MyAnnotation.class))); assertEquals(seenBindings.size(), 3); assertEquals( ImmutableSet.copyOf(seenBindings), ImmutableSet.of( new ConfigurationBinding<>(Key.get(Config1.class), Config1.class, Optional.empty()), new ConfigurationBinding<>(Key.get(Config1.class, MyAnnotation.class), Config1.class, Optional.empty()), new ConfigurationBinding<>(Key.get(AnotherConfig.class), AnotherConfig.class, Optional.empty()))); }
@Override public void configure(Binder binder) { configBinder(binder).bindConfig(DriftNettyConnectionFactoryConfig.class); configBinder(binder).bindConfigurationBindingListener((binding, configBinder) -> { if (binding.getConfigClass().equals(DriftClientConfig.class)) { configBinder.bindConfig(DriftNettyClientConfig.class, binding.getKey().getAnnotation(), binding.getPrefix().orElse(null)); } }); binder.bind(ByteBufAllocator.class).toInstance(allocator); binder.bind(new TypeLiteral<MethodInvokerFactory<Annotation>>() {}) .toProvider(MethodInvokerFactoryProvider.class) .in(Scopes.SINGLETON); }
@Test public void testConfigurationBindingListener() { List<ConfigurationBinding<?>> seenBindings = new ArrayList<>(); Module module = binder -> { ConfigBinder configBinder = configBinder(binder); configBinder.bindConfig(AnotherConfig.class); configBinder.bindConfigurationBindingListener((configurationBinding, callbackConfigBinder) -> { seenBindings.add(configurationBinding); callbackConfigBinder.bindConfig(Config1.class); callbackConfigBinder.bindConfig(Config1.class, MyAnnotation.class); }); }; Injector injector = createInjector(properties, module); verifyConfig(injector.getInstance(Config1.class)); verifyConfig(injector.getInstance(Key.get(Config1.class, MyAnnotation.class))); assertEquals(seenBindings.size(), 3); assertEquals( ImmutableSet.copyOf(seenBindings), ImmutableSet.of( new ConfigurationBinding<>(Key.get(Config1.class), Config1.class, Optional.empty()), new ConfigurationBinding<>(Key.get(Config1.class, MyAnnotation.class), Config1.class, Optional.empty()), new ConfigurationBinding<>(Key.get(AnotherConfig.class), AnotherConfig.class, Optional.empty()))); }
key = configurationBinding.getKey(); configClass = configurationBinding.getConfigClass(); prefix = configurationBinding.getPrefix().orElse(null); ConfigurationMetadata<T> metadata = getConfigurationMetadata(configurationBinding.getConfigClass()); T defaults = configurationFactory.getDefaultConfig(configurationBinding.getKey()); String prefix = configurationBinding.getPrefix() .map(value -> value + ".") .orElse("");
key = configurationBinding.getKey(); configClass = configurationBinding.getConfigClass(); prefix = configurationBinding.getPrefix().orElse(null); ConfigurationMetadata<T> metadata = getConfigurationMetadata(configurationBinding.getConfigClass()); T defaults = configurationFactory.getDefaultConfig(configurationBinding.getKey()); String prefix = configurationBinding.getPrefix() .map(value -> value + ".") .orElse("");
/** * This is used by the configuration provider */ <T> T build(ConfigurationProvider<T> configurationProvider) { requireNonNull(configurationProvider, "configurationProvider"); registerConfigurationProvider(configurationProvider, Optional.empty()); // check for a prebuilt instance T instance = getCachedInstance(configurationProvider); if (instance != null) { return instance; } ConfigurationBinding<T> configurationBinding = configurationProvider.getConfigurationBinding(); ConfigurationHolder<T> holder = build(configurationBinding.getConfigClass(), configurationBinding.getPrefix(), getConfigDefaults(configurationBinding.getKey())); instance = holder.getInstance(); // inform caller about warnings if (warningsMonitor != null) { for (Message message : holder.getProblems().getWarnings()) { warningsMonitor.onWarning(message.toString()); } } // add to instance cache T existingValue = putCachedInstance(configurationProvider, instance); // if key was already associated with a value, there was a // creation race and we lost. Just use the winners' instance; if (existingValue != null) { return existingValue; } return instance; }
/** * This is used by the configuration provider */ <T> T build(ConfigurationProvider<T> configurationProvider) { requireNonNull(configurationProvider, "configurationProvider"); registerConfigurationProvider(configurationProvider, Optional.empty()); // check for a prebuilt instance T instance = getCachedInstance(configurationProvider); if (instance != null) { return instance; } ConfigurationBinding<T> configurationBinding = configurationProvider.getConfigurationBinding(); ConfigurationHolder<T> holder = build(configurationBinding.getConfigClass(), configurationBinding.getPrefix(), getConfigDefaults(configurationBinding.getKey())); instance = holder.getInstance(); // inform caller about warnings if (warningsMonitor != null) { for (Message message : holder.getProblems().getWarnings()) { warningsMonitor.onWarning(message.toString()); } } // add to instance cache T existingValue = putCachedInstance(configurationProvider, instance); // if key was already associated with a value, there was a // creation race and we lost. Just use the winners' instance; if (existingValue != null) { return existingValue; } return instance; }