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

io.helidon.webserver.observe.log.LogObserverConfig Maven / Gradle / Ivy

The 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.log;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
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.webserver.observe.ObserverConfigBase;

/**
 * Log Observer configuration.
 *
 * @see #builder()
 * @see #create()
 */
@Generated(value = "io.helidon.builder.codegen.BuilderCodegen", trigger = "io.helidon.webserver.observe.log.LogObserverConfigBlueprint")
public interface LogObserverConfig extends LogObserverConfigBlueprint, Prototype.Api, ObserverConfigBase {

    /**
     * Create a new fluent API builder to customize configuration.
     *
     * @return a new builder
     */
    static LogObserverConfig.Builder builder() {
        return new LogObserverConfig.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 LogObserverConfig.Builder builder(LogObserverConfig instance) {
        return LogObserverConfig.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 LogObserverConfig create(Config config) {
        return LogObserverConfig.builder().config(config).buildPrototype();
    }

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

    /**
     * Fluent API builder base for {@link LogObserver}.
     *
     * @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 LogObserverConfig> extends ObserverConfigBase.BuilderBase implements Prototype.ConfiguredBuilder {

        private boolean permitAll;
        private Config config;
        private LogStreamConfig stream = LogStreamConfig.create();
        private String endpoint = "log";

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

        /**
         * 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(LogObserverConfig prototype) {
            super.from(prototype);
            endpoint(prototype.endpoint());
            permitAll(prototype.permitAll());
            stream(prototype.stream());
            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(LogObserverConfig.BuilderBase builder) {
            super.from(builder);
            endpoint(builder.endpoint());
            permitAll(builder.permitAll());
            stream(builder.stream());
            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("permit-all").as(Boolean.class).ifPresent(this::permitAll);
            config.get("stream").map(LogStreamConfig::create).ifPresent(this::stream);
            return self();
        }

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

        /**
         * Permit all access, even when not authorized.
         *
         * @param permitAll whether to permit access for anybody
         * @return updated builder instance
         * @see #permitAll()
         */
        public BUILDER permitAll(boolean permitAll) {
            this.permitAll = permitAll;
            return self();
        }

        /**
         * Configuration of log stream.
         *
         * @param stream log stream configuration
         * @return updated builder instance
         * @see #stream()
         */
        public BUILDER stream(LogStreamConfig stream) {
            Objects.requireNonNull(stream);
            this.stream = stream;
            return self();
        }

        /**
         * Configuration of log stream.
         *
         * @param consumer consumer of builder for
         *                 log stream configuration
         * @return updated builder instance
         * @see #stream()
         */
        public BUILDER stream(Consumer consumer) {
            Objects.requireNonNull(consumer);
            var builder = LogStreamConfig.builder();
            consumer.accept(builder);
            this.stream(builder.build());
            return self();
        }

        /**
         * Configuration of log stream.
         *
         * @param supplier supplier of
         *                 log stream configuration
         * @return updated builder instance
         * @see #stream()
         */
        public BUILDER stream(Supplier supplier) {
            Objects.requireNonNull(supplier);
            this.stream(supplier.get());
            return self();
        }

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

        /**
         * Permit all access, even when not authorized.
         *
         * @return the permit all
         */
        public boolean permitAll() {
            return permitAll;
        }

        /**
         * Configuration of log stream.
         *
         * @return the stream
         */
        public LogStreamConfig stream() {
            return stream;
        }

        /**
         * 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 "LogObserverConfigBuilder{"
                    + "endpoint=" + endpoint + ","
                    + "permitAll=" + permitAll + ","
                    + "stream=" + stream
                    + "};"
                    + 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 LogObserverConfigImpl extends ObserverConfigBaseImpl implements LogObserverConfig, Supplier {

            private final boolean permitAll;
            private final LogStreamConfig stream;
            private final String endpoint;

            /**
             * Create an instance providing a builder.
             *
             * @param builder extending builder base of this prototype
             */
            protected LogObserverConfigImpl(LogObserverConfig.BuilderBase builder) {
                super(builder);
                this.endpoint = builder.endpoint();
                this.permitAll = builder.permitAll();
                this.stream = builder.stream();
            }

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

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

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

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

            @Override
            public LogStreamConfig stream() {
                return stream;
            }

            @Override
            public String toString() {
                return "LogObserverConfig{"
                        + "endpoint=" + endpoint + ","
                        + "permitAll=" + permitAll + ","
                        + "stream=" + stream
                        + "};"
                        + super.toString();
            }

            @Override
            public boolean equals(Object o) {
                if (o == this) {
                    return true;
                }
                if (!(o instanceof LogObserverConfig other)) {
                    return false;
                }
                return super.equals(other)
                            && Objects.equals(endpoint, other.endpoint())
                    && permitAll == other.permitAll()
                    && Objects.equals(stream, other.stream());
            }

            @Override
            public int hashCode() {
                return 31 * super.hashCode() + Objects.hash(endpoint, permitAll, stream);
            }

        }

    }

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

        private Builder() {
        }

        @Override
        public LogObserverConfig buildPrototype() {
            preBuildPrototype();
            validatePrototype();
            return new LogObserverConfigImpl(this);
        }

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

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy