View Javadoc
1   /*
2    * Copyright 2015 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  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.UnstableApi;
23  
24  import java.net.InetAddress;
25  import java.net.InetSocketAddress;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * A {@link AbstractAddressResolver} that resolves {@link InetSocketAddress}.
31   */
32  @UnstableApi
33  public class InetSocketAddressResolver extends AbstractAddressResolver<InetSocketAddress> {
34  
35      final NameResolver<InetAddress> nameResolver;
36  
37      /**
38       * @param executor the {@link EventExecutor} which is used to notify the listeners of the {@link Future} returned
39       *                 by {@link #resolve(java.net.SocketAddress)}
40       * @param nameResolver the {@link NameResolver} used for name resolution
41       */
42      public InetSocketAddressResolver(EventExecutor executor, NameResolver<InetAddress> nameResolver) {
43          super(executor, InetSocketAddress.class);
44          this.nameResolver = nameResolver;
45      }
46  
47      @Override
48      protected boolean doIsResolved(InetSocketAddress address) {
49          return !address.isUnresolved();
50      }
51  
52      @Override
53      protected void doResolve(final InetSocketAddress unresolvedAddress, final Promise<InetSocketAddress> promise)
54              throws Exception {
55          // Note that InetSocketAddress.getHostName() will never incur a reverse lookup here,
56          // because an unresolved address always has a host name.
57          nameResolver.resolve(unresolvedAddress.getHostName())
58                  .addListener(new FutureListener<InetAddress>() {
59                      @Override
60                      public void operationComplete(Future<InetAddress> future) throws Exception {
61                          if (future.isSuccess()) {
62                              promise.setSuccess(new InetSocketAddress(future.getNow(), unresolvedAddress.getPort()));
63                          } else {
64                              promise.setFailure(future.cause());
65                          }
66                      }
67                  });
68      }
69  
70      @Override
71      protected void doResolveAll(final InetSocketAddress unresolvedAddress,
72                                  final Promise<List<InetSocketAddress>> promise) throws Exception {
73          // Note that InetSocketAddress.getHostName() will never incur a reverse lookup here,
74          // because an unresolved address always has a host name.
75          nameResolver.resolveAll(unresolvedAddress.getHostName())
76                  .addListener(new FutureListener<List<InetAddress>>() {
77                      @Override
78                      public void operationComplete(Future<List<InetAddress>> future) throws Exception {
79                          if (future.isSuccess()) {
80                              List<InetAddress> inetAddresses = future.getNow();
81                              List<InetSocketAddress> socketAddresses =
82                                      new ArrayList<InetSocketAddress>(inetAddresses.size());
83                              for (InetAddress inetAddress : inetAddresses) {
84                                  socketAddresses.add(new InetSocketAddress(inetAddress, unresolvedAddress.getPort()));
85                              }
86                              promise.setSuccess(socketAddresses);
87                          } else {
88                              promise.setFailure(future.cause());
89                          }
90                      }
91                  });
92      }
93  
94      @Override
95      public void close() {
96          nameResolver.close();
97      }
98  }