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