Skip to content

Commit

Permalink
Let InitializrStatsAutoConfiguration back off if Elastic URI is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Feb 5, 2024
1 parent 057efb7 commit af5208b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,16 +24,21 @@
import io.spring.initializr.metadata.InitializrMetadataProvider;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.StringUtils;

/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
Expand All @@ -44,7 +49,7 @@
@AutoConfiguration(after = RestTemplateAutoConfiguration.class,
afterName = "io.spring.initializr.web.autoconfigure.InitializrAutoConfiguration")
@EnableConfigurationProperties(StatsProperties.class)
@ConditionalOnProperty("initializr.stats.elastic.uri")
@Conditional(InitializrStatsAutoConfiguration.ElasticUriCondition.class)
class InitializrStatsAutoConfiguration {

private final StatsProperties statsProperties;
Expand Down Expand Up @@ -74,4 +79,17 @@ RetryTemplate statsRetryTemplate() {
return retryTemplate;
}

static class ElasticUriCondition extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String elasticUri = context.getEnvironment().getProperty("initializr.stats.elastic.uri");
if (StringUtils.hasText(elasticUri)) {
return ConditionOutcome.match("initializr.stats.elastic.uri is set");
}
return ConditionOutcome.noMatch("initializr.stats.elastic.uri is not set");
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,6 +90,17 @@ void customRestTemplateBuilderIsUsed() {
});
}

@Test
void shouldBackOffIfElasticUriIsNotSet() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ProjectGenerationStatPublisher.class));
}

@Test
void shouldBackOffIfElasticUriIsEmpty() {
this.contextRunner.withPropertyValues("initializr.stats.elastic.uri=")
.run((context) -> assertThat(context).doesNotHaveBean(ProjectGenerationStatPublisher.class));
}

@Configuration
static class CustomStatsRetryTemplateConfiguration {

Expand Down

0 comments on commit af5208b

Please sign in to comment.