aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client-api
diff options
context:
space:
mode:
authorMorten Tokle <mortent@verizonmedia.com>2022-01-04 15:23:46 +0100
committerMorten Tokle <mortent@verizonmedia.com>2022-01-04 15:23:46 +0100
commit0b0233583f656e2e5b02bc7fd0fbf69a7b2a2e3c (patch)
tree4498c22c02416a211a638aa3289f271be0553ed8 /vespa-feed-client-api
parent95494e40aaf1bfbccca89c0b45b7dfbc7f1b8387 (diff)
Supply default implementation
Diffstat (limited to 'vespa-feed-client-api')
-rw-r--r--vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java5
-rw-r--r--vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java48
2 files changed, 27 insertions, 26 deletions
diff --git a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java
index 94d5a0ba2d1..95c9b2c95fe 100644
--- a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java
+++ b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java
@@ -27,11 +27,12 @@ public interface FeedClientBuilder {
/** Creates a builder for multiple container endpoints **/
static FeedClientBuilder create(List<URI> endpoints) {
- return Helper.getFeedClientBuilder().setEndpointUris(endpoints);
+ return Helper.getFeedClientBuilderSupplier().get().setEndpointUris(endpoints);
}
+ /** Override FeedClientBuilder. This will be preferred in {@link #create} */
static void setFeedClientBuilderSupplier(Supplier<FeedClientBuilder> supplier) {
- Helper.setFeedClientBuilderReference(supplier);
+ Helper.setFeedClientBuilderSupplier(supplier);
}
/**
* Sets the number of connections this client will use per endpoint.
diff --git a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java
index edf9457aaa1..6971b2ea8f5 100644
--- a/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java
+++ b/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/Helper.java
@@ -21,38 +21,38 @@ import static ai.vespa.feed.client.FeedClientBuilder.PREFERRED_IMPLEMENTATION_PR
*/
class Helper {
- static final AtomicReference<Supplier<FeedClientBuilder>> feedClientBuilderSupplier = new AtomicReference<>();
+ private static final AtomicReference<Supplier<FeedClientBuilder>> feedClientBuilderSupplier = new AtomicReference<>(Helper::getFeedClientBuilder);
- static final void setFeedClientBuilderReference(Supplier<FeedClientBuilder> supplier) {
+ static final void setFeedClientBuilderSupplier(Supplier<FeedClientBuilder> supplier) {
feedClientBuilderSupplier.set(supplier);
}
+ static Supplier<FeedClientBuilder> getFeedClientBuilderSupplier() {
+ return feedClientBuilderSupplier.get();
+ }
+
static FeedClientBuilder getFeedClientBuilder() {
- if (Helper.feedClientBuilderSupplier.get()!=null) {
- return Helper.feedClientBuilderSupplier.get().get();
+ String defaultImplementation = "ai.vespa.feed.client.impl.FeedClientBuilderImpl";
+ String preferredImplementation = System.getProperty(PREFERRED_IMPLEMENTATION_PROPERTY, defaultImplementation);
+ Iterator<FeedClientBuilder> iterator = ServiceLoader.load(FeedClientBuilder.class).iterator();
+ if (iterator.hasNext()) {
+ List<FeedClientBuilder> builders = new ArrayList<>();
+ iterator.forEachRemaining(builders::add);
+ return builders.stream()
+ .filter(builder -> preferredImplementation.equals(builder.getClass().getName()))
+ .findFirst()
+ .orElse(builders.get(0));
} else {
- String defaultImplementation = "ai.vespa.feed.client.impl.FeedClientBuilderImpl";
- String preferredImplementation = System.getProperty(PREFERRED_IMPLEMENTATION_PROPERTY, defaultImplementation);
- Iterator<FeedClientBuilder> iterator = ServiceLoader.load(FeedClientBuilder.class).iterator();
- if (iterator.hasNext()) {
- List<FeedClientBuilder> builders = new ArrayList<>();
- iterator.forEachRemaining(builders::add);
- return builders.stream()
- .filter(builder -> preferredImplementation.equals(builder.getClass().getName()))
- .findFirst()
- .orElse(builders.get(0));
- } else {
- try {
- Class<?> aClass = Class.forName(preferredImplementation);
- for (Constructor<?> constructor : aClass.getConstructors()) {
- if (constructor.getParameterTypes().length == 0) {
- return ((FeedClientBuilder) constructor.newInstance());
- }
+ try {
+ Class<?> aClass = Class.forName(preferredImplementation);
+ for (Constructor<?> constructor : aClass.getConstructors()) {
+ if (constructor.getParameterTypes().length == 0) {
+ return ((FeedClientBuilder) constructor.newInstance());
}
- throw new RuntimeException("Could not find Feed client builder implementation");
- } catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
- throw new RuntimeException(e);
}
+ throw new RuntimeException("Could not find Feed client builder implementation");
+ } catch (ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
+ throw new RuntimeException(e);
}
}
}