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;
018
019import java.io.IOException;
020import java.util.List;
021
022import org.springframework.beans.factory.BeanClassLoaderAware;
023import org.springframework.context.annotation.Configuration;
024import org.springframework.core.io.support.SpringFactoriesLoader;
025import org.springframework.core.type.classreading.MetadataReader;
026import org.springframework.core.type.classreading.MetadataReaderFactory;
027import org.springframework.core.type.filter.TypeFilter;
028
029/**
030 * A {@link TypeFilter} implementation that matches registered auto-configuration classes.
031 *
032 * @author Stephane Nicoll
033 * @since 1.5.0
034 */
035public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoaderAware {
036
037        private ClassLoader beanClassLoader;
038
039        private volatile List<String> autoConfigurations;
040
041        @Override
042        public void setBeanClassLoader(ClassLoader beanClassLoader) {
043                this.beanClassLoader = beanClassLoader;
044        }
045
046        @Override
047        public boolean match(MetadataReader metadataReader,
048                        MetadataReaderFactory metadataReaderFactory) throws IOException {
049                return isConfiguration(metadataReader) && isAutoConfiguration(metadataReader);
050        }
051
052        private boolean isConfiguration(MetadataReader metadataReader) {
053                return metadataReader.getAnnotationMetadata()
054                                .isAnnotated(Configuration.class.getName());
055        }
056
057        private boolean isAutoConfiguration(MetadataReader metadataReader) {
058                return getAutoConfigurations()
059                                .contains(metadataReader.getClassMetadata().getClassName());
060        }
061
062        protected List<String> getAutoConfigurations() {
063                if (this.autoConfigurations == null) {
064                        this.autoConfigurations = SpringFactoriesLoader.loadFactoryNames(
065                                        EnableAutoConfiguration.class, this.beanClassLoader);
066                }
067                return this.autoConfigurations;
068        }
069
070}