6

What's the right way to read configuration in dropwizard from something like a database, or a REST call? I have a use case where I cannot have a yml file with some values, and should retrieve settings/config at startup time from a preconfigured URL with REST calls.

Is it right to just invoke these REST calls in the get methods of the ApplicationConfiguration class?

ragebiswas
  • 3,818
  • 9
  • 38
  • 39

2 Answers2

6

Similar to my answer here, you implement the ConfigurationSourceProvider interface the way you wish to implement and configure your dropwizard application to use it on your Application class by:

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap){
  bootstrap.setConfigurationSourceProvider(new MyDatabaseConfigurationSourceProvider());
}

By default, the InputStream you return is read as YAML and mapped to the Configuration object. The default implementation

You can override this via

bootstrap.setConfigurationFactoryFactory(new MyDatabaseConfigurationFactoryFactory<>());

Then you have your FactoryFactory :) that returns a Factory which reads the InputStream and returns your Configuration.

public T build(ConfigurationSourceProvider provider, String path {
  Decode.onWhateverFormatYouWish(provider.open(path));
}
Community
  • 1
  • 1
Natan
  • 2,816
  • 20
  • 37
  • Thanks! So it looks like I have to return an inputstream to a YAML file from this implementation. That is, read properties from DB, persist in a yaml, and then return a handle to it. Somewhat inconvenient, but should work I guess. – ragebiswas Feb 10 '16 at 05:51
  • 1
    I didn't realize it was returning InputStream. It is somewhat inconvenient, I agree. As it looks like you can also configure how you handle the `InputStream` as well. It's still a little tedious since you still have to work with `InputStream` but at least you can work within memory. I will update my answer. – Natan Feb 10 '16 at 10:01
4

elaborating a bit further on Nathan's reply, you might want to consider using the UrlConfigurationSourceProvider , which is also provided with dropwizard, and allows to retrieve the configuration from an URL.

Something like:

@Override
public void initialize(Bootstrap<MyRestApplicationConfiguration> bootstrap) {
    bootstrap.setConfigurationSourceProvider(new UrlConfigurationSourceProvider());
}
Daniele
  • 1,053
  • 10
  • 17
  • Thanks Daniele - wish I could accept both answers. Going with Natan's since my URL doesn't exactly return a YAML - I need to process it. – ragebiswas Feb 10 '16 at 05:31