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

io.vrap.rmf.base.client.HttpClientSupplier Maven / Gradle / Ivy

There is a newer version: 17.17.0
Show newest version

package io.vrap.rmf.base.client;

import java.text.MessageFormat;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

import io.vrap.rmf.base.client.error.BaseException;

/**
 * Interface to supply a HTTP client implementation specified by a {@link ServiceLoader}
 */
public interface HttpClientSupplier extends Supplier, ExecutorHttpClientSupplier {
    static Supplier of() {
        final ServiceLoader loader = ServiceLoader.load(HttpClientSupplier.class,
            HttpClientSupplier.class.getClassLoader());
        HttpClientSupplier httpClientFactory = null;
        try {
            httpClientFactory = loader.iterator().next();
        }
        catch (Throwable ignored) {
        }

        if (httpClientFactory == null) {
            final String s = MessageFormat.format(
                "No {0} found. A dependency providing a compatible HTTP client may be missing e.g. commercetools-http-client.",
                HttpClientSupplier.class.getCanonicalName());
            throw new BaseException(new NoClassDefFoundError(s));
        }
        return httpClientFactory;
    }

    static Supplier of(ExecutorService executorService) {
        final ServiceLoader loader = ServiceLoader.load(ExecutorHttpClientSupplier.class,
            ExecutorHttpClientSupplier.class.getClassLoader());
        ExecutorHttpClientSupplier httpClientFactory = null;
        try {
            httpClientFactory = loader.iterator().next();
        }
        catch (Throwable ignored) {
        }

        if (httpClientFactory == null) {
            final String s = MessageFormat.format(
                "No {0} found. A dependency providing a compatible HTTP client may be missing e.g. commercetools-http-client.",
                HttpClientSupplier.class.getCanonicalName());
            throw new BaseException(new NoClassDefFoundError(s));
        }
        return httpClientFactory.get(executorService);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy