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.time.Duration;
020
021import org.apache.camel.RuntimeCamelException;
022import org.apache.camel.converter.TimePatternConverter;
023import org.apache.camel.util.ObjectHelper;
024
025public class HealthCheckConfiguration implements Cloneable {
026    public static final Boolean DEFAULT_VALUE_ENABLED = Boolean.FALSE;
027    public static final Duration DEFAULT_VALUE_INTERVAL = Duration.ZERO;
028    public static final Integer DEFAULT_VALUE_FAILURE_THRESHOLD = 0;
029
030    /**
031     * Set if the check associated to this configuration is enabled or not.
032     */
033    private Boolean enabled;
034
035    /**
036     * Set the check interval.
037     */
038    private Duration interval;
039
040    /**
041     * Set the number of failure before reporting the service as un-healthy.
042     */
043    private Integer failureThreshold;
044
045    // *************************************************
046    // Properties
047    // *************************************************
048
049    /**
050     * @return true if the check associated to this configuration is enabled,
051     *         false otherwise.
052     */
053    public Boolean isEnabled() {
054        return enabled;
055    }
056
057    /**
058     * Set if the check associated to this configuration is enabled or not.
059     */
060    public void setEnabled(Boolean enabled) {
061        this.enabled = enabled;
062    }
063
064    /**
065     * @return the check interval.
066     */
067    public Duration getInterval() {
068        return interval;
069    }
070
071    /**
072     * Set the check interval.
073     */
074    public void setInterval(Duration interval) {
075        this.interval = interval;
076    }
077
078    /**
079     * Set the check interval in a human readable format.
080     */
081    public void setInterval(String interval) {
082        if (ObjectHelper.isNotEmpty(interval)) {
083            this.interval = Duration.ofMillis(TimePatternConverter.toMilliSeconds(interval));
084        } else {
085            this.interval = null;
086        }
087    }
088
089    /**
090     * @return the number of failure before reporting the service as un-healthy.
091     */
092    public Integer getFailureThreshold() {
093        return failureThreshold;
094    }
095
096    /**
097     * Set the number of failure before reporting the service as un-healthy.
098     */
099    public void setFailureThreshold(Integer failureThreshold) {
100        this.failureThreshold = failureThreshold;
101    }
102
103    // *************************************************
104    //
105    // *************************************************
106    public static Boolean defaultValueEnabled() {
107        return DEFAULT_VALUE_ENABLED;
108    }
109
110    public static Duration defaultValueInterval() {
111        return DEFAULT_VALUE_INTERVAL;
112    }
113
114    public static Integer defaultValueFailureThreshold() {
115        return DEFAULT_VALUE_FAILURE_THRESHOLD;
116    }
117
118    public HealthCheckConfiguration copy() {
119        try {
120            return (HealthCheckConfiguration)super.clone();
121        } catch (CloneNotSupportedException e) {
122            throw new RuntimeCamelException(e);
123        }
124    }
125
126    public static Builder builder() {
127        return new Builder();
128    }
129
130    // *************************************************
131    //
132    // *************************************************
133
134    public static final class Builder implements org.apache.camel.Builder<HealthCheckConfiguration> {
135        private Boolean enabled;
136        private Duration interval;
137        private Integer failureThreshold;
138
139        private Builder() {
140        }
141
142        public Builder complete(HealthCheckConfiguration template) {
143            if (template != null) {
144                if (this.enabled == null) {
145                    this.enabled = template.enabled;
146                }
147                if (this.interval == null) {
148                    this.interval = template.interval;
149                }
150                if (this.failureThreshold == null) {
151                    this.failureThreshold = template.failureThreshold;
152                }
153            }
154
155            return this;
156        }
157
158        public Builder enabled(Boolean enabled) {
159            this.enabled = enabled;
160            return this;
161        }
162
163        public Builder interval(Duration interval) {
164            this.interval = interval;
165            return this;
166        }
167
168        public Builder interval(Long interval) {
169            return ObjectHelper.isNotEmpty(interval)
170                ? interval(Duration.ofMillis(interval))
171                : this;
172        }
173
174        public Builder interval(String interval) {
175            return ObjectHelper.isNotEmpty(interval)
176                ? interval(TimePatternConverter.toMilliSeconds(interval))
177                : this;
178        }
179
180        public Builder failureThreshold(Integer failureThreshold) {
181            this.failureThreshold = failureThreshold;
182            return this;
183        }
184
185        @Override
186        public HealthCheckConfiguration build() {
187            HealthCheckConfiguration conf = new HealthCheckConfiguration();
188            conf.setEnabled(ObjectHelper.supplyIfEmpty(enabled, HealthCheckConfiguration::defaultValueEnabled));
189            conf.setInterval(ObjectHelper.supplyIfEmpty(interval, HealthCheckConfiguration::defaultValueInterval));
190            conf.setFailureThreshold(ObjectHelper.supplyIfEmpty(failureThreshold, HealthCheckConfiguration::defaultValueFailureThreshold));
191
192            return conf;
193        }
194    }
195}