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

com.atlassian.bamboo.specs.maven.sandbox.FileToBambooSpecsMapper Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
package com.atlassian.bamboo.specs.maven.sandbox;

import org.apache.maven.plugin.logging.Log;

import java.lang.annotation.Annotation;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Function;

public class FileToBambooSpecsMapper implements Function>> {
    private final Log log;
    private final ClassLoader classLoader;
    private final Class planAnnotation;

    public FileToBambooSpecsMapper(final Log log, final ClassLoader classLoader) {
        this.log = log;
        this.classLoader = classLoader;
        try {
            planAnnotation = (Class) this.classLoader.loadClass("com.atlassian.bamboo.specs.api.BambooSpec");
        } catch (final ClassNotFoundException e) {
            throw new RuntimeException("Unable to find Bamboo plan marker", e);
        }
    }

    @Override
    public Optional> apply(final Path path) {
        try {
            final String className = path.toString()
                    .replace("/", ".")
                    .replace("\\", ".")
                    .replace(".class", "");

            log.debug("Scanning " + className + " for " + planAnnotation.getSimpleName() + " annotation.");


            final Class someClass = classLoader.loadClass(className);

            if (someClass.getAnnotation(planAnnotation) == null) {
                log.debug(planAnnotation.getSimpleName() + " annotation not found on " + someClass);
                return Optional.empty();
            }

            log.info("Found @" + planAnnotation.getSimpleName() + " annotation on " + someClass);
            return Optional.of(someClass);
        } catch (final ClassNotFoundException e) {
            log.warn("Error when loading class " + path, e);
            return Optional.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy