use of io.airlift.configuration.ConfigurationModule in project airlift by airlift.
the class TestHttpServiceSelectorBinder method testHttpsSelector.
@Test
public void testHttpsSelector() {
Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())), new TestingNodeModule(), new TestingDiscoveryModule(), binder -> discoveryBinder(binder).bindHttpSelector("apple"));
InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("https", "fake://server-https").build()));
HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
assertEquals(selector.selectHttpService().stream().collect(onlyElement()), URI.create("fake://server-https"));
}
use of io.airlift.configuration.ConfigurationModule in project airlift by airlift.
the class AbstractTestDiscoveryModule method testBinding.
@Test
public void testBinding() throws Exception {
Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(ImmutableMap.of("discovery.uri", "fake://server"))), new JsonModule(), new TestingNodeModule(), discoveryModule);
// should produce a discovery announcement client and a lookup client
assertNotNull(injector.getInstance(DiscoveryAnnouncementClient.class));
assertNotNull(injector.getInstance(DiscoveryLookupClient.class));
// should produce an Announcer
assertNotNull(injector.getInstance(Announcer.class));
// should produce a ServiceSelectorManager
assertNotNull(injector.getInstance(ServiceSelectorManager.class));
}
use of io.airlift.configuration.ConfigurationModule in project airlift by airlift.
the class AbstractTestDiscoveryModule method testMerging.
@Test
public void testMerging() throws Exception {
final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl(URI.create("http://127.0.0.1:4444"), URI.create("http://example.com:4444"), null, null);
Map<String, String> config = ImmutableMap.<String, String>builder().put("discovery.uri", "fake://server").put("discovery.carrot.pool", "test").build();
Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(config)), new JsonModule(), new TestingNodeModule(), new DiscoveryModule(), binder -> {
binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo);
discoveryBinder(binder).bindHttpAnnouncement("apple");
discoveryBinder(binder).bindHttpAnnouncement("banana");
discoveryBinder(binder).bindHttpAnnouncement("carrot");
discoveryBinder(binder).bindHttpSelector("apple");
discoveryBinder(binder).bindHttpSelector("banana");
discoveryBinder(binder).bindHttpSelector("carrot");
discoveryBinder(binder).bindHttpSelector("grape");
});
HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
assertEquals(selector.selectHttpService().stream().collect(onlyElement()), URI.create("http://127.0.0.1:4444"));
selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("banana")));
assertEquals(selector.selectHttpService().stream().collect(onlyElement()), URI.create("http://127.0.0.1:4444"));
selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("carrot")));
assertTrue(selector.selectHttpService().isEmpty());
selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("grape")));
assertTrue(selector.selectHttpService().isEmpty());
}
use of io.airlift.configuration.ConfigurationModule in project airlift by airlift.
the class Bootstrap method initialize.
public Injector initialize() {
checkState(!initialized, "Already initialized");
initialized = true;
Logging logging = null;
if (initializeLogging) {
logging = Logging.initialize();
}
Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> log.error(throwable, "Uncaught exception in thread %s", thread.getName()));
Map<String, String> requiredProperties;
if (requiredConfigurationProperties == null) {
// initialize configuration
log.info("Loading configuration");
requiredProperties = Collections.emptyMap();
String configFile = System.getProperty("config");
if (configFile != null) {
try {
requiredProperties = loadPropertiesFrom(configFile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
} else {
requiredProperties = requiredConfigurationProperties;
}
Map<String, String> unusedProperties = new TreeMap<>(requiredProperties);
// combine property sources
Map<String, String> properties = new HashMap<>();
if (optionalConfigurationProperties != null) {
properties.putAll(optionalConfigurationProperties);
}
properties.putAll(requiredProperties);
properties.putAll(getSystemProperties());
// replace environment variables in property values
List<Message> errors = new ArrayList<>();
properties = replaceEnvironmentVariables(properties, System.getenv(), (key, error) -> {
unusedProperties.remove(key);
errors.add(new Message(error));
});
// create configuration factory
properties = ImmutableSortedMap.copyOf(properties);
List<Message> warnings = new ArrayList<>();
ConfigurationFactory configurationFactory = new ConfigurationFactory(properties, warning -> warnings.add(new Message(warning)));
Boolean quietConfig = configurationFactory.build(BootstrapConfig.class).getQuiet();
// initialize logging
if (logging != null) {
log.info("Initializing logging");
LoggingConfiguration configuration = configurationFactory.build(LoggingConfiguration.class);
logging.configure(configuration);
}
// Register configuration classes defined in the modules
configurationFactory.registerConfigurationClasses(modules);
// Validate configuration classes
errors.addAll(configurationFactory.validateRegisteredConfigurationProvider());
// at this point all config file properties should be used
// so we can calculate the unused properties
unusedProperties.keySet().removeAll(configurationFactory.getUsedProperties());
for (String key : unusedProperties.keySet()) {
Message message = new Message(format("Configuration property '%s' was not used", key));
(strictConfig ? errors : warnings).add(message);
}
// If there are configuration errors, fail-fast to keep output clean
if (!errors.isEmpty()) {
throw new ApplicationConfigurationException(errors, warnings);
}
// Log effective configuration
if (!((quietConfig == null) ? quiet : quietConfig)) {
logConfiguration(configurationFactory);
}
// Log any warnings
if (!warnings.isEmpty()) {
StringBuilder message = new StringBuilder();
message.append("Configuration warnings\n");
message.append("==========\n\n");
message.append("Configuration should be updated:\n\n");
for (int index = 0; index < warnings.size(); index++) {
message.append(format("%s) %s\n", index + 1, warnings.get(index)));
}
message.append("\n");
message.append("==========");
log.warn(message.toString());
}
// system modules
Builder<Module> moduleList = ImmutableList.builder();
moduleList.add(new LifeCycleModule());
moduleList.add(new ConfigurationModule(configurationFactory));
moduleList.add(binder -> binder.bind(WarningsMonitor.class).toInstance(log::warn));
// disable broken Guice "features"
moduleList.add(Binder::disableCircularProxies);
moduleList.add(Binder::requireExplicitBindings);
moduleList.add(Binder::requireExactBindingAnnotations);
moduleList.addAll(modules);
// create the injector
Injector injector = Guice.createInjector(Stage.PRODUCTION, moduleList.build());
// Create the life-cycle manager
LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
// Start services
lifeCycleManager.start();
return injector;
}
use of io.airlift.configuration.ConfigurationModule in project airlift by airlift.
the class TestHttpServiceSelectorBinder method testHttpSelectorString.
@Test
public void testHttpSelectorString() {
Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())), new TestingNodeModule(), new TestingDiscoveryModule(), binder -> discoveryBinder(binder).bindHttpSelector("apple"));
InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", "fake://server-http").build()));
HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
assertEquals(selector.selectHttpService().stream().collect(onlyElement()), URI.create("fake://server-http"));
}
Aggregations