io.vrap.rmf.base.client.HttpClientSupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rmf-java-base Show documentation
Show all versions of rmf-java-base Show documentation
The e-commerce SDK from commercetools Composable Commerce for Java
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);
}
}