001/* 002 * (C) Copyright 2017-2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Mariana Cedica 018 */ 019package org.nuxeo.ecm.core.management.statuses; 020 021import java.util.Collection; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.apache.commons.lang3.StringUtils; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.management.api.ProbeInfo; 029 030import com.fasterxml.jackson.core.JsonProcessingException; 031import com.fasterxml.jackson.databind.ObjectMapper; 032 033/** 034 * Returns the status of the application 035 * 036 * @since 9.3 037 */ 038public class HealthCheckResult { 039 040 private static final Log log = LogFactory.getLog(HealthCheckResult.class); 041 042 protected Collection<ProbeInfo> probes; 043 044 protected boolean healthy; 045 046 public HealthCheckResult(Collection<ProbeInfo> probesToCheck) { 047 this.probes = probesToCheck; 048 healthy = probes.stream().allMatch(p -> p.getStatus().isSuccess()); 049 } 050 051 public boolean isHealthy() { 052 return healthy; 053 } 054 055 public String toJson() { 056 ObjectMapper om = new ObjectMapper(); 057 Map<String, String> res = new HashMap<>(); 058 try { 059 for (ProbeInfo probe : probes) { 060 res.put(probe.getShortcutName(), (probe.getStatus().isSuccess() ? "ok" : "failed")); 061 } 062 return om.writeValueAsString(res); 063 } catch (JsonProcessingException e) { 064 log.error(e); 065 return StringUtils.EMPTY; 066 } 067 } 068}