Skip to content

Commit

Permalink
Merge pull request #10930 from geoand/#10720
Browse files Browse the repository at this point in the history
Warn when JAX-RS @path is used along with MP Health annotations
  • Loading branch information
jmartisk authored Jul 23, 2020
2 parents b50faf5 + f36b000 commit d5fa09d
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.microprofile.health.Readiness;
import org.eclipse.microprofile.health.spi.HealthCheckResponseProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -88,6 +89,7 @@ class SmallRyeHealthProcessor {
private static final DotName READINESS = DotName.createSimple(Readiness.class.getName());
private static final DotName HEALTH_GROUP = DotName.createSimple(HealthGroup.class.getName());
private static final DotName HEALTH_GROUPS = DotName.createSimple(HealthGroups.class.getName());
private static final DotName JAX_RS_PATH = DotName.createSimple("javax.ws.rs.Path");

// For the UI
private static final String HEALTH_UI_WEBJAR_GROUP_ID = "io.smallrye";
Expand Down Expand Up @@ -176,6 +178,11 @@ public void defineHealthRoutes(BuildProducer<RouteBuildItem> routes,
BeanArchiveIndexBuildItem beanArchiveIndex) {
IndexView index = beanArchiveIndex.getIndex();

// log a warning if users try to use MP Health annotations with JAX-RS @Path
warnIfJaxRsPathUsed(index, LIVENESS);
warnIfJaxRsPathUsed(index, READINESS);
warnIfJaxRsPathUsed(index, HEALTH);

// Register the health handler
routes.produce(new RouteBuildItem(health.rootPath, new SmallRyeHealthHandler(), HandlerType.BLOCKING));

Expand Down Expand Up @@ -215,6 +222,29 @@ public void defineHealthRoutes(BuildProducer<RouteBuildItem> routes,
}
}

private void warnIfJaxRsPathUsed(IndexView index, DotName healthAnnotation) {
Collection<AnnotationInstance> instances = index.getAnnotations(healthAnnotation);
for (AnnotationInstance instance : instances) {
boolean containsPath = false;

AnnotationTarget target = instance.target();
if (target.kind() == Kind.CLASS) {
if (target.asClass().classAnnotation(JAX_RS_PATH) != null) {
containsPath = true;
}
} else if (target.kind() == Kind.METHOD) {
if (target.asMethod().hasAnnotation(JAX_RS_PATH)) {
containsPath = true;
}
}
if (containsPath) {
LOG.warnv(
"The use of @Path has no effect when @{0} is used and should therefore be removed. Offending target is {1}: {2}",
healthAnnotation.withoutPackagePrefix(), target.kind(), target);
}
}
}

@BuildStep
public void kubernetes(HttpBuildTimeConfig httpConfig,
BuildProducer<KubernetesHealthLivenessPathBuildItem> livenessPathItemProducer,
Expand Down

0 comments on commit d5fa09d

Please sign in to comment.