001 002package io.vrap.rmf.base.client; 003 004import java.text.MessageFormat; 005import java.util.ServiceLoader; 006import java.util.concurrent.ExecutorService; 007import java.util.function.Supplier; 008 009import io.vrap.rmf.base.client.error.BaseException; 010 011/** 012 * Interface to supply a HTTP client implementation specified by a {@link ServiceLoader} 013 */ 014public interface HttpClientSupplier extends Supplier<VrapHttpClient>, ExecutorHttpClientSupplier { 015 static Supplier<VrapHttpClient> of() { 016 final ServiceLoader<HttpClientSupplier> loader = ServiceLoader.load(HttpClientSupplier.class, 017 HttpClientSupplier.class.getClassLoader()); 018 HttpClientSupplier httpClientFactory = null; 019 try { 020 httpClientFactory = loader.iterator().next(); 021 } 022 catch (Throwable ignored) { 023 } 024 025 if (httpClientFactory == null) { 026 final String s = MessageFormat.format( 027 "No {0} found. A dependency providing a compatible HTTP client may be missing e.g. commercetools-http-client.", 028 HttpClientSupplier.class.getCanonicalName()); 029 throw new BaseException(new NoClassDefFoundError(s)); 030 } 031 return httpClientFactory; 032 } 033 034 static Supplier<VrapHttpClient> of(ExecutorService executorService) { 035 final ServiceLoader<ExecutorHttpClientSupplier> loader = ServiceLoader.load(ExecutorHttpClientSupplier.class, 036 ExecutorHttpClientSupplier.class.getClassLoader()); 037 ExecutorHttpClientSupplier httpClientFactory = null; 038 try { 039 httpClientFactory = loader.iterator().next(); 040 } 041 catch (Throwable ignored) { 042 } 043 044 if (httpClientFactory == null) { 045 final String s = MessageFormat.format( 046 "No {0} found. A dependency providing a compatible HTTP client may be missing e.g. commercetools-http-client.", 047 HttpClientSupplier.class.getCanonicalName()); 048 throw new BaseException(new NoClassDefFoundError(s)); 049 } 050 return httpClientFactory.get(executorService); 051 052 } 053}