1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver;
17
18 import io.netty.util.concurrent.EventExecutor;
19 import io.netty.util.concurrent.Future;
20 import io.netty.util.concurrent.FutureListener;
21 import io.netty.util.concurrent.Promise;
22 import io.netty.util.internal.ObjectUtil;
23
24 import java.util.Arrays;
25 import java.util.List;
26
27 import static io.netty.util.internal.ObjectUtil.checkNotNull;
28
29
30
31
32
33
34 public final class CompositeNameResolver<T> extends SimpleNameResolver<T> {
35
36 private final NameResolver<T>[] resolvers;
37
38
39
40
41
42
43 public CompositeNameResolver(EventExecutor executor, NameResolver<T>... resolvers) {
44 super(executor);
45 checkNotNull(resolvers, "resolvers");
46 for (int i = 0; i < resolvers.length; i++) {
47 ObjectUtil.checkNotNull(resolvers[i], "resolvers[" + i + ']');
48 }
49 if (resolvers.length < 2) {
50 throw new IllegalArgumentException("resolvers: " + Arrays.asList(resolvers) +
51 " (expected: at least 2 resolvers)");
52 }
53 this.resolvers = resolvers.clone();
54 }
55
56 @Override
57 protected void doResolve(String inetHost, Promise<T> promise) throws Exception {
58 doResolveRec(inetHost, promise, 0, null);
59 }
60
61 private void doResolveRec(final String inetHost,
62 final Promise<T> promise,
63 final int resolverIndex,
64 Throwable lastFailure) throws Exception {
65 if (resolverIndex >= resolvers.length) {
66 promise.setFailure(lastFailure);
67 } else {
68 NameResolver<T> resolver = resolvers[resolverIndex];
69 resolver.resolve(inetHost).addListener(new FutureListener<T>() {
70 @Override
71 public void operationComplete(Future<T> future) throws Exception {
72 if (future.isSuccess()) {
73 promise.setSuccess(future.getNow());
74 } else {
75 doResolveRec(inetHost, promise, resolverIndex + 1, future.cause());
76 }
77 }
78 });
79 }
80 }
81
82 @Override
83 protected void doResolveAll(String inetHost, Promise<List<T>> promise) throws Exception {
84 doResolveAllRec(inetHost, promise, 0, null);
85 }
86
87 private void doResolveAllRec(final String inetHost,
88 final Promise<List<T>> promise,
89 final int resolverIndex,
90 Throwable lastFailure) throws Exception {
91 if (resolverIndex >= resolvers.length) {
92 promise.setFailure(lastFailure);
93 } else {
94 NameResolver<T> resolver = resolvers[resolverIndex];
95 resolver.resolveAll(inetHost).addListener(new FutureListener<List<T>>() {
96 @Override
97 public void operationComplete(Future<List<T>> future) throws Exception {
98 if (future.isSuccess()) {
99 promise.setSuccess(future.getNow());
100 } else {
101 doResolveAllRec(inetHost, promise, resolverIndex + 1, future.cause());
102 }
103 }
104 });
105 }
106 }
107 }