2

I'm working on making a custom properties provider to load the contents of a Spring cloud config server at startup. I need to make a single call at the initialization of the provider to fetch these properties, and would like to use the Mule HttpService in order to make the http client for this call, instead of creating my own. Unfortunately, whenever I try this, it seems the HttpService hasn't been created yet and so throws an NPE once it's referenced.

CustomConfigurationPropertiesProviderFactory.java

public class CustomConfigurationPropertiesProviderFactory implements ConfigurationPropertiesProviderFactory {

  public static final String EXTENSION_NAMESPACE = "custom-properties";
  public static final String CONFIGURATION_PROPERTIES_ELEMENT = "config";
  public static final ComponentIdentifier CUSTOM_CONFIGURATION_PROPERTIES =
      builder().namespace(EXTENSION_NAMESPACE).name(CONFIGURATION_PROPERTIES_ELEMENT).build();

  @Inject
  HttpService httpService;

  @Override
  public ComponentIdentifier getSupportedComponentIdentifier() {
    return CUSTOM_CONFIGURATION_PROPERTIES;
  }

  @Override
  public ConfigurationPropertiesProvider createProvider(ConfigurationParameters parameters,
                                                              ResourceProvider externalResourceProvider) {
    String url = parameters.getStringParameter("url");

    return new CustomConfigurationPropertiesProvider(url, httpService);
  }

}

CustomConfigurationPropertiesProvider.java

public class CustomConfigurationPropertiesProvider implements ConfigurationPropertiesProvider {

  private final static String PREFIX = "custom::";

  private Properties properties = null;

  public CustomConfigurationPropertiesProvider(String url, HttpService httpService) {
    HttpClientConfiguration.Builder builder = new HttpClientConfiguration.Builder();
    builder.setName("customProperties");
    HttpClient client = httpService.getClientFactory().create(builder.build()); //NPE here
    client.start();
    // proceed to create and execute request, then load into properties
  }

  @Override
  public Optional<ConfigurationProperty> getConfigurationProperty(String configurationAttributeKey) {
    if (configurationAttributeKey.startsWith(PREFIX)) {
      String effectiveKey = configurationAttributeKey.substring(PREFIX.length());
      if (properties != null && !properties.isEmpty()) {
        return Optional.of(new ConfigurationProperty() {
          @Override
          public Object getSource() {...}

          @Override
          public Object getRawValue() { return properties.getProperty(effectiveKey); }

          @Override
          public String getKey() { return effectiveKey; }
        });
      }
    }
    return Optional.empty();
  }
}

What do I need to change to properly inject this service?

I've been following the advice from these two bits of documentation, for reference:

alexcjcd
  • 31
  • 5
  • This looks like you are injecting the service on a class that has no lifecycle associated to it. I would attempt the injection at the provider level and extending from `DefaultConfigurationPropertiesProvider ` as in the docs example. To get further help, I suggest posting the exact error you get. – afelisatti Nov 11 '19 at 12:34

0 Answers0