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