use of org.apache.sling.hc.api.execution.HealthCheckSelector in project sling by apache.
the class CompositeHealthCheckTest method testCyclicRecursion.
@Test
public void testCyclicRecursion() {
// three checks, cyclic
final String[] filterTags = new String[] { "check2" };
final DummyHcServiceReference hcRef1 = new DummyHcServiceReference("Check 1", new String[] { "check1" }, filterTags);
final DummyHcServiceReference hcRef2 = new DummyHcServiceReference("Check 2", new String[] { "check2" }, new String[] { "check3" });
final DummyHcServiceReference hcRef3 = new DummyHcServiceReference("Check 3", new String[] { "check3" }, new String[] { "check1" });
// test check is hcRef1
doReturn(hcRef1).when(componentContext).getServiceReference();
compositeHealthCheck.setFilterTags(filterTags);
compositeHealthCheck.setHealthCheckFilter(new HealthCheckFilter(null) {
@Override
public ServiceReference[] getHealthCheckServiceReferences(HealthCheckSelector selector) {
String[] tags = selector.tags();
ServiceReference[] result = new ServiceReference[] {};
if (tags.length > 0) {
if (tags[0].equals(filterTags[0])) {
result = new ServiceReference[] { hcRef2 };
} else if (tags[0].equals("check3")) {
result = new ServiceReference[] { hcRef3 };
} else if (tags[0].equals("check1")) {
result = new ServiceReference[] { hcRef1 };
}
}
return result;
}
});
Result result = compositeHealthCheck.execute();
verify(healthCheckExecutor, never()).execute(any(HealthCheckSelector.class));
assertEquals(Result.Status.HEALTH_CHECK_ERROR, result.getStatus());
}
use of org.apache.sling.hc.api.execution.HealthCheckSelector in project sling by apache.
the class HealthCheckExecutorServlet method doGet.
protected void doGet(final HttpServletRequest request, final HttpServletResponse response, final String format) throws ServletException, IOException {
HealthCheckSelector selector = HealthCheckSelector.empty();
String pathInfo = request.getPathInfo();
String pathTokensStr = StringUtils.removeStart(splitFormat(pathInfo)[0], "/");
List<String> tags = new ArrayList<String>();
List<String> names = new ArrayList<String>();
if (StringUtils.isNotBlank(pathTokensStr)) {
String[] pathTokens = pathTokensStr.split(PARAM_SPLIT_REGEX);
for (String pathToken : pathTokens) {
if (pathToken.indexOf(' ') >= 0) {
// token contains space. assume it is a name
names.add(pathToken);
} else {
tags.add(pathToken);
}
}
}
if (tags.size() == 0) {
// if not provided via path use parameter or default
tags = Arrays.asList(StringUtils.defaultIfEmpty(request.getParameter(PARAM_TAGS.name), "").split(PARAM_SPLIT_REGEX));
}
selector.withTags(tags.toArray(new String[0]));
if (names.size() == 0) {
// if not provided via path use parameter or default
names = Arrays.asList(StringUtils.defaultIfEmpty(request.getParameter(PARAM_NAMES.name), "").split(PARAM_SPLIT_REGEX));
}
selector.withNames(names.toArray(new String[0]));
final Boolean includeDebug = Boolean.valueOf(request.getParameter(PARAM_INCLUDE_DEBUG.name));
final Map<Result.Status, Integer> statusMapping = request.getParameter(PARAM_HTTP_STATUS.name) != null ? getStatusMapping(request.getParameter(PARAM_HTTP_STATUS.name)) : null;
HealthCheckExecutionOptions executionOptions = new HealthCheckExecutionOptions();
executionOptions.setCombineTagsWithOr(Boolean.valueOf(StringUtils.defaultString(request.getParameter(PARAM_COMBINE_TAGS_WITH_OR.name), "true")));
executionOptions.setForceInstantExecution(Boolean.valueOf(request.getParameter(PARAM_FORCE_INSTANT_EXECUTION.name)));
String overrideGlobalTimeoutVal = request.getParameter(PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
if (StringUtils.isNumeric(overrideGlobalTimeoutVal)) {
executionOptions.setOverrideGlobalTimeout(Integer.valueOf(overrideGlobalTimeoutVal));
}
List<HealthCheckExecutionResult> executionResults = this.healthCheckExecutor.execute(selector, executionOptions);
Result.Status mostSevereStatus = Result.Status.DEBUG;
for (HealthCheckExecutionResult executionResult : executionResults) {
Status status = executionResult.getHealthCheckResult().getStatus();
if (status.ordinal() > mostSevereStatus.ordinal()) {
mostSevereStatus = status;
}
}
Result overallResult = new Result(mostSevereStatus, "Overall status " + mostSevereStatus);
sendNoCacheHeaders(response);
if (statusMapping != null) {
Integer httpStatus = statusMapping.get(overallResult.getStatus());
response.setStatus(httpStatus);
}
if (FORMAT_HTML.equals(format)) {
sendHtmlResponse(overallResult, executionResults, request, response, includeDebug);
} else if (FORMAT_JSON.equals(format)) {
sendJsonResponse(overallResult, executionResults, null, response, includeDebug);
} else if (FORMAT_JSONP.equals(format)) {
String jsonpCallback = StringUtils.defaultIfEmpty(request.getParameter(PARAM_JSONP_CALLBACK.name), JSONP_CALLBACK_DEFAULT);
sendJsonResponse(overallResult, executionResults, jsonpCallback, response, includeDebug);
} else if (StringUtils.endsWith(format, FORMAT_TXT)) {
sendTxtResponse(overallResult, response, StringUtils.equals(format, FORMAT_VERBOSE_TXT), executionResults, includeDebug);
} else {
response.setContentType("text/plain");
response.getWriter().println("Invalid format " + format + " - supported formats: html|json|jsonp|txt|verbose.txt");
}
}
use of org.apache.sling.hc.api.execution.HealthCheckSelector in project sling by apache.
the class HealthCheckFilterTest method testWithTwoNames.
@Test
public void testWithTwoNames() {
HealthCheckSelector selector = names("foo").withNames("bar");
assertStrEquals("(&(objectClass=org.apache.sling.hc.api.HealthCheck)(|(hc.name=foo)(hc.name=bar)))", filter.getServiceFilter(selector, false));
}
use of org.apache.sling.hc.api.execution.HealthCheckSelector in project sling by apache.
the class HealthCheckFilterTest method testWithTwoNamesExcludingOne.
@Test
public void testWithTwoNamesExcludingOne() {
HealthCheckSelector selector = names("foo", "bar", "-baz");
assertStrEquals("(&(objectClass=org.apache.sling.hc.api.HealthCheck)(!(hc.name=baz))(|(hc.name=foo)(hc.name=bar)))", filter.getServiceFilter(selector, false));
}
Aggregations