Search in sources :

Example 1 with ConfigDataLocationResolverContext

use of org.springframework.boot.context.config.ConfigDataLocationResolverContext in project spring-cloud-consul by spring-cloud.

the class ConsulConfigDataLocationResolver method resolveProfileSpecific.

@Override
public List<ConsulConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, Profiles profiles) throws ConfigDataLocationNotFoundException {
    UriComponents locationUri = parseLocation(resolverContext, location);
    // create consul client
    registerBean(resolverContext, ConsulProperties.class, loadProperties(resolverContext, locationUri));
    registerAndPromoteBean(resolverContext, ConsulClient.class, this::createConsulClient);
    // create locations
    ConsulConfigProperties properties = loadConfigProperties(resolverContext);
    ConsulPropertySources consulPropertySources = new ConsulPropertySources(properties, log);
    List<Context> contexts = (locationUri == null || CollectionUtils.isEmpty(locationUri.getPathSegments())) ? consulPropertySources.generateAutomaticContexts(profiles.getAccepted(), false) : getCustomContexts(locationUri, properties);
    registerAndPromoteBean(resolverContext, ConsulConfigProperties.class, InstanceSupplier.of(properties));
    registerAndPromoteBean(resolverContext, ConsulConfigIndexes.class, InstanceSupplier.from(ConsulConfigDataIndexes::new));
    return contexts.stream().map(propertySourceContext -> new ConsulConfigDataResource(propertySourceContext.getPath(), properties, consulPropertySources, propertySourceContext.getProfile())).collect(Collectors.toList());
}
Also used : BootstrapContext(org.springframework.boot.BootstrapContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigDataLocationResolverContext(org.springframework.boot.context.config.ConfigDataLocationResolverContext) Context(org.springframework.cloud.consul.config.ConsulPropertySources.Context) ConfigurableBootstrapContext(org.springframework.boot.ConfigurableBootstrapContext) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Arrays(java.util.Arrays) BootstrapContext(org.springframework.boot.BootstrapContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Profiles(org.springframework.boot.context.config.Profiles) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigDataLocation(org.springframework.boot.context.config.ConfigDataLocation) Nullable(org.springframework.lang.Nullable) ConfigDataLocationResolverContext(org.springframework.boot.context.config.ConfigDataLocationResolverContext) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) ConfigDataLocationNotFoundException(org.springframework.boot.context.config.ConfigDataLocationNotFoundException) Bindable(org.springframework.boot.context.properties.bind.Bindable) ConsulClient(com.ecwid.consul.v1.ConsulClient) Context(org.springframework.cloud.consul.config.ConsulPropertySources.Context) ConfigurableBootstrapContext(org.springframework.boot.ConfigurableBootstrapContext) FILES(org.springframework.cloud.consul.config.ConsulConfigProperties.Format.FILES) Collectors(java.util.stream.Collectors) List(java.util.List) InstanceSupplier(org.springframework.boot.BootstrapRegistry.InstanceSupplier) ConsulAutoConfiguration(org.springframework.cloud.consul.ConsulAutoConfiguration) CollectionUtils(org.springframework.util.CollectionUtils) Log(org.apache.commons.logging.Log) Binder(org.springframework.boot.context.properties.bind.Binder) Collections(java.util.Collections) ConfigDataLocationResolver(org.springframework.boot.context.config.ConfigDataLocationResolver) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) StringUtils(org.springframework.util.StringUtils) UriComponents(org.springframework.web.util.UriComponents) UriComponents(org.springframework.web.util.UriComponents)

Example 2 with ConfigDataLocationResolverContext

use of org.springframework.boot.context.config.ConfigDataLocationResolverContext in project spring-cloud-consul by spring-cloud.

the class ConsulConfigDataLocationResolverTests method testResolveProfileSpecific.

private List<ConsulConfigDataResource> testResolveProfileSpecific(String location) {
    ConsulConfigDataLocationResolver resolver = createResolver();
    ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class);
    when(context.getBootstrapContext()).thenReturn(new DefaultBootstrapContext());
    MockEnvironment env = new MockEnvironment();
    env.setProperty("spring.application.name", "testapp");
    when(context.getBinder()).thenReturn(Binder.get(env));
    Profiles profiles = mock(Profiles.class);
    when(profiles.getAccepted()).thenReturn(Collections.singletonList("dev"));
    return resolver.resolveProfileSpecific(context, ConfigDataLocation.of(location), profiles);
}
Also used : DefaultBootstrapContext(org.springframework.boot.DefaultBootstrapContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) Profiles(org.springframework.boot.context.config.Profiles) ConfigDataLocationResolverContext(org.springframework.boot.context.config.ConfigDataLocationResolverContext)

Example 3 with ConfigDataLocationResolverContext

use of org.springframework.boot.context.config.ConfigDataLocationResolverContext in project spring-cloud-consul by spring-cloud.

the class ConsulConfigDataLocationResolverTests method testLoadProperties.

@Test
public void testLoadProperties() {
    Binder binder = Binder.get(new MockEnvironment());
    ConfigDataLocationResolverContext resolverContext = mock(ConfigDataLocationResolverContext.class);
    when(resolverContext.getBinder()).thenReturn(binder);
    when(resolverContext.getBootstrapContext()).thenReturn(new DefaultBootstrapContext());
    ConsulProperties properties = createResolver().loadProperties(resolverContext, UriComponentsBuilder.fromUriString("consul://myhost:8502").build());
    assertThat(properties.getHost()).isEqualTo("myhost");
    assertThat(properties.getPort()).isEqualTo(8502);
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) DefaultBootstrapContext(org.springframework.boot.DefaultBootstrapContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) ConfigDataLocationResolverContext(org.springframework.boot.context.config.ConfigDataLocationResolverContext) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigDataLocationResolverContext (org.springframework.boot.context.config.ConfigDataLocationResolverContext)3 DefaultBootstrapContext (org.springframework.boot.DefaultBootstrapContext)2 Profiles (org.springframework.boot.context.config.Profiles)2 Binder (org.springframework.boot.context.properties.bind.Binder)2 ConsulProperties (org.springframework.cloud.consul.ConsulProperties)2 MockEnvironment (org.springframework.mock.env.MockEnvironment)2 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Log (org.apache.commons.logging.Log)1 Test (org.junit.jupiter.api.Test)1 BootstrapContext (org.springframework.boot.BootstrapContext)1 InstanceSupplier (org.springframework.boot.BootstrapRegistry.InstanceSupplier)1 ConfigurableBootstrapContext (org.springframework.boot.ConfigurableBootstrapContext)1 ConfigDataLocation (org.springframework.boot.context.config.ConfigDataLocation)1 ConfigDataLocationNotFoundException (org.springframework.boot.context.config.ConfigDataLocationNotFoundException)1