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

io.helidon.webserver.observe.log.LogStreamConfig 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.time.Duration;
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.http.HttpMediaType;
import io.helidon.http.HttpMediaTypes;

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

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

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

    /**
     * Fluent API builder base for {@link LogStreamConfig}.
     *
     * @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 LogStreamConfig> implements Prototype.ConfiguredBuilder {

        private boolean enabled = true;
        private Config config;
        private Duration idleMessageTimeout = Duration.parse("PT5S");
        private HttpMediaType contentType = HttpMediaTypes.PLAINTEXT_UTF_8;
        private int queueSize = 100;
        private String idleString = "%\n";

        /**
         * Protected to support extensibility.
         */
        protected BuilderBase() {
        }

        /**
         * 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(LogStreamConfig prototype) {
            enabled(prototype.enabled());
            contentType(prototype.contentType());
            idleMessageTimeout(prototype.idleMessageTimeout());
            queueSize(prototype.queueSize());
            idleString(prototype.idleString());
            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(LogStreamConfig.BuilderBase builder) {
            enabled(builder.enabled());
            contentType(builder.contentType());
            idleMessageTimeout(builder.idleMessageTimeout());
            queueSize(builder.queueSize());
            idleString(builder.idleString());
            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;
            config.get("enabled").as(Boolean.class).ifPresent(this::enabled);
            config.get("content-type").map(LogStreamConfigBlueprint::createContentType).ifPresent(this::contentType);
            config.get("idle-message-timeout").as(Duration.class).ifPresent(this::idleMessageTimeout);
            config.get("queue-size").as(Integer.class).ifPresent(this::queueSize);
            config.get("idle-string").as(String.class).ifPresent(this::idleString);
            return self();
        }

        /**
         * Whether stream is enabled.
         *
         * @param enabled whether to allow streaming of log statements
         * @return updated builder instance
         * @see #enabled()
         */
        public BUILDER enabled(boolean enabled) {
            this.enabled = enabled;
            return self();
        }

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

        /**
         *
         *
         * @param contentTypeConfig
         * @return updated builder instance
         * @see #contentType()
         */
        public BUILDER contentType(Config contentTypeConfig) {
            Objects.requireNonNull(contentTypeConfig);
            this.contentType = LogStreamConfigBlueprint.createContentType(contentTypeConfig);
            return self();
        }

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

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

        /**
         * How long to wait before we send the idle message, to make sure we keep the stream alive.
         *
         * @param idleMessageTimeout if no messages appear within this duration, and idle message will be sent
         * @return updated builder instance
         * @see #idleString()
         * @see #idleMessageTimeout()
         */
        public BUILDER idleMessageTimeout(Duration idleMessageTimeout) {
            Objects.requireNonNull(idleMessageTimeout);
            this.idleMessageTimeout = idleMessageTimeout;
            return self();
        }

        /**
         * Length of the in-memory queue that buffers log messages from loggers before sending them over the network.
         * If the messages are produced faster than we can send them to client, excess messages are DISCARDED, and will not
         * be sent.
         *
         * @param queueSize size of the in-memory queue for log messages
         * @return updated builder instance
         * @see #queueSize()
         */
        public BUILDER queueSize(int queueSize) {
            this.queueSize = queueSize;
            return self();
        }

        /**
         * String sent when there are no log messages within the {@link #idleMessageTimeout()}.
         *
         * @param idleString string to write over the network when no log messages are received
         * @return updated builder instance
         * @see #idleString()
         */
        public BUILDER idleString(String idleString) {
            Objects.requireNonNull(idleString);
            this.idleString = idleString;
            return self();
        }

        /**
         * Whether stream is enabled.
         *
         * @return the enabled
         */
        public boolean enabled() {
            return enabled;
        }

        /**
         *
         *
         * @return the content type
         */
        public HttpMediaType contentType() {
            return contentType;
        }

        /**
         * How long to wait before we send the idle message, to make sure we keep the stream alive.
         *
         * @return the idle message timeout
         * @see #idleString()
         * @see #idleMessageTimeout()
         */
        public Duration idleMessageTimeout() {
            return idleMessageTimeout;
        }

        /**
         * Length of the in-memory queue that buffers log messages from loggers before sending them over the network.
         * If the messages are produced faster than we can send them to client, excess messages are DISCARDED, and will not
         * be sent.
         *
         * @return the queue size
         */
        public int queueSize() {
            return queueSize;
        }

        /**
         * String sent when there are no log messages within the {@link #idleMessageTimeout()}.
         *
         * @return the idle string
         */
        public String idleString() {
            return idleString;
        }

        /**
         * 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 "LogStreamConfigBuilder{"
                    + "enabled=" + enabled + ","
                    + "contentType=" + contentType + ","
                    + "idleMessageTimeout=" + idleMessageTimeout + ","
                    + "queueSize=" + queueSize + ","
                    + "idleString=" + idleString
                    + "}";
        }

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

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

        /**
         * Generated implementation of the prototype, can be extended by descendant prototype implementations.
         */
        protected static class LogStreamConfigImpl implements LogStreamConfig {

            private final boolean enabled;
            private final Duration idleMessageTimeout;
            private final HttpMediaType contentType;
            private final int queueSize;
            private final String idleString;

            /**
             * Create an instance providing a builder.
             *
             * @param builder extending builder base of this prototype
             */
            protected LogStreamConfigImpl(LogStreamConfig.BuilderBase builder) {
                this.enabled = builder.enabled();
                this.contentType = builder.contentType();
                this.idleMessageTimeout = builder.idleMessageTimeout();
                this.queueSize = builder.queueSize();
                this.idleString = builder.idleString();
            }

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

            @Override
            public HttpMediaType contentType() {
                return contentType;
            }

            @Override
            public Duration idleMessageTimeout() {
                return idleMessageTimeout;
            }

            @Override
            public int queueSize() {
                return queueSize;
            }

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

            @Override
            public String toString() {
                return "LogStreamConfig{"
                        + "enabled=" + enabled + ","
                        + "contentType=" + contentType + ","
                        + "idleMessageTimeout=" + idleMessageTimeout + ","
                        + "queueSize=" + queueSize + ","
                        + "idleString=" + idleString
                        + "}";
            }

            @Override
            public boolean equals(Object o) {
                if (o == this) {
                    return true;
                }
                if (!(o instanceof LogStreamConfig other)) {
                    return false;
                }
                return enabled == other.enabled()
                    && Objects.equals(contentType, other.contentType())
                    && Objects.equals(idleMessageTimeout, other.idleMessageTimeout())
                    && queueSize == other.queueSize()
                    && Objects.equals(idleString, other.idleString());
            }

            @Override
            public int hashCode() {
                return Objects.hash(enabled, contentType, idleMessageTimeout, queueSize, idleString);
            }

        }

    }

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

        private Builder() {
        }

        @Override
        public LogStreamConfig buildPrototype() {
            preBuildPrototype();
            validatePrototype();
            return new LogStreamConfigImpl(this);
        }

        @Override
        public LogStreamConfig build() {
            return buildPrototype();
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy