org.zodiac.autoconfigure.bootstrap.condition.AbstractOnBootstrapCondition Maven / Gradle / Ivy
package org.zodiac.autoconfigure.bootstrap.condition;
import java.lang.annotation.Annotation;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.type.AnnotatedTypeMetadata;
abstract class AbstractOnBootstrapCondition extends SpringBootCondition {
private String message = "Spring Cloud bootstrap environment";
private final Class extends Annotation> conditionType;
private final boolean available;
AbstractOnBootstrapCondition(Class extends Annotation> conditionType, boolean available) {
this.conditionType = conditionType;
this.available = available;
}
@Override
public final ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConfigurableEnvironment environment = (ConfigurableEnvironment)context.getEnvironment();
boolean springCloudMatch = matchCondition(environment);
String message = "Spring Cloud bootstrap environment";
return new ConditionOutcome(springCloudMatch, ConditionMessage.forCondition(conditionType).available(message));
}
protected abstract boolean matchCondition(ConfigurableEnvironment environment);
protected ConditionOutcome createConditionOutcome(boolean match) {
ConditionMessage conditionMessage = available ? ConditionMessage.forCondition(conditionType).available(message)
: ConditionMessage.forCondition(conditionType).notAvailable(message);
return new ConditionOutcome(match, conditionMessage);
}
}