All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avanza.astrix.context.AstrixApiProviderClassScanner Maven / Gradle / Ivy

/*
 * Copyright 2014 Avanza Bank AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.avanza.astrix.context;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.avanza.astrix.beans.publish.ApiProviderClass;
import com.avanza.astrix.beans.publish.ApiProviders;
/**
 * Uses classpath scanning to find api-providers. 

* * @author Elias Lindholm (elilin) * */ public class AstrixApiProviderClassScanner implements ApiProviders { private final Logger log = LoggerFactory.getLogger(AstrixApiProviderClassScanner.class); private static final Map> apiProvidersByBasePackage = new ConcurrentHashMap<>(); private final List basePackages = new ArrayList<>(); private final List> providerAnnotationsToScanFor; public AstrixApiProviderClassScanner(List> providerAnnotationsToScanFor, String basePackage, String... otherBasePackages) { this.providerAnnotationsToScanFor = providerAnnotationsToScanFor; this.basePackages.add(basePackage); this.basePackages.addAll(Arrays.asList(otherBasePackages)); } @Override public Stream getAll() { return basePackages.stream() .flatMap(this::scanPackage); } private Stream scanPackage(String basePackage) { log.debug("Scanning package for api-providers: package={}", basePackage); List providerClasses = apiProvidersByBasePackage.get(basePackage); if (providerClasses != null) { log.debug("Returning cached api-providers found on earlier scan types={}", providerClasses); return providerClasses.stream(); } List> allProviderAnnotationTypes = getAllProviderAnnotationTypes(); log.debug("Running scan for api-providers of types={}", allProviderAnnotationTypes); List discoveredApiPRoviders = new ArrayList<>(); Reflections reflections = new Reflections(basePackage); for (Class apiAnnotation : allProviderAnnotationTypes) { for (Class providerClass : reflections.getTypesAnnotatedWith(apiAnnotation)) { ApiProviderClass provider = ApiProviderClass.create(providerClass); log.debug("Found api provider {}", provider); discoveredApiPRoviders.add(provider); } } apiProvidersByBasePackage.put(basePackage, discoveredApiPRoviders); return discoveredApiPRoviders.stream(); } void addBasePackage(String basePackage) { this.basePackages.add(basePackage); } private List> getAllProviderAnnotationTypes() { return providerAnnotationsToScanFor; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy