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