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