Search in sources :

Example 1 with Extension

use of org.mule.runtime.extension.api.annotation.Extension in project mule by mulesoft.

the class MuleExtensionAnnotationParser method getExtension.

public static Extension getExtension(Class<?> extensionType) {
    try {
        Extension extension = extensionType.getAnnotation(Extension.class);
        checkState(extension != null, format("%s is not a Mule extension since it's not annotated with %s", extensionType.getName(), Extension.class.getName()));
        return extension;
    } catch (Exception e) {
        logger.error(format("%s getting '@Extension' annotation from %s", e.getClass().getName(), extensionType.getName()), e);
        throw e;
    }
}
Also used : Extension(org.mule.runtime.extension.api.annotation.Extension) OnException(org.mule.runtime.extension.api.annotation.OnException)

Example 2 with Extension

use of org.mule.runtime.extension.api.annotation.Extension in project mule by mulesoft.

the class ExtensionPluginMetadataGenerator method scanForExtensionAnnotatedClasses.

/**
 * Scans for a {@link Class} annotated with {@link Extension} annotation and return the {@link Class} or {@code null} if there
 * is no annotated {@link Class}. It would only look for classes when the URLs for the plugin have an artifact not packaged
 * (target/classes/ or target-test/classes).
 *
 * @param plugin the {@link Artifact} to generate its extension manifest if it is an extension.
 * @param urls {@link URL}s to use for discovering {@link Class}es annotated with {@link Extension}
 * @return {@link Class} annotated with {@link Extension} or {@code null}
 */
Class scanForExtensionAnnotatedClasses(Artifact plugin, List<URL> urls) {
    final URL firstURL = urls.stream().findFirst().get();
    logger.debug("Scanning plugin '{}' for annotated Extension class from {}", plugin, firstURL);
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Extension.class));
    try (URLClassLoader classLoader = new URLClassLoader(new URL[] { firstURL }, null)) {
        scanner.setResourceLoader(new PathMatchingResourcePatternResolver(classLoader));
        Set<BeanDefinition> extensionsAnnotatedClasses = scanner.findCandidateComponents("");
        if (!extensionsAnnotatedClasses.isEmpty()) {
            if (extensionsAnnotatedClasses.size() > 1) {
                logger.warn("While scanning class loader on plugin '{}' for discovering @Extension classes annotated, more than one " + "found. It will pick up the first one, found: {}", plugin, extensionsAnnotatedClasses);
            }
            String extensionClassName = extensionsAnnotatedClasses.iterator().next().getBeanClassName();
            try {
                return Class.forName(extensionClassName);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Cannot load Extension class '" + extensionClassName + "'", e);
            }
        }
        logger.debug("No class found annotated with {}", Extension.class.getName());
        return null;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) URL(java.net.URL) Extension(org.mule.runtime.extension.api.annotation.Extension) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) URLClassLoader(java.net.URLClassLoader) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 3 with Extension

use of org.mule.runtime.extension.api.annotation.Extension in project mule by mulesoft.

the class AetherClassPathClassifier method buildPluginUrlClassifications.

/**
 * Plugin classifications are being done by resolving the dependencies for each plugin coordinates defined by the rootArtifact
 * direct dependencies as {@value #MULE_SERVICE_CLASSIFIER}.
 * <p/>
 * While resolving the dependencies for the plugin artifact, only {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE}
 * dependencies will be selected. {@link ClassPathClassifierContext#getExcludedArtifacts()} will be exluded too.
 * <p/>
 * The resulting {@link PluginUrlClassification} for each plugin will have as name the Maven artifact id coordinates:
 * {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}.
 *
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param directDependencies {@link List} of {@link Dependency} with direct dependencies for the rootArtifact
 * @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact
 * @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
 * @return {@link List} of {@link PluginUrlClassification}s for plugins class loaders
 */
private List<PluginUrlClassification> buildPluginUrlClassifications(ClassPathClassifierContext context, List<Dependency> directDependencies, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
    Set<ArtifactClassificationNode> pluginsClassified = newLinkedHashSet();
    Artifact rootArtifact = context.getRootArtifact();
    List<Artifact> pluginsArtifacts = directDependencies.stream().filter(dependency -> dependency.getArtifact().getClassifier().equals(MULE_PLUGIN_CLASSIFIER)).map(dependency -> dependency.getArtifact()).collect(toList());
    logger.debug("{} plugins defined to be classified", pluginsArtifacts.size());
    Predicate<Dependency> mulePluginDependencyFilter = dependency -> dependency.getArtifact().getClassifier().equals(MULE_PLUGIN_CLASSIFIER) && dependency.getScope().equals(COMPILE);
    if (PLUGIN.equals(rootArtifactType)) {
        logger.debug("rootArtifact '{}' identified as Mule plugin", rootArtifact);
        buildPluginUrlClassification(rootArtifact, context, mulePluginDependencyFilter, pluginsClassified, rootArtifactRemoteRepositories);
        pluginsArtifacts = pluginsArtifacts.stream().filter(pluginArtifact -> !(rootArtifact.getGroupId().equals(pluginArtifact.getGroupId()) && rootArtifact.getArtifactId().equals(pluginArtifact.getArtifactId()))).collect(toList());
    }
    pluginsArtifacts.stream().forEach(pluginArtifact -> buildPluginUrlClassification(pluginArtifact, context, mulePluginDependencyFilter, pluginsClassified, rootArtifactRemoteRepositories));
    if (context.isExtensionMetadataGenerationEnabled()) {
        ExtensionPluginMetadataGenerator extensionPluginMetadataGenerator = new ExtensionPluginMetadataGenerator(context.getPluginResourcesFolder());
        for (ArtifactClassificationNode pluginClassifiedNode : pluginsClassified) {
            List<URL> urls = generateExtensionMetadata(pluginClassifiedNode.getArtifact(), context, extensionPluginMetadataGenerator, pluginClassifiedNode.getUrls(), rootArtifactRemoteRepositories);
            pluginClassifiedNode.setUrls(urls);
        }
    }
    return toPluginUrlClassification(pluginsClassified);
}
Also used : PLUGIN(org.mule.test.runner.api.ArtifactClassificationType.PLUGIN) ListIterator(java.util.ListIterator) URL(java.net.URL) Optional.of(java.util.Optional.of) TEST(org.eclipse.aether.util.artifact.JavaScopes.TEST) LoggerFactory(org.slf4j.LoggerFactory) DependencyFilterUtils.orFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.orFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) APPLICATION(org.mule.test.runner.api.ArtifactClassificationType.APPLICATION) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) Collections.singleton(java.util.Collections.singleton) DependencyFilterUtils.andFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.andFilter) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactIdUtils.toId(org.eclipse.aether.util.artifact.ArtifactIdUtils.toId) PROVIDED(org.eclipse.aether.util.artifact.JavaScopes.PROVIDED) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) String.format(java.lang.String.format) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) Optional.empty(java.util.Optional.empty) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Extension(org.mule.runtime.extension.api.annotation.Extension) Dependency(org.eclipse.aether.graph.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) COMPILE(org.eclipse.aether.util.artifact.JavaScopes.COMPILE) MODULE(org.mule.test.runner.api.ArtifactClassificationType.MODULE) VersionChecker.areCompatibleVersions(org.mule.maven.client.internal.util.VersionChecker.areCompatibleVersions) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Sets.newLinkedHashSet(com.google.common.collect.Sets.newLinkedHashSet) IOException(java.io.IOException) File(java.io.File) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Collectors.toList(java.util.stream.Collectors.toList) FileFilter(java.io.FileFilter) StringUtils.endsWithIgnoreCase(org.apache.commons.lang3.StringUtils.endsWithIgnoreCase) VersionChecker.isHighestVersion(org.mule.maven.client.internal.util.VersionChecker.isHighestVersion) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) Exclusion(org.eclipse.aether.graph.Exclusion) DependencyFilterUtils.classpathFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.classpathFilter) Collections(java.util.Collections) Dependency(org.eclipse.aether.graph.Dependency) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) URL(java.net.URL)

Aggregations

Extension (org.mule.runtime.extension.api.annotation.Extension)3 IOException (java.io.IOException)2 URL (java.net.URL)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 Sets.newLinkedHashSet (com.google.common.collect.Sets.newLinkedHashSet)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 UncheckedIOException (java.io.UncheckedIOException)1 String.format (java.lang.String.format)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singleton (java.util.Collections.singleton)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1