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