org.grails.spring.boot.env.YamlPropertySourceLoader Maven / Gradle / Ivy
/*
* Copyright 2018-2023 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.grails.spring.boot.env;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.config.YamlProcessor;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import grails.util.Environment;
import org.grails.config.NavigableMap;
import org.grails.config.NavigableMapPropertySource;
/**
* Replacement for Spring Boot's YAML loader that uses Grails' NavigableMap.
*
* @author graemerocher
* @since 3.0
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
public class YamlPropertySourceLoader extends YamlProcessor implements PropertySourceLoader {
private static final String ENVIRONMENTS = "environments";
@Override
public String[] getFileExtensions() {
return new String[] { "yml", "yaml" };
}
@Override
public List> load(String name, Resource resource) throws IOException {
return load(name, resource, Collections.emptyList());
}
public List> load(String name, Resource resource, List filteredKeys) throws IOException {
setResources(resource);
setDocumentMatchers(properties -> {
String profile = properties.getProperty("spring.profiles");
return profile == null || profile.equalsIgnoreCase(System.getProperty("spring.profiles.active")) ?
MatchStatus.FOUND : MatchStatus.NOT_FOUND;
});
List