Search in sources :

Example 1 with Bootstrap

use of io.dropwizard.setup.Bootstrap in project dropwizard by dropwizard.

the class InheritedServerCommandTest method usesDefaultConfigPath.

@Test
void usesDefaultConfigPath() throws Exception {
    class SingletonConfigurationFactory implements ConfigurationFactory<Configuration> {

        @Override
        public Configuration build(final ConfigurationSourceProvider provider, final String path) {
            return configuration;
        }

        @Override
        public Configuration build() {
            throw new AssertionError("Didn't use the default config path variable");
        }
    }
    when(configuration.getLoggingFactory()).thenReturn(mock(LoggingFactory.class));
    final Bootstrap<Configuration> bootstrap = new Bootstrap<>(application);
    bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new SingletonConfigurationFactory());
    bootstrap.addCommand(new ConfiguredCommand<Configuration>("test", "a test command") {

        @Override
        protected void run(final Bootstrap<Configuration> bootstrap, final Namespace namespace, final Configuration configuration) {
            assertThat(namespace.getString("file")).isNotEmpty().isEqualTo("yaml/server.yml");
        }

        @Override
        protected Argument addFileArgument(final Subparser subparser) {
            return super.addFileArgument(subparser).setDefault("yaml/server.yml");
        }
    });
    final JarLocation location = mock(JarLocation.class);
    when(location.toString()).thenReturn("dw-thing.jar");
    when(location.getVersion()).thenReturn(Optional.of("1.0.0"));
    Cli cli = new Cli(location, bootstrap, System.out, System.err);
    cli.run("test");
}
Also used : LoggingFactory(io.dropwizard.logging.LoggingFactory) JarLocation(io.dropwizard.util.JarLocation) Configuration(io.dropwizard.Configuration) Argument(net.sourceforge.argparse4j.inf.Argument) Namespace(net.sourceforge.argparse4j.inf.Namespace) ConfigurationSourceProvider(io.dropwizard.configuration.ConfigurationSourceProvider) ConfigurationFactory(io.dropwizard.configuration.ConfigurationFactory) Subparser(net.sourceforge.argparse4j.inf.Subparser) Bootstrap(io.dropwizard.setup.Bootstrap) Test(org.junit.jupiter.api.Test)

Example 2 with Bootstrap

use of io.dropwizard.setup.Bootstrap in project dropwizard by dropwizard.

the class Application method run.

/**
 * Parses command-line arguments and runs the application. Call this method from a {@code public
 * static void main} entry point in your application.
 *
 * @param arguments the command-line arguments
 * @throws Exception if something goes wrong
 */
public void run(String... arguments) throws Exception {
    final Bootstrap<T> bootstrap = new Bootstrap<>(this);
    addDefaultCommands(bootstrap);
    initialize(bootstrap);
    // Should be called after initialize to give an opportunity to set a custom metric registry
    bootstrap.registerMetrics();
    final Cli cli = new Cli(new JarLocation(getClass()), bootstrap, System.out, System.err);
    // only exit if there's an error running the command
    cli.run(arguments).ifPresent(this::onFatalError);
}
Also used : Cli(io.dropwizard.cli.Cli) JarLocation(io.dropwizard.util.JarLocation) Bootstrap(io.dropwizard.setup.Bootstrap)

Example 3 with Bootstrap

use of io.dropwizard.setup.Bootstrap in project dropwizard by dropwizard.

the class DropwizardTestSupport method startIfRequired.

private void startIfRequired() throws Exception {
    if (jettyServer != null) {
        return;
    }
    application = newApplication();
    final Bootstrap<C> bootstrap = new Bootstrap<C>(getApplication()) {

        @Override
        public void run(C configuration, Environment environment) throws Exception {
            environment.lifecycle().addServerLifecycleListener(server -> jettyServer = server);
            super.run(configuration, environment);
            for (ServiceListener<C> listener : listeners) {
                try {
                    listener.onRun(configuration, environment, DropwizardTestSupport.this);
                } catch (Exception ex) {
                    throw new RuntimeException("Error running app rule start listener", ex);
                }
            }
        }
    };
    getApplication().initialize(bootstrap);
    if (configSourceProvider != null) {
        bootstrap.setConfigurationSourceProvider(configSourceProvider);
    }
    if (explicitConfig) {
        bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new POJOConfigurationFactory<>(getConfiguration()));
    } else if (customPropertyPrefix != null) {
        @NotNull final String prefix = customPropertyPrefix;
        bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new YamlConfigurationFactory<>(klass, validator, objectMapper, prefix));
    }
    final Map<String, Object> namespaceAttributes = Optional.ofNullable(configPath).filter(path -> !path.isEmpty()).map(path -> Collections.singletonMap("file", (Object) path)).orElse(Collections.emptyMap());
    final Namespace namespace = new Namespace(namespaceAttributes);
    final Command command = commandInstantiator.apply(application);
    command.run(bootstrap, namespace);
    if (command instanceof EnvironmentCommand) {
        @SuppressWarnings("unchecked") EnvironmentCommand<C> environmentCommand = (EnvironmentCommand<C>) command;
        this.configuration = environmentCommand.getConfiguration();
        this.environment = environmentCommand.getEnvironment();
    } else if (command instanceof ConfiguredCommand) {
        @SuppressWarnings("unchecked") ConfiguredCommand<C> configuredCommand = (ConfiguredCommand<C>) command;
        this.configuration = configuredCommand.getConfiguration();
    }
}
Also used : ConfigurationSourceProvider(io.dropwizard.configuration.ConfigurationSourceProvider) Bootstrap(io.dropwizard.setup.Bootstrap) LoggingUtil(io.dropwizard.logging.LoggingUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Configuration(io.dropwizard.Configuration) Namespace(net.sourceforge.argparse4j.inf.Namespace) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Command(io.dropwizard.cli.Command) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) Server(org.eclipse.jetty.server.Server) Nullable(javax.annotation.Nullable) Environment(io.dropwizard.setup.Environment) Application(io.dropwizard.Application) EnvironmentCommand(io.dropwizard.cli.EnvironmentCommand) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Sets(io.dropwizard.util.Sets) Set(java.util.Set) ServerCommand(io.dropwizard.cli.ServerCommand) NotNull(javax.validation.constraints.NotNull) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServerConnector(org.eclipse.jetty.server.ServerConnector) List(java.util.List) Managed(io.dropwizard.lifecycle.Managed) Optional(java.util.Optional) Connector(org.eclipse.jetty.server.Connector) Collections(java.util.Collections) ConfiguredCommand(io.dropwizard.cli.ConfiguredCommand) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) EnvironmentCommand(io.dropwizard.cli.EnvironmentCommand) InvocationTargetException(java.lang.reflect.InvocationTargetException) Namespace(net.sourceforge.argparse4j.inf.Namespace) ConfiguredCommand(io.dropwizard.cli.ConfiguredCommand) Command(io.dropwizard.cli.Command) EnvironmentCommand(io.dropwizard.cli.EnvironmentCommand) ServerCommand(io.dropwizard.cli.ServerCommand) ConfiguredCommand(io.dropwizard.cli.ConfiguredCommand) Bootstrap(io.dropwizard.setup.Bootstrap) Environment(io.dropwizard.setup.Environment)

Example 4 with Bootstrap

use of io.dropwizard.setup.Bootstrap in project keywhiz by square.

the class KeywhizConfigTest method createObjectMapper.

private static ObjectMapper createObjectMapper() {
    KeywhizService service = new KeywhizService();
    Bootstrap<KeywhizConfig> bootstrap = new Bootstrap<>(service);
    service.initialize(bootstrap);
    ObjectMapper objectMapper = bootstrap.getObjectMapper().copy();
    return objectMapper;
}
Also used : Bootstrap(io.dropwizard.setup.Bootstrap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with Bootstrap

use of io.dropwizard.setup.Bootstrap in project keywhiz by square.

the class ServiceContext method create.

public static ServiceContext create() {
    KeywhizService service = new KeywhizService();
    Bootstrap<KeywhizConfig> bootstrap = new Bootstrap<>(service);
    service.initialize(bootstrap);
    ObjectMapper objectMapper = bootstrap.getObjectMapper().copy();
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    KeywhizConfig config = loadTestConfig(objectMapper, validator);
    Environment environment = new Environment(bootstrap.getApplication().getName(), objectMapper, validator, bootstrap.getMetricRegistry(), bootstrap.getClassLoader());
    return new ServiceContext(service, bootstrap, config, environment);
}
Also used : KeywhizService(keywhiz.KeywhizService) KeywhizConfig(keywhiz.KeywhizConfig) Bootstrap(io.dropwizard.setup.Bootstrap) Environment(io.dropwizard.setup.Environment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(javax.validation.Validator)

Aggregations

Bootstrap (io.dropwizard.setup.Bootstrap)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Environment (io.dropwizard.setup.Environment)4 Configuration (io.dropwizard.Configuration)3 JarLocation (io.dropwizard.util.JarLocation)3 ConfigurationFactory (io.dropwizard.configuration.ConfigurationFactory)2 ConfigurationSourceProvider (io.dropwizard.configuration.ConfigurationSourceProvider)2 Validator (javax.validation.Validator)2 Namespace (net.sourceforge.argparse4j.inf.Namespace)2 Injector (com.google.inject.Injector)1 Application (io.dropwizard.Application)1 Cli (io.dropwizard.cli.Cli)1 Command (io.dropwizard.cli.Command)1 ConfiguredCommand (io.dropwizard.cli.ConfiguredCommand)1 EnvironmentCommand (io.dropwizard.cli.EnvironmentCommand)1 ServerCommand (io.dropwizard.cli.ServerCommand)1 ConfigurationException (io.dropwizard.configuration.ConfigurationException)1 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)1 Managed (io.dropwizard.lifecycle.Managed)1 LoggingFactory (io.dropwizard.logging.LoggingFactory)1