From 40b55b0ff63ea81eacf42d2b2fc148b73b849c01 Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Thu, 22 May 2014 17:40:38 +0200 Subject: [PATCH] Extract AbstractHealthAggregator This commit makes it easier for users to implement HealthAggregators --- .../health/AbstractHealthAggregator.java | 46 +++++++++++++++++ .../health/OrderedHealthAggregator.java | 51 ++++++++----------- 2 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java new file mode 100644 index 00000000000..375e11b4b63 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.health; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Base {@link HealthAggregator} implementation to allow subclasses to focus on + * aggregating the {@link Status} instances and not deal with contextual details etc. + * + * @author Christian Dupuis + * @since 1.1.0 + */ +public abstract class AbstractHealthAggregator implements HealthAggregator { + + @Override + public final Health aggregate(Map healths) { + Health health = new Health(); + + List status = new ArrayList(); + for (Map.Entry h : healths.entrySet()) { + health.withDetail(h.getKey(), h.getValue()); + status.add(h.getValue().getStatus()); + } + health.status(aggregateStatus(status)); + return health; + } + + protected abstract Status aggregateStatus(List status); +} diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/OrderedHealthAggregator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/OrderedHealthAggregator.java index 0ca86f4b54a..b12fb198408 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/OrderedHealthAggregator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/OrderedHealthAggregator.java @@ -16,12 +16,10 @@ package org.springframework.boot.actuate.health; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; -import java.util.Map; /** * Default {@link HealthAggregator} implementation that aggregates {@link Health} @@ -34,48 +32,41 @@ import java.util.Map; * @author Christian Dupuis * @since 1.1.0 */ -public class OrderedHealthAggregator implements HealthAggregator { +public class OrderedHealthAggregator extends AbstractHealthAggregator { private List statusOrder = Arrays.asList("DOWN", "OUT_OF_SERVICE", "UP", "UNKOWN"); - @Override - public Health aggregate(Map healths) { - Health health = new Health(); - - List status = new ArrayList(); - for (Map.Entry h : healths.entrySet()) { - health.withDetail(h.getKey(), h.getValue()); - status.add(h.getValue().getStatus()); - } - health.status(aggregateStatus(status)); - return health; - } - public void setStatusOrder(List statusOrder) { this.statusOrder = statusOrder; } + @Override protected Status aggregateStatus(List status) { - + // If no status is given return UNKOWN if (status.size() == 0) { return Status.UNKOWN; } - Collections.sort(status, new Comparator() { - - @Override - public int compare(Status s1, Status s2) { - return Integer.valueOf( - OrderedHealthAggregator.this.statusOrder.indexOf(s1.getCode())) - .compareTo( - Integer.valueOf(OrderedHealthAggregator.this.statusOrder - .indexOf(s2.getCode()))); - - } - }); - + // Sort given Status instances by configured order + Collections.sort(status, new StatusComparator(this.statusOrder)); return status.get(0); } + private class StatusComparator implements Comparator { + + private final List statusOrder; + + public StatusComparator(List statusOrder) { + this.statusOrder = statusOrder; + } + + @Override + public int compare(Status s1, Status s2) { + return Integer.valueOf(this.statusOrder.indexOf(s1.getCode())).compareTo( + Integer.valueOf(this.statusOrder.indexOf(s2.getCode()))); + } + + } + }