Skip to content

Commit

Permalink
Merge pull request #5540 from aubi/FISH-5780-no-valid-ee-in-mp-health
Browse files Browse the repository at this point in the history
FISH-5780 FISH-5779 create web component invocation around mp health check call
  • Loading branch information
aubi authored Dec 20, 2021
2 parents b3c7e11 + cbac503 commit 8ce5c38
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
*/
package fish.payara.microprofile.healthcheck;

import com.sun.enterprise.web.WebApplication;
import com.sun.enterprise.web.WebComponentInvocation;
import com.sun.enterprise.web.WebContainer;
import static fish.payara.microprofile.healthcheck.HealthCheckType.LIVENESS;
import static fish.payara.microprofile.healthcheck.HealthCheckType.READINESS;
import static fish.payara.microprofile.healthcheck.HealthCheckType.STARTUP;
Expand Down Expand Up @@ -99,6 +102,10 @@
import fish.payara.monitoring.collect.MonitoringWatchSource;
import fish.payara.nucleus.healthcheck.configuration.Checker;
import fish.payara.nucleus.healthcheck.events.PayaraHealthCheckServiceEvents;
import java.util.Optional;
import org.glassfish.api.invocation.InvocationException;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.internal.data.ApplicationInfo;

/**
* Service that handles the registration, execution, and response of MicroProfile HealthChecks.
Expand All @@ -115,6 +122,9 @@ public class HealthCheckService implements EventListener, ConfigListener, Monito
@Inject
private ApplicationRegistry applicationRegistry;

@Inject
private InvocationManager invocationManager;

@Inject
private MicroprofileHealthCheckConfiguration configuration;

Expand Down Expand Up @@ -445,14 +455,28 @@ private HealthCheckResponse performHealthCheckInApplicationContext(
String appName, HealthCheck healthCheck) {
Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
Optional<WebComponentInvocation> wciOpt = Optional.empty();
try {
currentThread.setContextClassLoader(applicationRegistry.get(appName).getAppClassLoader());
ApplicationInfo appInfo = applicationRegistry.get(appName);
currentThread.setContextClassLoader(appInfo.getAppClassLoader());
wciOpt = createWebComponentInvocation(appInfo);
wciOpt.ifPresent(wci -> invocationManager.preInvoke(wci));
return healthCheck.call();
} finally {
wciOpt.ifPresent(wci -> invocationManager.postInvoke(wci));
currentThread.setContextClassLoader(originalClassLoader);
}
}

private Optional<WebComponentInvocation> createWebComponentInvocation(ApplicationInfo appInfo) throws InvocationException {
return appInfo.getModuleInfos().stream()
.map(mi -> mi.getEngineRefForContainer(WebContainer.class))
.filter(engineRef -> engineRef != null)
.flatMap(engineRef -> ((WebApplication) engineRef.getApplicationContainer()).getWebModules().stream())
.findFirst()
.map(wm -> new WebComponentInvocation(wm));
}

private void constructResponse(HttpServletResponse httpResponse,
Set<HealthCheckResponse> healthCheckResponses,
HealthCheckType type, String enablePrettyPrint) throws IOException {
Expand Down

0 comments on commit 8ce5c38

Please sign in to comment.