Search in sources :

Example 1 with HealthMetaData

use of org.wildfly.swarm.microprofile.health.HealthMetaData in project wildfly-swarm by wildfly-swarm.

the class HealthAnnotationProcessor method process.

@Override
public void process() throws NamingException {
    // first pass: jboss-web context root
    Optional<String> jbossWebContext = Optional.empty();
    // if (archive instanceof JBossWebContainer) {
    if (archive.getName().endsWith(".war")) {
        JBossWebContainer war = archive.as(WARArchive.class);
        if (war.getContextRoot() != null) {
            jbossWebContext = Optional.of(war.getContextRoot());
        }
    }
    // second pass: JAX-RS applications
    Optional<String> appPath = Optional.empty();
    Collection<AnnotationInstance> appPathAnnotations = index.getAnnotations(APP_PATH);
    for (AnnotationInstance annotation : appPathAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            appPath = Optional.of(annotation.value().asString());
        }
    }
    // third pass: JAX-RS resources
    Collection<AnnotationInstance> pathAnnotations = index.getAnnotations(PATH);
    for (AnnotationInstance annotation : pathAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            ClassInfo classInfo = annotation.target().asClass();
            for (MethodInfo methodInfo : classInfo.methods()) {
                if (methodInfo.hasAnnotation(HEALTH) || methodInfo.hasAnnotation(MP_HEALTH)) {
                    StringBuilder sb = new StringBuilder();
                    boolean isSecure = false;
                    // prepend the jboss-web cntext if given
                    if (jbossWebContext.isPresent() && !jbossWebContext.get().equals("/")) {
                        safeAppend(sb, jbossWebContext.get());
                    }
                    // prepend the appPath if given
                    if (appPath.isPresent() && !appPath.get().equals("/")) {
                        safeAppend(sb, appPath.get());
                    }
                    // the class level @Path
                    for (AnnotationInstance classAnnotation : classInfo.classAnnotations()) {
                        if (classAnnotation.name().equals(PATH)) {
                            String methodPathValue = classAnnotation.value().asString();
                            if (!methodPathValue.equals("/")) {
                                safeAppend(sb, methodPathValue);
                            }
                        }
                    }
                    if (methodInfo.hasAnnotation(PATH)) {
                        // the method level @Path
                        safeAppend(sb, methodInfo.annotation(PATH).value().asString());
                        // the method level @Health either MP or regular Swarm
                        AnnotationInstance healthAnnotation = methodInfo.annotation(HEALTH);
                        if (null == healthAnnotation) {
                            healthAnnotation = methodInfo.annotation(MP_HEALTH);
                        }
                        isSecure = healthAnnotation.value("inheritSecurity") != null ? healthAnnotation.value("inheritSecurity").asBoolean() : true;
                    } else {
                        throw new RuntimeException("@Health requires an explicit @Path annotation");
                    }
                    HealthMetaData metaData = new HealthMetaData(sb.toString(), isSecure);
                    Monitor.lookup().registerHealth(metaData);
                }
            }
        }
    }
}
Also used : HealthMetaData(org.wildfly.swarm.microprofile.health.HealthMetaData) JBossWebContainer(org.wildfly.swarm.undertow.descriptors.JBossWebContainer) MethodInfo(org.jboss.jandex.MethodInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 ClassInfo (org.jboss.jandex.ClassInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1 HealthMetaData (org.wildfly.swarm.microprofile.health.HealthMetaData)1 JBossWebContainer (org.wildfly.swarm.undertow.descriptors.JBossWebContainer)1