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

de.codecentric.cxf.autodetection.WebServiceScanner Maven / Gradle / Ivy

Go to download

Boot starter for SOAP-Webservices with Apache CXF using JAX-WS & JAXB with Annotations only

The newest version!
package de.codecentric.cxf.autodetection;

import de.codecentric.cxf.common.BootStarterCxfException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class WebServiceScanner {

    protected static final String NO_CLASS_FOUND = "No class found";

    protected  Class scanForClassWhichImplementsAndPickFirst(Class interfaceName, String packageName) throws BootStarterCxfException {
        List namesOfClassesWithInterface = new ArrayList<>();

        Set beans = scanForClasses(new AssignableTypeFilter(interfaceName), packageName);

        if (beans.isEmpty()) {
            throw new BootStarterCxfException(WebServiceAutoDetector.NO_CLASS_FOUND);
        }

        beans.forEach((BeanDefinition beanDef) -> namesOfClassesWithInterface.add(beanDef.getBeanClassName()));
        return justPickTheClassThatIsNotAnInterface(namesOfClassesWithInterface);
    }

    protected Class justPickTheClassThatIsNotAnInterface(List namesOfClasses) throws BootStarterCxfException {
        for (String className : namesOfClasses) {
            if (!isInterface(className)) {
                return classForName(className);
            }
        }
        throw new BootStarterCxfException(NO_CLASS_FOUND);
    }

    private Set scanForClasses(TypeFilter typeFilter, String basePackage) {
        ClassPathScanningCandidateComponentProvider scanningProvider = new InterfaceIncludingClassPathScanningCandidateComponentProvider();
        scanningProvider.addIncludeFilter(typeFilter);
        return scanningProvider.findCandidateComponents(basePackage);
    }

    protected  Class scanForClassWithAnnotationAndPickTheFirstOneFound(Class annotationName, String packageName) throws BootStarterCxfException {
        return classForName(scanForClassNamesWithAnnotation(annotationName, packageName).get(0));
    }

    protected  List scanForClassNamesWithAnnotation(Class annotation, String packageName) throws BootStarterCxfException {
        List namesOfClassesWithAnnotation = new ArrayList<>();

        Set beans = scanForClasses(new AnnotationTypeFilter(annotation), packageName);

        if(beans.isEmpty()) {
            throw new BootStarterCxfException(NO_CLASS_FOUND);
        }

        beans.stream().forEach((BeanDefinition bean) -> namesOfClassesWithAnnotation.add(bean.getBeanClassName()));
        return namesOfClassesWithAnnotation;
    }

    protected   Class scanForClassWithAnnotationAndIsAnInterface(Class annotationName, String packageName) throws BootStarterCxfException {
        List namesOfClassesWithAnnotation = scanForClassNamesWithAnnotation(annotationName, packageName);

        if(namesOfClassesWithAnnotation.size() > 1) {
            return justPickTheClassThatIsAnInterface(namesOfClassesWithAnnotation);
        } else {
            return classForName(namesOfClassesWithAnnotation.get(0));
        }
    }

    protected Class justPickTheClassThatIsAnInterface(List namesOfClassesWithAnnotation) throws BootStarterCxfException {
        for (String className : namesOfClassesWithAnnotation) {
            if (isInterface(className)) {
                return classForName(className);
            }
        }
        throw new BootStarterCxfException(NO_CLASS_FOUND);
    }

    protected boolean isInterface(String className) throws BootStarterCxfException {
        return classForName(className).isInterface();
    }

    protected Class classForName(String className) throws BootStarterCxfException {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException exception) {
            throw new BootStarterCxfException(NO_CLASS_FOUND, exception);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy