Search in sources :

Example 1 with ConfiguredCommand

use of io.dropwizard.cli.ConfiguredCommand 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Application (io.dropwizard.Application)1 Configuration (io.dropwizard.Configuration)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 ConfigurationSourceProvider (io.dropwizard.configuration.ConfigurationSourceProvider)1 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)1 Managed (io.dropwizard.lifecycle.Managed)1 LoggingUtil (io.dropwizard.logging.LoggingUtil)1 Bootstrap (io.dropwizard.setup.Bootstrap)1 Environment (io.dropwizard.setup.Environment)1 Sets (io.dropwizard.util.Sets)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1