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;
}
}
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);
}
}
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);
}
Aggregations