001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.context.properties.source;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.Set;
023import java.util.function.Function;
024
025import org.springframework.core.env.PropertySource;
026import org.springframework.core.env.StandardEnvironment;
027
028/**
029 * Function used to determine if a {@link ConfigurationPropertySource} should be included
030 * when determining unbound elements. If the underlying {@link PropertySource} is a
031 * systemEnvironment or systemProperties property source, it will not be considered for
032 * unbound element failures.
033 *
034 * @author Madhura Bhave
035 * @since 2.0.0
036 */
037public class UnboundElementsSourceFilter
038                implements Function<ConfigurationPropertySource, Boolean> {
039
040        private static final Set<String> BENIGN_PROPERTY_SOURCE_NAMES = Collections
041                        .unmodifiableSet(new HashSet<>(Arrays.asList(
042                                        StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
043                                        StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)));
044
045        @Override
046        public Boolean apply(ConfigurationPropertySource configurationPropertySource) {
047                Object underlyingSource = configurationPropertySource.getUnderlyingSource();
048                if (underlyingSource instanceof PropertySource) {
049                        String name = ((PropertySource<?>) underlyingSource).getName();
050                        return !BENIGN_PROPERTY_SOURCE_NAMES.contains(name);
051
052                }
053                return true;
054        }
055
056}