Search in sources :

Example 6 with ConfigurationModule

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"));
}
Also used : TestingDiscoveryModule(io.airlift.discovery.client.testing.TestingDiscoveryModule) Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) InMemoryDiscoveryClient(io.airlift.discovery.client.testing.InMemoryDiscoveryClient) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Example 7 with ConfigurationModule

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));
}
Also used : Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) JsonModule(io.airlift.json.JsonModule) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Example 8 with ConfigurationModule

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());
}
Also used : Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) JsonModule(io.airlift.json.JsonModule) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Example 9 with ConfigurationModule

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;
}
Also used : Module(com.google.inject.Module) Logger(io.airlift.log.Logger) ConfigRecord(io.airlift.configuration.ConfigurationInspector.ConfigRecord) HashMap(java.util.HashMap) ConfigurationLoader.getSystemProperties(io.airlift.configuration.ConfigurationLoader.getSystemProperties) ArrayList(java.util.ArrayList) ConfigurationLoader.loadPropertiesFrom(io.airlift.configuration.ConfigurationLoader.loadPropertiesFrom) ImmutableList(com.google.common.collect.ImmutableList) Binder(com.google.inject.Binder) Message(com.google.inject.spi.Message) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) Map(java.util.Map) Logging(io.airlift.log.Logging) ConfigurationUtils.replaceEnvironmentVariables(io.airlift.configuration.ConfigurationUtils.replaceEnvironmentVariables) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ConfigurationModule(io.airlift.configuration.ConfigurationModule) WarningsMonitor(io.airlift.configuration.WarningsMonitor) IOException(java.io.IOException) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Injector(com.google.inject.Injector) Stage(com.google.inject.Stage) UncheckedIOException(java.io.UncheckedIOException) LoggingConfiguration(io.airlift.log.LoggingConfiguration) List(java.util.List) ConfigAttribute(io.airlift.configuration.ConfigurationInspector.ConfigAttribute) ConfigurationInspector(io.airlift.configuration.ConfigurationInspector) TreeMap(java.util.TreeMap) Guice(com.google.inject.Guice) Builder(com.google.common.collect.ImmutableList.Builder) Collections(java.util.Collections) Message(com.google.inject.spi.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Binder(com.google.inject.Binder) Injector(com.google.inject.Injector) LoggingConfiguration(io.airlift.log.LoggingConfiguration) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) Logging(io.airlift.log.Logging) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TreeMap(java.util.TreeMap) Module(com.google.inject.Module) ConfigurationModule(io.airlift.configuration.ConfigurationModule)

Example 10 with ConfigurationModule

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"));
}
Also used : TestingDiscoveryModule(io.airlift.discovery.client.testing.TestingDiscoveryModule) Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) InMemoryDiscoveryClient(io.airlift.discovery.client.testing.InMemoryDiscoveryClient) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Aggregations

Injector (com.google.inject.Injector)12 ConfigurationFactory (io.airlift.configuration.ConfigurationFactory)12 ConfigurationModule (io.airlift.configuration.ConfigurationModule)12 Test (org.testng.annotations.Test)11 TestingNodeModule (io.airlift.node.testing.TestingNodeModule)8 InMemoryDiscoveryClient (io.airlift.discovery.client.testing.InMemoryDiscoveryClient)6 TestingDiscoveryModule (io.airlift.discovery.client.testing.TestingDiscoveryModule)6 ImmutableList (com.google.common.collect.ImmutableList)2 Guice (com.google.inject.Guice)2 Module (com.google.inject.Module)2 JsonModule (io.airlift.json.JsonModule)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Builder (com.google.common.collect.ImmutableList.Builder)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 Binder (com.google.inject.Binder)1 BindingAnnotation (com.google.inject.BindingAnnotation)1