001/*
002 * Copyright 2012-2018 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.autoconfigure.condition;
018
019import java.util.List;
020import java.util.function.Supplier;
021
022import org.springframework.boot.context.properties.bind.BindResult;
023import org.springframework.boot.context.properties.bind.Bindable;
024import org.springframework.boot.context.properties.bind.Binder;
025import org.springframework.context.annotation.Condition;
026import org.springframework.context.annotation.ConditionContext;
027import org.springframework.core.type.AnnotatedTypeMetadata;
028
029/**
030 * {@link Condition} that checks if a property whose value is a list is defined in the
031 * environment.
032 *
033 * @author Eneias Silva
034 * @author Stephane Nicoll
035 * @since 2.0.5
036 */
037public class OnPropertyListCondition extends SpringBootCondition {
038
039        private static final Bindable<List<String>> STRING_LIST = Bindable
040                        .listOf(String.class);
041
042        private final String propertyName;
043
044        private final Supplier<ConditionMessage.Builder> messageBuilder;
045
046        /**
047         * Create a new instance with the property to check and the message builder to use.
048         * @param propertyName the name of the property
049         * @param messageBuilder a message builder supplier that should provide a fresh
050         * instance on each call
051         */
052        protected OnPropertyListCondition(String propertyName,
053                        Supplier<ConditionMessage.Builder> messageBuilder) {
054                this.propertyName = propertyName;
055                this.messageBuilder = messageBuilder;
056        }
057
058        @Override
059        public ConditionOutcome getMatchOutcome(ConditionContext context,
060                        AnnotatedTypeMetadata metadata) {
061                BindResult<?> property = Binder.get(context.getEnvironment())
062                                .bind(this.propertyName, STRING_LIST);
063                ConditionMessage.Builder messageBuilder = this.messageBuilder.get();
064                if (property.isBound()) {
065                        return ConditionOutcome
066                                        .match(messageBuilder.found("property").items(this.propertyName));
067                }
068                return ConditionOutcome
069                                .noMatch(messageBuilder.didNotFind("property").items(this.propertyName));
070        }
071
072}