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
23 import java.util.List;
24
25 import static io.netty.util.internal.ObjectUtil.*;
26
27 /**
28  * A skeletal {@link NameResolver} implementation.
29  */

30 public abstract class SimpleNameResolver<T> implements NameResolver<T> {
31
32     private final EventExecutor executor;
33
34     /**
35      * @param executor the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned
36      *                 by {@link #resolve(String)}
37      */

38     protected SimpleNameResolver(EventExecutor executor) {
39         this.executor = checkNotNull(executor, "executor");
40     }
41
42     /**
43      * Returns the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned
44      * by {@link #resolve(String)}.
45      */

46     protected EventExecutor executor() {
47         return executor;
48     }
49
50     @Override
51     public final Future<T> resolve(String inetHost) {
52         final Promise<T> promise = executor().newPromise();
53         return resolve(inetHost, promise);
54     }
55
56     @Override
57     public Future<T> resolve(String inetHost, Promise<T> promise) {
58         checkNotNull(promise, "promise");
59
60         try {
61             doResolve(inetHost, promise);
62             return promise;
63         } catch (Exception e) {
64             return promise.setFailure(e);
65         }
66     }
67
68     @Override
69     public final Future<List<T>> resolveAll(String inetHost) {
70         final Promise<List<T>> promise = executor().newPromise();
71         return resolveAll(inetHost, promise);
72     }
73
74     @Override
75     public Future<List<T>> resolveAll(String inetHost, Promise<List<T>> promise) {
76         checkNotNull(promise, "promise");
77
78         try {
79             doResolveAll(inetHost, promise);
80             return promise;
81         } catch (Exception e) {
82             return promise.setFailure(e);
83         }
84     }
85
86     /**
87      * Invoked by {@link #resolve(String)} to perform the actual name resolution.
88      */

89     protected abstract void doResolve(String inetHost, Promise<T> promise) throws Exception;
90
91     /**
92      * Invoked by {@link #resolveAll(String)} to perform the actual name resolution.
93      */

94     protected abstract void doResolveAll(String inetHost, Promise<List<T>> promise) throws Exception;
95
96     @Override
97     public void close() { }
98 }
99