View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.resolver;
18  
19  import io.netty.util.concurrent.EventExecutor;
20  import io.netty.util.concurrent.Future;
21  import io.netty.util.concurrent.Promise;
22  import io.netty.util.internal.UnstableApi;
23  
24  import java.util.List;
25  
26  import static io.netty.util.internal.ObjectUtil.*;
27  
28  /**
29   * A skeletal {@link NameResolver} implementation.
30   */
31  @UnstableApi
32  public abstract class SimpleNameResolver<T> implements NameResolver<T> {
33  
34      private final EventExecutor executor;
35  
36      /**
37       * @param executor the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned
38       *                 by {@link #resolve(String)}
39       */
40      protected SimpleNameResolver(EventExecutor executor) {
41          this.executor = checkNotNull(executor, "executor");
42      }
43  
44      /**
45       * Returns the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned
46       * by {@link #resolve(String)}.
47       */
48      protected EventExecutor executor() {
49          return executor;
50      }
51  
52      @Override
53      public final Future<T> resolve(String inetHost) {
54          final Promise<T> promise = executor().newPromise();
55          return resolve(inetHost, promise);
56      }
57  
58      @Override
59      public Future<T> resolve(String inetHost, Promise<T> promise) {
60          checkNotNull(promise, "promise");
61  
62          try {
63              doResolve(inetHost, promise);
64              return promise;
65          } catch (Exception e) {
66              return promise.setFailure(e);
67          }
68      }
69  
70      @Override
71      public final Future<List<T>> resolveAll(String inetHost) {
72          final Promise<List<T>> promise = executor().newPromise();
73          return resolveAll(inetHost, promise);
74      }
75  
76      @Override
77      public Future<List<T>> resolveAll(String inetHost, Promise<List<T>> promise) {
78          checkNotNull(promise, "promise");
79  
80          try {
81              doResolveAll(inetHost, promise);
82              return promise;
83          } catch (Exception e) {
84              return promise.setFailure(e);
85          }
86      }
87  
88      /**
89       * Invoked by {@link #resolve(String)} to perform the actual name resolution.
90       */
91      protected abstract void doResolve(String inetHost, Promise<T> promise) throws Exception;
92  
93      /**
94       * Invoked by {@link #resolveAll(String)} to perform the actual name resolution.
95       */
96      protected abstract void doResolveAll(String inetHost, Promise<List<T>> promise) throws Exception;
97  
98      @Override
99      public void close() { }
100 }