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.JsonProperty;
19 import org.apache.commons.lang.StringUtils;
20
21 /**
22 * Defines the name and value for a single metric.
23 *
24 * All metrics must have a "Measure" name, a "Metric" name and a value. The value may be any value that translates
25 * property to JSON. The value may also be null.
26 *
27 * This class is annotated such that it should serialize to JSON when using the Jackson library.
28 *
29 * @author Eric Westfall
30 */
31 public class HealthMetric {
32
33 @JsonProperty("Measure")
34 private final String measure;
35
36 @JsonProperty("Metric")
37 private final String metric;
38
39 @JsonProperty("Value")
40 private final Object value;
41
42 /**
43 * Construct a new HealthMetric using the given two-art name and value. The format of the name is "Measure:Metric"
44 * where "Measure" and "Metric" are replaced by the desired measure and metric name.
45 *
46 * Invoking new HealthMetric("a:b", "c") is equivalent to invoking new HealthMetric("a", "b", "c")
47 *
48 * @param name the name for this health metric, must be a string that includes two non-blank parts separated by a colon
49 * @param value the value of this health metric
50 */
51 public HealthMetric(String name, Object value) {
52 if (StringUtils.isBlank(name)) {
53 throw new IllegalArgumentException("Metric name must not be blank");
54 }
55 String[] nameParts = name.split(":");
56 if (nameParts.length != 2 || nameParts[0].isEmpty() || nameParts[1].isEmpty()) {
57 throw new IllegalArgumentException("Metric name was not valid, should be two non-blank parts separated by ':'. Instead was " + name);
58 }
59 this.measure = nameParts[0];
60 this.metric = nameParts[1];
61 this.value = value;
62 }
63
64 /**
65 * Construct a new HealthMetric with the given measure name, metric name, and value.
66 *
67 * @param measure the name of the measure
68 * @param metric the name of the metric
69 * @param value the value of this health metric
70 */
71 public HealthMetric(String measure, String metric, Object value) {
72 if (StringUtils.isBlank(measure)) {
73 throw new IllegalArgumentException("measure name must not be blank");
74 }
75 if (StringUtils.isBlank(metric)) {
76 throw new IllegalArgumentException("metric name must not be blank");
77 }
78 this.measure = measure;
79 this.metric = metric;
80 this.value = value;
81 }
82
83 public String getMeasure() {
84 return measure;
85 }
86
87 public String getMetric() {
88 return metric;
89 }
90
91 public Object getValue() {
92 return value;
93 }
94
95 }