|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.condition; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.springframework.beans.BeansException; |
| 25 | +import org.springframework.beans.factory.BeanClassLoaderAware; |
| 26 | +import org.springframework.beans.factory.BeanFactory; |
| 27 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter; |
| 29 | +import org.springframework.boot.autoconfigure.AutoConfigurationMetadata; |
| 30 | +import org.springframework.util.ClassUtils; |
| 31 | +import org.springframework.util.CollectionUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Abstract base class for a {@link SpringBootCondition} that also implements |
| 35 | + * {@link AutoConfigurationImportFilter}. |
| 36 | + * |
| 37 | + * @author Phillip Webb |
| 38 | + */ |
| 39 | +abstract class FilteringSpringBootCondition extends SpringBootCondition |
| 40 | + implements AutoConfigurationImportFilter, BeanFactoryAware, BeanClassLoaderAware { |
| 41 | + |
| 42 | + private BeanFactory beanFactory; |
| 43 | + |
| 44 | + private ClassLoader beanClassLoader; |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean[] match(String[] autoConfigurationClasses, |
| 48 | + AutoConfigurationMetadata autoConfigurationMetadata) { |
| 49 | + ConditionEvaluationReport report = ConditionEvaluationReport |
| 50 | + .find(this.beanFactory); |
| 51 | + ConditionOutcome[] outcomes = getOutcomes(autoConfigurationClasses, |
| 52 | + autoConfigurationMetadata); |
| 53 | + boolean[] match = new boolean[outcomes.length]; |
| 54 | + for (int i = 0; i < outcomes.length; i++) { |
| 55 | + match[i] = (outcomes[i] == null || outcomes[i].isMatch()); |
| 56 | + if (!match[i] && outcomes[i] != null) { |
| 57 | + logOutcome(autoConfigurationClasses[i], outcomes[i]); |
| 58 | + if (report != null) { |
| 59 | + report.recordConditionEvaluation(autoConfigurationClasses[i], this, |
| 60 | + outcomes[i]); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return match; |
| 65 | + } |
| 66 | + |
| 67 | + protected abstract ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses, |
| 68 | + AutoConfigurationMetadata autoConfigurationMetadata); |
| 69 | + |
| 70 | + @Override |
| 71 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 72 | + this.beanFactory = beanFactory; |
| 73 | + } |
| 74 | + |
| 75 | + protected final BeanFactory getBeanFactory() { |
| 76 | + return this.beanFactory; |
| 77 | + } |
| 78 | + |
| 79 | + protected final ClassLoader getBeanClassLoader() { |
| 80 | + return this.beanClassLoader; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 85 | + this.beanClassLoader = classLoader; |
| 86 | + } |
| 87 | + |
| 88 | + protected List<String> filter(Collection<String> classNames, |
| 89 | + ClassNameFilter classNameFilter, ClassLoader classLoader) { |
| 90 | + if (CollectionUtils.isEmpty(classNames)) { |
| 91 | + return Collections.emptyList(); |
| 92 | + } |
| 93 | + List<String> matches = new ArrayList<>(classNames.size()); |
| 94 | + for (String candidate : classNames) { |
| 95 | + if (classNameFilter.matches(candidate, classLoader)) { |
| 96 | + matches.add(candidate); |
| 97 | + } |
| 98 | + } |
| 99 | + return matches; |
| 100 | + } |
| 101 | + |
| 102 | + protected enum ClassNameFilter { |
| 103 | + |
| 104 | + PRESENT { |
| 105 | + |
| 106 | + @Override |
| 107 | + public boolean matches(String className, ClassLoader classLoader) { |
| 108 | + return isPresent(className, classLoader); |
| 109 | + } |
| 110 | + |
| 111 | + }, |
| 112 | + |
| 113 | + MISSING { |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean matches(String className, ClassLoader classLoader) { |
| 117 | + return !isPresent(className, classLoader); |
| 118 | + } |
| 119 | + |
| 120 | + }; |
| 121 | + |
| 122 | + public abstract boolean matches(String className, ClassLoader classLoader); |
| 123 | + |
| 124 | + public static boolean isPresent(String className, ClassLoader classLoader) { |
| 125 | + if (classLoader == null) { |
| 126 | + classLoader = ClassUtils.getDefaultClassLoader(); |
| 127 | + } |
| 128 | + try { |
| 129 | + forName(className, classLoader); |
| 130 | + return true; |
| 131 | + } |
| 132 | + catch (Throwable ex) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static Class<?> forName(String className, ClassLoader classLoader) |
| 138 | + throws ClassNotFoundException { |
| 139 | + if (classLoader != null) { |
| 140 | + return classLoader.loadClass(className); |
| 141 | + } |
| 142 | + return Class.forName(className); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments