View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.web.health;
17  
18  import org.codehaus.jackson.annotate.JsonIgnore;
19  import org.codehaus.jackson.annotate.JsonProperty;
20  import org.codehaus.jackson.map.annotate.JsonSerialize;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * Indicates the health status of the application.
27   *
28   * Can be one of either {@link #OK} or {@link #FAILED}. This class is designed to be serializable to JSON if using
29   * the Jackson library.
30   *
31   * @author Eric Westfall
32   */
33  public class HealthStatus {
34  
35      public static final String OK = "Ok";
36      public static final String FAILED = "Failed";
37  
38      @JsonProperty("Status")
39      private String statusCode;
40  
41      @JsonProperty("Message")
42      @JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
43      private String message;
44  
45      @JsonProperty("Metrics")
46      private List<HealthMetric> metrics;
47  
48      public HealthStatus() {
49          this(OK);
50      }
51  
52      public HealthStatus(String statusCode) {
53          setStatusCode(statusCode);
54          setMetrics(new ArrayList<HealthMetric>());
55      }
56  
57      @JsonIgnore
58      public boolean isOk() {
59          return OK.equals(statusCode);
60      }
61  
62      public String getStatusCode() {
63          return statusCode;
64      }
65  
66      public void setStatusCode(String statusCode) {
67          if (!statusCode.equals(OK) && !statusCode.equals(FAILED)) {
68              throw new IllegalArgumentException("Status code must be one of '" + OK + "' or '" + FAILED + "'");
69          }
70          this.statusCode = statusCode;
71      }
72  
73      public String getMessage() {
74          return message;
75      }
76  
77      public void setMessage(String message) {
78          this.message = message;
79      }
80  
81      public List<HealthMetric> getMetrics() {
82          return metrics;
83      }
84  
85      public void setMetrics(List<HealthMetric> metrics) {
86          if (metrics == null) {
87              throw new IllegalArgumentException("metrics list must not be null");
88          }
89          this.metrics = metrics;
90      }
91  
92      public void appendMessage(String metricName, String message) {
93          String fullMessage = "* " + metricName + " -> " + message;
94          if (getMessage() == null) {
95              setMessage(fullMessage);
96          } else {
97              setMessage(getMessage() + " " + fullMessage);
98          }
99      }
100 
101 }