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.autoconfigure.condition;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.springframework.context.annotation.Condition;
023import org.springframework.core.Ordered;
024import org.springframework.core.annotation.Order;
025
026/**
027 * {@link Condition} that will match when any nested class condition matches. Can be used
028 * to create composite conditions, for example:
029 *
030 * <pre class="code">
031 * static class OnJndiOrProperty extends AnyNestedCondition {
032 *
033 *    OnJndiOrProperty() {
034 *        super(ConfigurationPhase.PARSE_CONFIGURATION);
035 *    }
036 *
037 *    &#064;ConditionalOnJndi()
038 *    static class OnJndi {
039 *    }
040 *
041 *    &#064;ConditionalOnProperty("something")
042 *    static class OnProperty {
043 *    }
044 *
045 * }
046 * </pre>
047 * <p>
048 * The
049 * {@link org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase
050 * ConfigurationPhase} should be specified according to the conditions that are defined.
051 * In the example above, all conditions are static and can be evaluated early so
052 * {@code PARSE_CONFIGURATION} is a right fit.
053 *
054 * @author Phillip Webb
055 * @since 1.2.0
056 */
057@Order(Ordered.LOWEST_PRECEDENCE - 20)
058public abstract class AnyNestedCondition extends AbstractNestedCondition {
059
060        public AnyNestedCondition(ConfigurationPhase configurationPhase) {
061                super(configurationPhase);
062        }
063
064        @Override
065        protected ConditionOutcome getFinalMatchOutcome(MemberMatchOutcomes memberOutcomes) {
066                boolean match = !memberOutcomes.getMatches().isEmpty();
067                List<ConditionMessage> messages = new ArrayList<>();
068                messages.add(ConditionMessage.forCondition("AnyNestedCondition")
069                                .because(memberOutcomes.getMatches().size() + " matched "
070                                                + memberOutcomes.getNonMatches().size() + " did not"));
071                for (ConditionOutcome outcome : memberOutcomes.getAll()) {
072                        messages.add(outcome.getConditionMessage());
073                }
074                return new ConditionOutcome(match, ConditionMessage.of(messages));
075        }
076
077}