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