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

org.tbk.spring.testcontainer.bitcoind.config.BitcoindContainerZeroMqClientAutoConfiguration Maven / Gradle / Ivy

The newest version!
package org.tbk.spring.testcontainer.bitcoind.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.tbk.bitcoin.zeromq.config.BitcoinZeroMqClientAutoConfiguration;
import org.tbk.bitcoin.zeromq.config.BitcoinZmqClientConfigBuilderCustomizer;
import org.tbk.spring.testcontainer.bitcoind.BitcoindContainer;

import java.util.Optional;
import java.util.function.Function;

@Slf4j
@AutoConfiguration
@ConditionalOnClass(BitcoinZmqClientConfigBuilderCustomizer.class)
@AutoConfigureAfter(BitcoindContainerAutoConfiguration.class)
@AutoConfigureBefore(BitcoinZeroMqClientAutoConfiguration.class)
public class BitcoindContainerZeroMqClientAutoConfiguration {

    /**
     * Overwrite the default ports of the zeromq config as the mapping to the container
     * can only be determined during runtime.
     */
    @Bean
    @ConditionalOnBean(BitcoindContainer.class)
    BitcoinZmqClientConfigBuilderCustomizer bitcoinZmqClientConfigBuilderCustomizer(BitcoindContainer bitcoinContainer) {

        Function> replacePortInUrl = url -> {
            Optional specifiedListeningPort = Optional.ofNullable(url)
                    .map(val -> val.substring(val.indexOf("//")))
                    .map(val -> val.substring(val.indexOf(":") + 1))
                    .map(val -> Integer.parseInt(val, 10));

            return specifiedListeningPort.map(port -> {
                String host = bitcoinContainer.getHost();
                Integer mappedPort = bitcoinContainer.getMappedPort(port);
                return "tcp://" + host + ":" + mappedPort;
            });
        };

        return config -> {
            Optional pubRawBlockUrl = replacePortInUrl.apply(config.getZmqpubrawblock());
            Optional pubHashBlockUrl = replacePortInUrl.apply(config.getZmqpubhashblock());
            Optional pubRawTxUrl = replacePortInUrl.apply(config.getZmqpubrawtx());
            Optional pubHashTxUrl = replacePortInUrl.apply(config.getZmqpubhashtx());

            pubRawBlockUrl.ifPresent(config::zmqpubrawblock);
            pubHashBlockUrl.ifPresent(config::zmqpubhashblock);
            pubRawTxUrl.ifPresent(config::zmqpubrawtx);
            pubHashTxUrl.ifPresent(config::zmqpubhashtx);
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy