Skip to content

Commit

Permalink
ArC - make it possible to identify additional bean archives
Browse files Browse the repository at this point in the history
(cherry picked from commit c91431c)
  • Loading branch information
mkouba authored and gsmet committed Sep 6, 2021
1 parent c1cccb6 commit 063afdd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.arc.deployment;

import java.util.function.Predicate;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.deployment.ApplicationArchive;

/**
*
* By default, only explict/implicit bean archives (as defined by the spec) are considered during the bean discovery. However,
* extensions can register a logic to identify additional bean archives.
*/
public final class BeanArchivePredicateBuildItem extends MultiBuildItem {

private final Predicate<ApplicationArchive> predicate;

public BeanArchivePredicateBuildItem(Predicate<ApplicationArchive> predicate) {
this.predicate = predicate;
}

public Predicate<ApplicationArchive> getPredicate() {
return predicate;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.quarkus.arc.deployment;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.jboss.jandex.AnnotationInstance;
Expand Down Expand Up @@ -35,12 +40,13 @@ public BeanArchiveIndexBuildItem build(ArcConfig config, ApplicationArchivesBuil
List<BeanDefiningAnnotationBuildItem> additionalBeanDefiningAnnotations,
List<AdditionalBeanBuildItem> additionalBeans, List<GeneratedBeanBuildItem> generatedBeans,
LiveReloadBuildItem liveReloadBuildItem, BuildProducer<GeneratedClassBuildItem> generatedClass,
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems)
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems,
List<BeanArchivePredicateBuildItem> beanArchivePredicates)
throws Exception {

// First build an index from application archives
IndexView applicationIndex = buildApplicationIndex(config, applicationArchivesBuildItem,
additionalBeanDefiningAnnotations, customScopes, excludeDependencyBuildItems);
additionalBeanDefiningAnnotations, customScopes, excludeDependencyBuildItems, beanArchivePredicates);

// Then build additional index for beans added by extensions
Indexer additionalBeanIndexer = new Indexer();
Expand Down Expand Up @@ -81,7 +87,8 @@ public BeanArchiveIndexBuildItem build(ArcConfig config, ApplicationArchivesBuil

private IndexView buildApplicationIndex(ArcConfig config, ApplicationArchivesBuildItem applicationArchivesBuildItem,
List<BeanDefiningAnnotationBuildItem> additionalBeanDefiningAnnotations,
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems) {
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems,
List<BeanArchivePredicateBuildItem> beanArchivePredicates) {

Set<ApplicationArchive> archives = applicationArchivesBuildItem.getAllApplicationArchives();

Expand Down Expand Up @@ -116,17 +123,35 @@ private IndexView buildApplicationIndex(ArcConfig config, ApplicationArchivesBui
continue;
}
IndexView index = archive.getIndex();
// NOTE: Implicit bean archive without beans.xml contains one or more bean classes with a bean defining annotation and no extension
if (archive.getChildPath("META-INF/beans.xml") != null || archive.getChildPath("WEB-INF/beans.xml") != null
|| (index.getAllKnownImplementors(DotNames.EXTENSION).isEmpty()
&& containsBeanDefiningAnnotation(index, beanDefiningAnnotations))) {
if (isExplicitBeanArchive(archive) || isImplicitBeanArchive(index, beanDefiningAnnotations)
|| isAdditionalBeanArchive(archive, beanArchivePredicates)) {
indexes.add(index);
}
}
indexes.add(applicationArchivesBuildItem.getRootArchive().getIndex());
return CompositeIndex.create(indexes);
}

private boolean isExplicitBeanArchive(ApplicationArchive archive) {
return archive.getChildPath("META-INF/beans.xml") != null || archive.getChildPath("WEB-INF/beans.xml") != null;
}

private boolean isImplicitBeanArchive(IndexView index, Set<DotName> beanDefiningAnnotations) {
// NOTE: Implicit bean archive without beans.xml contains one or more bean classes with a bean defining annotation and no extension
return index.getAllKnownImplementors(DotNames.EXTENSION).isEmpty()
&& containsBeanDefiningAnnotation(index, beanDefiningAnnotations);
}

private boolean isAdditionalBeanArchive(ApplicationArchive archive,
List<BeanArchivePredicateBuildItem> beanArchivePredicates) {
for (BeanArchivePredicateBuildItem p : beanArchivePredicates) {
if (p.getPredicate().test(archive)) {
return true;
}
}
return false;
}

private boolean isApplicationArchiveExcluded(ArcConfig config, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems,
ApplicationArchive archive) {
if (archive.getArtifactKey() != null) {
Expand Down

0 comments on commit 063afdd

Please sign in to comment.