All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helidon.webserver.observe.health.HealthObserverConfig Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
/*
 * Copyright (c) 2024 Oracle and/or its affiliates.
 *
 * 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 io.helidon.webserver.observe.health;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

import io.helidon.builder.api.Prototype;
import io.helidon.common.Generated;
import io.helidon.common.config.Config;
import io.helidon.health.HealthCheck;
import io.helidon.health.HealthCheckResponse;
import io.helidon.health.HealthCheckType;
import io.helidon.webserver.observe.ObserverConfigBase;

/**
 * Configuration of Health observer.
 *
 * @see io.helidon.webserver.observe.health.HealthObserver#create(HealthObserverConfig)
 * @see io.helidon.webserver.observe.health.HealthObserver#builder()
 * @see #builder()
 * @see #create()
 */
@Generated(value = "io.helidon.builder.codegen.BuilderCodegen", trigger = "io.helidon.webserver.observe.health.HealthObserverConfigBlueprint")
public interface HealthObserverConfig extends HealthObserverConfigBlueprint, Prototype.Api, ObserverConfigBase {

    /**
     * Create a new fluent API builder to customize configuration.
     *
     * @return a new builder
     */
    static HealthObserverConfig.Builder builder() {
        return new HealthObserverConfig.Builder();
    }

    /**
     * Create a new fluent API builder from an existing instance.
     *
     * @param instance an existing instance used as a base for the builder
     * @return a builder based on an instance
     */
    static HealthObserverConfig.Builder builder(HealthObserverConfig instance) {
        return HealthObserverConfig.builder().from(instance);
    }

    /**
     * Create a new instance from configuration.
     *
     * @param config used to configure the new instance
     * @return a new instance configured from configuration
     */
    static HealthObserverConfig create(Config config) {
        return HealthObserverConfig.builder().config(config).buildPrototype();
    }

    /**
     * Create a new instance with default values.
     *
     * @return a new instance
     */
    static HealthObserverConfig create() {
        return HealthObserverConfig.builder().buildPrototype();
    }

    /**
     * Fluent API builder base for {@link HealthObserver}.
     *
     * @param  type of the builder extending this abstract builder
     * @param  type of the prototype interface that would be built by {@link #buildPrototype()}
     */
    abstract class BuilderBase, PROTOTYPE extends HealthObserverConfig> extends ObserverConfigBase.BuilderBase implements Prototype.ConfiguredBuilder {

        private final List healthChecks = new ArrayList<>();
        private boolean details = false;
        private boolean isHealthChecksMutated;
        private boolean useSystemServices = true;
        private Config config;
        private String endpoint = "health";

        /**
         * Protected to support extensibility.
         */
        protected BuilderBase() {
            name("health");
        }

        /**
         * Update this builder from an existing prototype instance. This method disables automatic service discovery.
         *
         * @param prototype existing prototype to update this builder from
         * @return updated builder instance
         */
        public BUILDER from(HealthObserverConfig prototype) {
            super.from(prototype);
            endpoint(prototype.endpoint());
            details(prototype.details());
            if (!isHealthChecksMutated) {
                healthChecks.clear();
            }
            addHealthChecks(prototype.healthChecks());
            useSystemServices(prototype.useSystemServices());
            this.config = prototype.config().orElse(null);
            return self();
        }

        /**
         * Update this builder from an existing prototype builder instance.
         *
         * @param builder existing builder prototype to update this builder from
         * @return updated builder instance
         */
        public BUILDER from(HealthObserverConfig.BuilderBase builder) {
            super.from(builder);
            endpoint(builder.endpoint());
            details(builder.details());
            if (isHealthChecksMutated) {
                if (builder.isHealthChecksMutated) {
                    addHealthChecks(builder.healthChecks);
                }
            } else {
                healthChecks.clear();
                addHealthChecks(builder.healthChecks);
            }
            useSystemServices(builder.useSystemServices());
            builder.config().ifPresent(this::config);
            return self();
        }

        /**
         * Add the provided health check using an explicit type (may differ from the
         * {@link io.helidon.health.HealthCheck#type()}.
         *
         * @param check   health check to add
         * @param type    type to use
         * @return updated builder instance
         */
        public BUILDER addCheck(HealthCheck check, HealthCheckType type) {
            HealthObserverSupport.CustomMethods.addCheck(this, check, type);
            return self();
        }

        /**
         * Add a health check using the provided response supplier, type, and name.
         *
         * @param responseSupplier supplier of the health check response
         * @param type             type to use
         * @param name             name to use for the health check
         * @return updated builder instance
         */
        public BUILDER addCheck(Supplier responseSupplier, HealthCheckType type, String name) {
            HealthObserverSupport.CustomMethods.addCheck(this, responseSupplier, type, name);
            return self();
        }

        /**
         * Add the provided health checks.
         *
         * @param checks  health checks to add
         * @return updated builder instance
         */
        public BUILDER addChecks(HealthCheck[] checks) {
            HealthObserverSupport.CustomMethods.addChecks(this, checks);
            return self();
        }

        /**
         * Update builder from configuration (node of this type).
         * If a value is present in configuration, it would override currently configured values.
         *
         * @param config configuration instance used to obtain values to update this builder
         * @return updated builder instance
         */
        @Override
        public BUILDER config(Config config) {
            Objects.requireNonNull(config);
            this.config = config;
            super.config(config);
            config.get("endpoint").as(String.class).ifPresent(this::endpoint);
            config.get("details").as(Boolean.class).ifPresent(this::details);
            config.get("use-system-services").as(Boolean.class).ifPresent(this::useSystemServices);
            return self();
        }

        /**
         *
         *
         * @param endpoint
         * @return updated builder instance
         * @see #endpoint()
         */
        public BUILDER endpoint(String endpoint) {
            Objects.requireNonNull(endpoint);
            this.endpoint = endpoint;
            return self();
        }

        /**
         * Whether details should be printed.
         * By default, health only returns a {@link io.helidon.http.Status#NO_CONTENT_204} for success,
         * {@link io.helidon.http.Status#SERVICE_UNAVAILABLE_503} for health down,
         * and {@link io.helidon.http.Status#INTERNAL_SERVER_ERROR_500} in case of error with no entity.
         * When details are enabled, health returns {@link io.helidon.http.Status#OK_200} for success, same codes
         * otherwise
         * and a JSON entity with detailed information about each health check executed.
         *
         * @param details set to {@code true} to enable details
         * @return updated builder instance
         * @see #details()
         */
        public BUILDER details(boolean details) {
            this.details = details;
            return self();
        }

        /**
         * Health checks with implicit types.
         *
         * @param healthChecks health checks to register with the observer
         * @return updated builder instance
         * @see #healthChecks()
         */
        public BUILDER healthChecks(List healthChecks) {
            Objects.requireNonNull(healthChecks);
            isHealthChecksMutated = true;
            this.healthChecks.clear();
            this.healthChecks.addAll(healthChecks);
            return self();
        }

        /**
         * Health checks with implicit types.
         *
         * @param healthChecks health checks to register with the observer
         * @return updated builder instance
         * @see #healthChecks()
         */
        public BUILDER addHealthChecks(List healthChecks) {
            Objects.requireNonNull(healthChecks);
            isHealthChecksMutated = true;
            this.healthChecks.addAll(healthChecks);
            return self();
        }

        /**
         * Health checks with implicit types.
         *
         * @param check health checks to register with the observer
         * @return updated builder instance
         * @see #healthChecks()
         */
        public BUILDER addCheck(HealthCheck check) {
            Objects.requireNonNull(check);
            this.healthChecks.add(check);
            isHealthChecksMutated = true;
            return self();
        }

        /**
         * Whether to use services discovered by {@link java.util.ServiceLoader}.
         * By default, all {@link io.helidon.health.spi.HealthCheckProvider} based health checks are added.
         *
         * @param useSystemServices set to {@code false} to disable discovery
         * @return updated builder instance
         * @see #useSystemServices()
         */
        public BUILDER useSystemServices(boolean useSystemServices) {
            this.useSystemServices = useSystemServices;
            return self();
        }

        /**
         *
         *
         * @return the endpoint
         */
        public String endpoint() {
            return endpoint;
        }

        /**
         * Whether details should be printed.
         * By default, health only returns a {@link io.helidon.http.Status#NO_CONTENT_204} for success,
         * {@link io.helidon.http.Status#SERVICE_UNAVAILABLE_503} for health down,
         * and {@link io.helidon.http.Status#INTERNAL_SERVER_ERROR_500} in case of error with no entity.
         * When details are enabled, health returns {@link io.helidon.http.Status#OK_200} for success, same codes
         * otherwise
         * and a JSON entity with detailed information about each health check executed.
         *
         * @return the details
         */
        public boolean details() {
            return details;
        }

        /**
         * Health checks with implicit types.
         *
         * @return the health checks
         */
        public List healthChecks() {
            return healthChecks;
        }

        /**
         * Whether to use services discovered by {@link java.util.ServiceLoader}.
         * By default, all {@link io.helidon.health.spi.HealthCheckProvider} based health checks are added.
         *
         * @return the use system services
         */
        public boolean useSystemServices() {
            return useSystemServices;
        }

        /**
         * If this instance was configured, this would be the config instance used.
         *
         * @return config node used to configure this builder, or empty if not configured
         */
        public Optional config() {
            return Optional.ofNullable(config);
        }

        @Override
        public String toString() {
            return "HealthObserverConfigBuilder{"
                    + "endpoint=" + endpoint + ","
                    + "details=" + details + ","
                    + "healthChecks=" + healthChecks + ","
                    + "useSystemServices=" + useSystemServices + ","
                    + "config=" + config
                    + "};"
                    + super.toString();
        }

        /**
         * Handles providers and decorators.
         */
        protected void preBuildPrototype() {
            super.preBuildPrototype();
        }

        /**
         * Validates required properties.
         */
        protected void validatePrototype() {
            super.validatePrototype();
        }

        /**
         * Generated implementation of the prototype, can be extended by descendant prototype implementations.
         */
        protected static class HealthObserverConfigImpl extends ObserverConfigBaseImpl implements HealthObserverConfig, Supplier {

            private final boolean details;
            private final boolean useSystemServices;
            private final List healthChecks;
            private final Optional config;
            private final String endpoint;

            /**
             * Create an instance providing a builder.
             *
             * @param builder extending builder base of this prototype
             */
            protected HealthObserverConfigImpl(HealthObserverConfig.BuilderBase builder) {
                super(builder);
                this.endpoint = builder.endpoint();
                this.details = builder.details();
                this.healthChecks = List.copyOf(builder.healthChecks());
                this.useSystemServices = builder.useSystemServices();
                this.config = builder.config();
            }

            @Override
            public HealthObserver build() {
                return HealthObserver.create(this);
            }

            @Override
            public HealthObserver get() {
                return build();
            }

            @Override
            public String endpoint() {
                return endpoint;
            }

            @Override
            public boolean details() {
                return details;
            }

            @Override
            public List healthChecks() {
                return healthChecks;
            }

            @Override
            public boolean useSystemServices() {
                return useSystemServices;
            }

            @Override
            public Optional config() {
                return config;
            }

            @Override
            public String toString() {
                return "HealthObserverConfig{"
                        + "endpoint=" + endpoint + ","
                        + "details=" + details + ","
                        + "healthChecks=" + healthChecks + ","
                        + "useSystemServices=" + useSystemServices + ","
                        + "config=" + config
                        + "};"
                        + super.toString();
            }

            @Override
            public boolean equals(Object o) {
                if (o == this) {
                    return true;
                }
                if (!(o instanceof HealthObserverConfig other)) {
                    return false;
                }
                return super.equals(other)
                            && Objects.equals(endpoint, other.endpoint())
                    && details == other.details()
                    && Objects.equals(healthChecks, other.healthChecks())
                    && useSystemServices == other.useSystemServices()
                    && Objects.equals(config, other.config());
            }

            @Override
            public int hashCode() {
                return 31 * super.hashCode() + Objects.hash(endpoint, details, healthChecks, useSystemServices, config);
            }

        }

    }

    /**
     * Fluent API builder for {@link HealthObserver}.
     */
    class Builder extends HealthObserverConfig.BuilderBase implements io.helidon.common.Builder {

        private Builder() {
        }

        @Override
        public HealthObserverConfig buildPrototype() {
            preBuildPrototype();
            validatePrototype();
            return new HealthObserverConfigImpl(this);
        }

        @Override
        public HealthObserver build() {
            return HealthObserver.create(this.buildPrototype());
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy