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

FISH-5780 FISH-5779 create web component invocation around mp health check call #5540

Merged
merged 1 commit into from
Dec 20, 2021
Merged
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
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