From 8f8059d5afaf65a6076b6ba99602d81c48f8dd8a Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 31 Mar 2021 05:22:28 -0400 Subject: [PATCH] Add abstract environment post processor for config data imports (#936) --- ...igDataMissingEnvironmentPostProcessor.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/ConfigDataMissingEnvironmentPostProcessor.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/ConfigDataMissingEnvironmentPostProcessor.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/ConfigDataMissingEnvironmentPostProcessor.java new file mode 100644 index 00000000..20707b59 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/ConfigDataMissingEnvironmentPostProcessor.java @@ -0,0 +1,96 @@ +/* + * Copyright 2015-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.commons; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; + +/** + * @author Ryan Baxter + */ +public abstract class ConfigDataMissingEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { + + /** + * Spring config import property name. + */ + public static final String CONFIG_IMPORT_PROPERTY = "spring.config.import"; + + private static final Bindable CONFIG_DATA_LOCATION_ARRAY = Bindable.of(String[].class); + + /** + * Order of post processor, set to run after + * {@link ConfigDataEnvironmentPostProcessor}. + */ + public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1000; + + @Override + public int getOrder() { + return ORDER; + } + + protected abstract boolean shouldProcessEnvironment(Environment environment); + + protected abstract String getPrefix(); + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + if (!shouldProcessEnvironment(environment)) { + return; + } + List property = getConfigImports(environment); + if (property == null || property.isEmpty()) { + throw new ImportException("No spring.config.import set", false); + } + if (!property.stream().anyMatch(impt -> impt.contains(getPrefix()))) { + throw new ImportException("spring.config.import missing " + getPrefix(), true); + } + } + + private List getConfigImports(ConfigurableEnvironment environment) { + List property = environment.getProperty(CONFIG_IMPORT_PROPERTY, List.class); + if (property == null || property.isEmpty()) { + Binder binder = Binder.get(environment); + property = Arrays + .asList(binder.bind(CONFIG_IMPORT_PROPERTY, CONFIG_DATA_LOCATION_ARRAY).orElse(new String[0])); + } + return property; + } + + public static class ImportException extends RuntimeException { + + /** + * Indicates if prefix is missing. + */ + public final boolean missingPrefix; + + ImportException(String message, boolean missingPrefix) { + super(message); + this.missingPrefix = missingPrefix; + } + + } + +}