001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.web.health;
017
018import org.codehaus.jackson.annotate.JsonIgnore;
019import org.codehaus.jackson.annotate.JsonProperty;
020import org.codehaus.jackson.map.annotate.JsonSerialize;
021
022import java.util.ArrayList;
023import java.util.List;
024
025/**
026 * Indicates the health status of the application.
027 *
028 * Can be one of either {@link #OK} or {@link #FAILED}. This class is designed to be serializable to JSON if using
029 * the Jackson library.
030 *
031 * @author Eric Westfall
032 */
033public class HealthStatus {
034
035    public static final String OK = "Ok";
036    public static final String FAILED = "Failed";
037
038    @JsonProperty("Status")
039    private String statusCode;
040
041    @JsonProperty("Message")
042    @JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
043    private String message;
044
045    @JsonProperty("Metrics")
046    private List<HealthMetric> metrics;
047
048    public HealthStatus() {
049        this(OK);
050    }
051
052    public HealthStatus(String statusCode) {
053        setStatusCode(statusCode);
054        setMetrics(new ArrayList<HealthMetric>());
055    }
056
057    @JsonIgnore
058    public boolean isOk() {
059        return OK.equals(statusCode);
060    }
061
062    public String getStatusCode() {
063        return statusCode;
064    }
065
066    public void setStatusCode(String statusCode) {
067        if (!statusCode.equals(OK) && !statusCode.equals(FAILED)) {
068            throw new IllegalArgumentException("Status code must be one of '" + OK + "' or '" + FAILED + "'");
069        }
070        this.statusCode = statusCode;
071    }
072
073    public String getMessage() {
074        return message;
075    }
076
077    public void setMessage(String message) {
078        this.message = message;
079    }
080
081    public List<HealthMetric> getMetrics() {
082        return metrics;
083    }
084
085    public void setMetrics(List<HealthMetric> metrics) {
086        if (metrics == null) {
087            throw new IllegalArgumentException("metrics list must not be null");
088        }
089        this.metrics = metrics;
090    }
091
092    public void appendMessage(String metricName, String message) {
093        String fullMessage = "* " + metricName + " -> " + message;
094        if (getMessage() == null) {
095            setMessage(fullMessage);
096        } else {
097            setMessage(getMessage() + " " + fullMessage);
098        }
099    }
100
101}