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    *   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  
17  package io.netty.resolver.dns;
18  
19  import io.netty.channel.ChannelFactory;
20  import io.netty.channel.EventLoop;
21  import io.netty.channel.ReflectiveChannelFactory;
22  import io.netty.channel.socket.DatagramChannel;
23  import io.netty.resolver.AddressResolver;
24  import io.netty.resolver.AddressResolverGroup;
25  import io.netty.resolver.InetSocketAddressResolver;
26  import io.netty.resolver.NameResolver;
27  import io.netty.util.concurrent.EventExecutor;
28  import io.netty.util.concurrent.Promise;
29  import io.netty.util.internal.StringUtil;
30  import io.netty.util.internal.UnstableApi;
31  
32  import java.net.InetAddress;
33  import java.net.InetSocketAddress;
34  import java.util.List;
35  import java.util.concurrent.ConcurrentMap;
36  
37  import static io.netty.util.internal.PlatformDependent.newConcurrentHashMap;
38  
39  /**
40   * A {@link AddressResolverGroup} of {@link DnsNameResolver}s.
41   */
42  @UnstableApi
43  public class DnsAddressResolverGroup extends AddressResolverGroup<InetSocketAddress> {
44  
45      private final ChannelFactory<? extends DatagramChannel> channelFactory;
46      private final DnsServerAddressStreamProvider nameServerProvider;
47  
48      private final ConcurrentMap<String, Promise<InetAddress>> resolvesInProgress = newConcurrentHashMap();
49      private final ConcurrentMap<String, Promise<List<InetAddress>>> resolveAllsInProgress = newConcurrentHashMap();
50  
51      public DnsAddressResolverGroup(
52              Class<? extends DatagramChannel> channelType,
53              DnsServerAddressStreamProvider nameServerProvider) {
54          this(new ReflectiveChannelFactory<DatagramChannel>(channelType), nameServerProvider);
55      }
56  
57      public DnsAddressResolverGroup(
58              ChannelFactory<? extends DatagramChannel> channelFactory,
59              DnsServerAddressStreamProvider nameServerProvider) {
60          this.channelFactory = channelFactory;
61          this.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          return newResolver((EventLoop) executor, channelFactory, nameServerProvider);
74      }
75  
76      /**
77       * @deprecated Override {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)}.
78       */
79      @Deprecated
80      protected AddressResolver<InetSocketAddress> newResolver(
81              EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
82              DnsServerAddressStreamProvider nameServerProvider) throws Exception {
83  
84          final NameResolver<InetAddress> resolver = new InflightNameResolver<InetAddress>(
85                  eventLoop,
86                  newNameResolver(eventLoop, channelFactory, nameServerProvider),
87                  resolvesInProgress,
88                  resolveAllsInProgress);
89  
90          return newAddressResolver(eventLoop, resolver);
91      }
92  
93      /**
94       * Creates a new {@link NameResolver}. Override this method to create an alternative {@link NameResolver}
95       * implementation or override the default configuration.
96       */
97      protected NameResolver<InetAddress> newNameResolver(EventLoop eventLoop,
98                                                          ChannelFactory<? extends DatagramChannel> channelFactory,
99                                                          DnsServerAddressStreamProvider nameServerProvider)
100             throws Exception {
101         return new DnsNameResolverBuilder(eventLoop)
102                 .channelFactory(channelFactory)
103                 .nameServerProvider(nameServerProvider)
104                 .build();
105     }
106 
107     /**
108      * Creates a new {@link AddressResolver}. Override this method to create an alternative {@link AddressResolver}
109      * implementation or override the default configuration.
110      */
111     protected AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
112                                                                     NameResolver<InetAddress> resolver)
113             throws Exception {
114         return new InetSocketAddressResolver(eventLoop, resolver);
115     }
116 }