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

com.gs.api.accelrx.AccelRxApplicationBuilder Maven / Gradle / Ivy

The newest version!
package com.gs.api.accelrx;

import com.google.common.collect.Sets;
import com.gs.api.accelrx.context.log4j.AccelRxMDCAdapter;
import com.gs.api.accelrx.crypto.AccelRxCryptoConfig;
import com.gs.api.accelrx.crypto.AccelRxCryptoImpl;
import com.gs.api.accelrx.encoding.AccelRxEncodingConfig;
import com.gs.api.accelrx.encoding.AccelRxEncodingImpl;
import com.gs.api.accelrx.monitor.AccelRxMonitorConfig;
import com.gs.api.accelrx.monitor.AccelRxMonitorImpl;
import com.gs.api.accelrx.monitor.prometheus.AccelRxMeterRegistry;
import io.reactivex.rxjava3.core.Single;
import io.vertx.core.VertxOptions;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MicrometerMetricsOptions;
import io.vertx.rxjava3.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class AccelRxApplicationBuilder {
    private static final Logger log = LoggerFactory.getLogger(AccelRxApplicationBuilder.class);

    private final Vertx vertx;

    private Single monitorConfig;
    private Single encodingConfig;
    private Single cryptoConfig;
    private Map applicationMetadata;

    private volatile Single monitor;
    private volatile Single encoding;
    private volatile Single crypto;

    private AccelRxApplicationBuilder(Vertx vertx) {
        this.vertx = vertx;
    }

    public static AccelRxApplicationBuilder create() {
        return create(createVertx());
    }

    public static AccelRxApplicationBuilder create(Vertx vertx) {
        AccelRxMDCAdapter.install();
        log.info("\n" +
                "                    _             \n" +
                "                   | |            \n" +
                "  ____  ___ ___ __ | | __ ____  __\n" +
                " / _` |/ __/ __/ _ \\ | | '__\\ \\/ /\n" +
                "| (_| | (_| (_|  __/ |_| |   >  < \n" +
                " \\__,_|\\___\\___\\___|_(_)_|  /_/\\_\\\n");
        return new AccelRxApplicationBuilder(vertx);
    }

    public AccelRxApplicationBuilder withMonitorConfig(AccelRxMonitorConfig monitorConfig) {
        return withMonitorConfig(Single.just(monitorConfig));
    }

    public AccelRxApplicationBuilder withMonitorConfig(Single monitorConfig) {
        this.monitorConfig = monitorConfig.cache();
        return this;
    }

    public AccelRxApplicationBuilder withEncodingConfig(AccelRxEncodingConfig encodingConfig) {
        return withEncodingConfig(Single.just(encodingConfig));
    }

    public AccelRxApplicationBuilder withEncodingConfig(Single encodingConfig) {
        this.encodingConfig = encodingConfig.cache();
        return this;
    }

    public AccelRxApplicationBuilder withCryptoConfig(AccelRxCryptoConfig cryptoConfig) {
        return withCryptoConfig(Single.just(cryptoConfig));
    }

    public AccelRxApplicationBuilder withCryptoConfig(Single cryptoConfig) {
        this.cryptoConfig = cryptoConfig.cache();
        return this;
    }

    public AccelRxApplicationBuilder withApplicationMetadata(Map applicationMetadata) {
        this.applicationMetadata = applicationMetadata;
        return this;
    }

    @Deprecated
    public Vertx vertx() {
        return vertx;
    }

    public Single application() {
        return Single.zip(monitor(), encoding(), crypto(),
                (monitor, encoding, crypto) ->
                        AccelRxApplicationImpl.create(
                                vertx,
                                monitor,
                                encoding,
                                crypto));
    }

    public AccelRxApplication blockingApplication() {
        return application().blockingGet();
    }

    private Single monitor() {
        if (monitor == null) {
            synchronized (this) {
                if (monitor == null) {
                    if (monitorConfig == null) {
                        monitorConfig = Single.just(AccelRxMonitorConfig.builder().build());
                    }
                    monitor = monitorConfig
                            .map(config -> (AccelRxMonitor) AccelRxMonitorImpl.create(vertx, AccelRxMeterRegistry.lookup(), applicationMetadata, config))
                            .flatMap(monitor -> monitor.start().toSingleDefault(monitor))
                            .cache();
                }
            }
        }
        return monitor;
    }

    private Single encoding() {
        if (encoding == null) {
            synchronized (this) {
                if (encoding == null) {
                    if (encodingConfig == null) {
                        encodingConfig = Single.just(AccelRxEncodingConfig.builder().build());
                    }
                    encoding = encodingConfig
                            .map(config -> (AccelRxEncoding) AccelRxEncodingImpl.create(vertx, config))
                            .flatMap(encoding -> monitor()
                                    .map(monitor -> monitor.addConfig("encoding", encoding.jsonConfig()))
                                    .map(monitor -> encoding))
                            .cache();
                }
            }
        }
        return encoding;
    }

    private Single crypto() {
        if (crypto == null) {
            synchronized (this) {
                if (crypto == null) {
                    if (cryptoConfig == null) {
                        cryptoConfig = Single.just(AccelRxCryptoConfig.builder().build());
                    }
                    crypto = cryptoConfig
                            .map(config -> (AccelRxCrypto) AccelRxCryptoImpl.create(vertx, config))
                            .flatMap(crypto -> monitor()
                                    .map(monitor -> monitor.addConfig("crypto", crypto.jsonConfig()))
                                    .map(monitor -> crypto))
                            .cache();
                }
            }
        }
        return crypto;
    }

    protected static Vertx createVertx() {
        VertxOptions vertxOptions = new VertxOptions();
        vertxOptions.setMetricsOptions(new MicrometerMetricsOptions()
                .setEnabled(true)
                .setMicrometerRegistry(AccelRxMeterRegistry.create())
                .setLabels(Sets.newHashSet(Label.HTTP_PATH, Label.HTTP_METHOD, Label.HTTP_CODE, Label.REMOTE))
                .setFactory(new io.vertx.micrometer.impl.VertxMetricsFactoryImpl()));
        return Vertx.vertx(vertxOptions);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy