001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.health; 018 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.Map; 022import java.util.Optional; 023 024import org.apache.camel.Builder; 025import org.apache.camel.util.ObjectHelper; 026 027/** 028 * A builder helper to create a result. 029 */ 030public final class HealthCheckResultBuilder implements Builder<HealthCheck.Result> { 031 private HealthCheck check; 032 private String message; 033 private Throwable error; 034 private Map<String, Object> details; 035 private HealthCheck.State state; 036 037 private HealthCheckResultBuilder(HealthCheck check) { 038 this.check = check; 039 } 040 041 public String message() { 042 return this.message; 043 } 044 045 public HealthCheckResultBuilder message(String message) { 046 this.message = message; 047 return this; 048 } 049 050 public Throwable error() { 051 return this.error; 052 } 053 054 public HealthCheckResultBuilder error(Throwable error) { 055 this.error = error; 056 return this; 057 } 058 059 public Object detail(String key) { 060 return this.details != null ? this.details.get(key) : null; 061 } 062 063 public HealthCheckResultBuilder detail(String key, Object value) { 064 if (this.details == null) { 065 this.details = new HashMap<>(); 066 } 067 068 this.details.put(key, value); 069 return this; 070 } 071 072 public HealthCheckResultBuilder details(Map<String, Object> details) { 073 if (ObjectHelper.isNotEmpty(details)) { 074 details.forEach(this::detail); 075 } 076 077 return this; 078 } 079 080 public HealthCheck.State state() { 081 return this.state; 082 } 083 084 public HealthCheckResultBuilder state(HealthCheck.State state) { 085 this.state = state; 086 return this; 087 } 088 089 public HealthCheckResultBuilder up() { 090 return state(HealthCheck.State.UP); 091 } 092 093 public HealthCheckResultBuilder down() { 094 return state(HealthCheck.State.DOWN); 095 } 096 097 public HealthCheckResultBuilder unknown() { 098 return state(HealthCheck.State.UNKNOWN); 099 } 100 101 @Override 102 public HealthCheck.Result build() { 103 // Validation 104 ObjectHelper.notNull(this.state, "Response State"); 105 106 final HealthCheck.State responseState = this.state; 107 final Optional<String> responseMessage = Optional.ofNullable(this.message); 108 final Optional<Throwable> responseError = Optional.ofNullable(this.error); 109 final Map<String, Object> responseDetails = HealthCheckResultBuilder.this.details != null 110 ? Collections.unmodifiableMap(new HashMap<>(HealthCheckResultBuilder.this.details)) 111 : Collections.emptyMap(); 112 113 return new HealthCheck.Result() { 114 @Override 115 public HealthCheck getCheck() { 116 return check; 117 } 118 119 @Override 120 public HealthCheck.State getState() { 121 return responseState; 122 } 123 124 @Override 125 public Optional<String> getMessage() { 126 return responseMessage; 127 } 128 129 @Override 130 public Optional<Throwable> getError() { 131 return responseError; 132 } 133 134 @Override 135 public Map<String, Object> getDetails() { 136 return responseDetails; 137 } 138 }; 139 } 140 141 public static HealthCheckResultBuilder on(HealthCheck check) { 142 return new HealthCheckResultBuilder(check); 143 } 144}