Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC - archive with a MutinyBean implementor is a bean archive #19902

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the name of this build item. The other alternative was AdditionalBeanArchiveBuildItem but given the fact that the item does not represent a bean archive but a logic that identifies a bean archive I decided for BeanArchivePredicateBuildItem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think BeanArchivePredicateBuildItem makes sense.


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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.grpc.internal.ServerImpl;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.BeanArchivePredicateBuildItem;
import io.quarkus.arc.deployment.CustomScopeAnnotationsBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanGizmoAdaptor;
Expand All @@ -49,6 +50,7 @@
import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.ApplicationArchive;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -546,6 +548,18 @@ void configureMetrics(GrpcBuildTimeConfig configuration, Optional<MetricsCapabil
}
}

@BuildStep
BeanArchivePredicateBuildItem additionalBeanArchives() {
return new BeanArchivePredicateBuildItem(new Predicate<ApplicationArchive>() {

@Override
public boolean test(ApplicationArchive archive) {
// Every archive that contains a generated implementor of MutinyBean is considered a bean archive
return !archive.getIndex().getKnownDirectImplementors(GrpcDotNames.MUTINY_BEAN).isEmpty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is archive.getIndex() always != null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so. But it's a good point that we could be more defensive here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we don't test the nullability in the BeanArchiveProcessor either. So it's probably OK.

}
});
}

private static class GrpcInterceptors {
final Set<String> globalInterceptors;
final Set<String> nonGlobalInterceptors;
Expand Down