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    *   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  
17  package io.netty5.resolver.dns;
18  
19  import io.netty5.channel.ChannelFactory;
20  import io.netty5.channel.EventLoop;
21  import io.netty5.channel.socket.DatagramChannel;
22  import io.netty5.resolver.AddressResolver;
23  import io.netty5.resolver.AddressResolverGroup;
24  import io.netty5.resolver.InetSocketAddressResolver;
25  import io.netty5.resolver.NameResolver;
26  import io.netty5.util.concurrent.EventExecutor;
27  import io.netty5.util.concurrent.Promise;
28  import io.netty5.util.internal.StringUtil;
29  
30  import java.net.InetAddress;
31  import java.net.InetSocketAddress;
32  import java.util.List;
33  import java.util.concurrent.ConcurrentHashMap;
34  import java.util.concurrent.ConcurrentMap;
35  
36  /**
37   * A {@link AddressResolverGroup} of {@link DnsNameResolver}s.
38   */
39  public class DnsAddressResolverGroup extends AddressResolverGroup<InetSocketAddress> {
40  
41      private final DnsNameResolverBuilder dnsResolverBuilder;
42  
43      private final ConcurrentMap<String, Promise<InetAddress>> resolvesInProgress = new ConcurrentHashMap<>();
44      private final ConcurrentMap<String, Promise<List<InetAddress>>> resolveAllsInProgress = new ConcurrentHashMap<>();
45  
46      public DnsAddressResolverGroup(DnsNameResolverBuilder dnsResolverBuilder) {
47          this.dnsResolverBuilder = dnsResolverBuilder.copy();
48      }
49  
50      public DnsAddressResolverGroup(
51              Class<? extends DatagramChannel> channelType,
52              DnsServerAddressStreamProvider nameServerProvider) {
53          this.dnsResolverBuilder = new DnsNameResolverBuilder();
54          dnsResolverBuilder.channelType(channelType).nameServerProvider(nameServerProvider);
55      }
56  
57      public DnsAddressResolverGroup(
58              ChannelFactory<? extends DatagramChannel> channelFactory,
59              DnsServerAddressStreamProvider nameServerProvider) {
60          this.dnsResolverBuilder = new DnsNameResolverBuilder();
61          dnsResolverBuilder.channelFactory(channelFactory).nameServerProvider(nameServerProvider);
62      }
63  
64      @SuppressWarnings("deprecation")
65      @Override
66      protected final AddressResolver<InetSocketAddress> newResolver(EventExecutor executor) throws Exception {
67          if (!(executor instanceof EventLoop)) {
68              throw new IllegalStateException(
69                      "unsupported executor type: " + StringUtil.simpleClassName(executor) +
70                      " (expected: " + StringUtil.simpleClassName(EventLoop.class));
71          }
72  
73          // we don't really need to pass channelFactory and nameServerProvider separately,
74          // but still keep this to ensure backward compatibility with (potentially) override methods
75          EventLoop loop = dnsResolverBuilder.eventLoop;
76          return newResolver(loop == null ? (EventLoop) executor : loop,
77                  dnsResolverBuilder.channelFactory(),
78                  dnsResolverBuilder.nameServerProvider());
79      }
80  
81      /**
82       * @deprecated Override {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)}.
83       */
84      @Deprecated
85      protected AddressResolver<InetSocketAddress> newResolver(
86              EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
87              DnsServerAddressStreamProvider nameServerProvider) throws Exception {
88  
89          final NameResolver<InetAddress> resolver = new InflightNameResolver<>(
90                  eventLoop,
91                  newNameResolver(eventLoop, channelFactory, nameServerProvider),
92                  resolvesInProgress,
93                  resolveAllsInProgress);
94  
95          return newAddressResolver(eventLoop, resolver);
96      }
97  
98      /**
99       * Creates a new {@link NameResolver}. Override this method to create an alternative {@link NameResolver}
100      * implementation or override the default configuration.
101      */
102     protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
103                                                         ChannelFactory<? extends DatagramChannel> channelFactory,
104                                                         DnsServerAddressStreamProvider nameServerProvider)
105             throws Exception {
106         DnsNameResolverBuilder builder = dnsResolverBuilder.copy();
107 
108         // once again, channelFactory and nameServerProvider are most probably set in builder already,
109         // but I do reassign them again to avoid corner cases with override methods
110         return builder.eventLoop(eventLoop)
111                 .channelFactory(channelFactory)
112                 .nameServerProvider(nameServerProvider)
113                 .build();
114     }
115 
116     /**
117      * Creates a new {@link AddressResolver}. Override this method to create an alternative {@link AddressResolver}
118      * implementation or override the default configuration.
119      */
120     protected AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
121                                                                     NameResolver<InetAddress> resolver)
122             throws Exception {
123         return new InetSocketAddressResolver(eventLoop, resolver);
124     }
125 }