Search in sources :

Example 1 with EnvironmentCommand

use of io.dropwizard.cli.EnvironmentCommand 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 2 with EnvironmentCommand

use of io.dropwizard.cli.EnvironmentCommand in project dropwizard-guicey by xvik.

the class CommandSupport method initCommands.

/**
 * Inject dependencies into all registered environment commands. (only field and setter injection could be used)
 * There is no need to process other commands, because only environment commands will run bundles and so will
 * start the injector.
 *
 * @param commands registered commands
 * @param injector guice injector object
 * @param tracker  stats tracker
 */
public static void initCommands(final List<Command> commands, final Injector injector, final StatsTracker tracker) {
    final Stopwatch timer = tracker.timer(CommandTime);
    if (commands != null) {
        for (Command cmd : commands) {
            if (cmd instanceof EnvironmentCommand) {
                injector.injectMembers(cmd);
            }
        }
    }
    timer.stop();
}
Also used : EnvironmentCommand(io.dropwizard.cli.EnvironmentCommand) EnvironmentCommand(io.dropwizard.cli.EnvironmentCommand) Command(io.dropwizard.cli.Command) Stopwatch(com.google.common.base.Stopwatch)

Aggregations

Command (io.dropwizard.cli.Command)2 EnvironmentCommand (io.dropwizard.cli.EnvironmentCommand)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Stopwatch (com.google.common.base.Stopwatch)1 Application (io.dropwizard.Application)1 Configuration (io.dropwizard.Configuration)1 ConfiguredCommand (io.dropwizard.cli.ConfiguredCommand)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