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();
}
}
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();
}
Aggregations