Search in sources :

Example 1 with SubstitutingSourceProvider

use of io.dropwizard.configuration.SubstitutingSourceProvider in project dropwizard by dropwizard.

the class HelloWorldApplication method initialize.

@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
    // Enable variable substitution with environment variables
    bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));
    bootstrap.addCommand(new RenderCommand());
    bootstrap.addBundle(new AssetsBundle());
    bootstrap.addBundle(new MigrationsBundle<HelloWorldConfiguration>() {

        @Override
        public DataSourceFactory getDataSourceFactory(HelloWorldConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }
    });
    bootstrap.addBundle(hibernateBundle);
    bootstrap.addBundle(new ViewBundle<HelloWorldConfiguration>() {

        @Override
        public Map<String, Map<String, String>> getViewConfiguration(HelloWorldConfiguration configuration) {
            return configuration.getViewRendererConfiguration();
        }
    });
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) RenderCommand(com.example.helloworld.cli.RenderCommand) DataSourceFactory(io.dropwizard.db.DataSourceFactory) EnvironmentVariableSubstitutor(io.dropwizard.configuration.EnvironmentVariableSubstitutor) AssetsBundle(io.dropwizard.assets.AssetsBundle) Map(java.util.Map)

Example 2 with SubstitutingSourceProvider

use of io.dropwizard.configuration.SubstitutingSourceProvider in project dropwizard by dropwizard.

the class DefaultLoggingFactoryTest method testConfigure.

@Test
public void testConfigure() throws Exception {
    final File newAppLog = folder.newFile("example-new-app.log");
    final File newAppNotAdditiveLog = folder.newFile("example-new-app-not-additive.log");
    final File defaultLog = folder.newFile("example.log");
    final StrSubstitutor substitutor = new StrSubstitutor(ImmutableMap.of("new_app", StringUtils.removeEnd(newAppLog.getAbsolutePath(), ".log"), "new_app_not_additive", StringUtils.removeEnd(newAppNotAdditiveLog.getAbsolutePath(), ".log"), "default", StringUtils.removeEnd(defaultLog.getAbsolutePath(), ".log")));
    final String configPath = Resources.getResource("yaml/logging_advanced.yml").getFile();
    final DefaultLoggingFactory config = factory.build(new SubstitutingSourceProvider(new FileConfigurationSourceProvider(), substitutor), configPath);
    config.configure(new MetricRegistry(), "test-logger");
    LoggerFactory.getLogger("com.example.app").debug("Application debug log");
    LoggerFactory.getLogger("com.example.app").info("Application log");
    LoggerFactory.getLogger("com.example.newApp").debug("New application debug log");
    LoggerFactory.getLogger("com.example.newApp").info("New application info log");
    LoggerFactory.getLogger("com.example.legacyApp").debug("Legacy application debug log");
    LoggerFactory.getLogger("com.example.legacyApp").info("Legacy application info log");
    LoggerFactory.getLogger("com.example.notAdditive").debug("Not additive application debug log");
    LoggerFactory.getLogger("com.example.notAdditive").info("Not additive application info log");
    config.stop();
    assertThat(Files.readLines(defaultLog, StandardCharsets.UTF_8)).containsOnly("INFO  com.example.app: Application log", "DEBUG com.example.newApp: New application debug log", "INFO  com.example.newApp: New application info log", "DEBUG com.example.legacyApp: Legacy application debug log", "INFO  com.example.legacyApp: Legacy application info log");
    assertThat(Files.readLines(newAppLog, StandardCharsets.UTF_8)).containsOnly("DEBUG com.example.newApp: New application debug log", "INFO  com.example.newApp: New application info log");
    assertThat(Files.readLines(newAppNotAdditiveLog, StandardCharsets.UTF_8)).containsOnly("DEBUG com.example.notAdditive: Not additive application debug log", "INFO  com.example.notAdditive: Not additive application info log");
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) MetricRegistry(com.codahale.metrics.MetricRegistry) File(java.io.File) FileConfigurationSourceProvider(io.dropwizard.configuration.FileConfigurationSourceProvider) Test(org.junit.Test)

Aggregations

SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 RenderCommand (com.example.helloworld.cli.RenderCommand)1 AssetsBundle (io.dropwizard.assets.AssetsBundle)1 EnvironmentVariableSubstitutor (io.dropwizard.configuration.EnvironmentVariableSubstitutor)1 FileConfigurationSourceProvider (io.dropwizard.configuration.FileConfigurationSourceProvider)1 DataSourceFactory (io.dropwizard.db.DataSourceFactory)1 File (java.io.File)1 Map (java.util.Map)1 StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)1 Test (org.junit.Test)1