Skip to content

Commit

Permalink
Merge pull request #33 from gaol/issue_30
Browse files Browse the repository at this point in the history
[issue-30] Dependencies need to be add to the deployment module when using Instance API
  • Loading branch information
gaol authored Nov 6, 2024
2 parents a23ab10 + fcf7e83 commit 727d2dd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module name="org.wildfly.subsystem"/>
<module name="org.jboss.as.weld.common"/>
<module name="org.jboss.jandex"/>
<module name="org.jboss.vfs"/>
<module name="io.vertx.core" />
<module name="io.smallrye.common.annotation" />
<module name="io.smallrye.reactive.mutiny.vertx-core" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
*/
package org.wildfly.extension.vertx.processors;

import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.Phase;
import org.jboss.as.server.deployment.annotation.CompositeIndex;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.Type;
import org.jboss.vfs.VirtualFile;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX;
import static org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE;

/**
*
Expand All @@ -33,36 +37,54 @@ public class VerticleDeploymentMarkerProcessor implements DeploymentUnitProcesso
public static final Phase PHASE = Phase.PARSE;
public static final int PRIORITY = 0x4000;

private static final String VERTX_ANNOTATION_NAME = "io.vertx.core.Vertx";
private static final String VERTX_MUTINY_ANNOTATION_NAME = "io.vertx.mutiny.core.Vertx";
private static final DotName DOT_NAME_INJECTION = DotName.createSimple("jakarta.inject.Inject");
private static final DotName DOT_NAME_VERTX_ANNOTATION = DotName.createSimple("io.vertx.core.Vertx");
private static final DotName DOT_NAME_VERTX_MUTINY_ANNOTATION = DotName.createSimple("io.vertx.mutiny.core.Vertx");
private static final DotName DOT_NAME_CDI_INSTANCE = DotName.createSimple("jakarta.enterprise.inject.Instance");

private static final List<DotName> dotNames = new ArrayList<>();
private static final Set<String> VERTX_CLASSES = new HashSet<>();
private static final Set<DotName> VERTX_CLASSES = new HashSet<>();
static {
dotNames.add(DotName.createSimple("jakarta.inject.Inject"));
dotNames.add(DotName.createSimple("jakarta.annotation.Resource"));
VERTX_CLASSES.add(VERTX_ANNOTATION_NAME);
VERTX_CLASSES.add(VERTX_MUTINY_ANNOTATION_NAME);
VERTX_CLASSES.add(DOT_NAME_VERTX_ANNOTATION);
VERTX_CLASSES.add(DOT_NAME_VERTX_MUTINY_ANNOTATION);
}

@Override
public void deploy(DeploymentPhaseContext context) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
final CompositeIndex index = deploymentUnit.getAttachment(COMPOSITE_ANNOTATION_INDEX);
if (dotNames.stream().anyMatch(dotName -> annotated(index, dotName))) {
if (annotated(index, DOT_NAME_INJECTION) || descriptorExists(deploymentUnit)) {
VertxDeploymentAttachment.attachVertxDeployments(deploymentUnit);
}
}

private boolean descriptorExists(DeploymentUnit deploymentUnit) {
ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
if (deploymentRoot != null && deploymentRoot.getRoot() != null) {
VirtualFile vertxDeploymentFile = deploymentRoot.getRoot().getChild("WEB-INF/vertx.json");
if (vertxDeploymentFile == null || !vertxDeploymentFile.exists()) {
vertxDeploymentFile = deploymentRoot.getRoot().getChild("META-INF/vertx.json");
}
return vertxDeploymentFile != null && vertxDeploymentFile.exists() && vertxDeploymentFile.isFile();
}
return false;
}

private boolean annotated(CompositeIndex index, DotName injectAnnotationName) {
final List<AnnotationInstance> resourceAnnotations = index.getAnnotations(injectAnnotationName);
for (AnnotationInstance annotation : resourceAnnotations) {
final AnnotationTarget annotationTarget = annotation.target();
if (annotationTarget instanceof FieldInfo) {
final String fieldType = annotationTarget.asField().type().name().toString();
Type type = annotationTarget.asField().type();
final DotName fieldType = type.name();
if (VERTX_CLASSES.contains(fieldType)) {
return true;
}
if (DOT_NAME_CDI_INSTANCE.equals(fieldType) && type.kind() == PARAMETERIZED_TYPE) {
List<Type> arguments = type.asParameterizedType().arguments();
if (arguments.size() == 1 && VERTX_CLASSES.contains(arguments.get(0).name())) {
return true;
}
}
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class VertxDependenciesProcessor implements DeploymentUnitProcessor {
private static final String MODULE_VERTX_CORE = "io.vertx.core";
private static final String MODULE_VERTX_MUTINY_CORE = "io.smallrye.reactive.mutiny.vertx-core";
private static final String MODULE_MUTINY = "io.smallrye.reactive.mutiny";
private static final String MODULE_SMALLRYE_COMMON_ANNOTATION = "io.smallrye.common.annotation";

@Override
public void deploy(DeploymentPhaseContext context) throws DeploymentUnitProcessingException {
Expand All @@ -51,7 +52,8 @@ public void deploy(DeploymentPhaseContext context) throws DeploymentUnitProcessi
new ModuleDependency(moduleLoader, MODULE_VERTX_EXTENSION, false, true, true, false),
new ModuleDependency(moduleLoader, MODULE_VERTX_CORE, false, true, true, false),
new ModuleDependency(moduleLoader, MODULE_VERTX_MUTINY_CORE, false, true, true, false),
new ModuleDependency(moduleLoader, MODULE_MUTINY, false, true, true, false)
new ModuleDependency(moduleLoader, MODULE_MUTINY, false, true, true, false),
new ModuleDependency(moduleLoader, MODULE_SMALLRYE_COMMON_ANNOTATION, true, false, false, false)
));
}

Expand Down

0 comments on commit 727d2dd

Please sign in to comment.