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.NameResolver;
24  import io.netty.resolver.NameResolverGroup;
25  import io.netty.util.concurrent.EventExecutor;
26  import io.netty.util.internal.StringUtil;
27  
28  import java.net.InetSocketAddress;
29  
30  import static io.netty.resolver.dns.DnsNameResolver.*;
31  
32  /**
33   * A {@link NameResolverGroup} of {@link DnsNameResolver}s.
34   */
35  public final class DnsNameResolverGroup extends NameResolverGroup<InetSocketAddress> {
36  
37      private final ChannelFactory<? extends DatagramChannel> channelFactory;
38      private final InetSocketAddress localAddress;
39      private final Iterable<InetSocketAddress> nameServerAddresses;
40  
41      public DnsNameResolverGroup(
42              Class<? extends DatagramChannel> channelType,
43              InetSocketAddress nameServerAddress) {
44          this(channelType, ANY_LOCAL_ADDR, nameServerAddress);
45      }
46  
47      public DnsNameResolverGroup(
48              Class<? extends DatagramChannel> channelType,
49              InetSocketAddress localAddress, InetSocketAddress nameServerAddress) {
50          this(new ReflectiveChannelFactory<DatagramChannel>(channelType), localAddress, nameServerAddress);
51      }
52  
53      public DnsNameResolverGroup(
54              ChannelFactory<? extends DatagramChannel> channelFactory,
55              InetSocketAddress nameServerAddress) {
56          this(channelFactory, ANY_LOCAL_ADDR, nameServerAddress);
57      }
58  
59      public DnsNameResolverGroup(
60              ChannelFactory<? extends DatagramChannel> channelFactory,
61              InetSocketAddress localAddress, InetSocketAddress nameServerAddress) {
62          this(channelFactory, localAddress, DnsServerAddresses.singleton(nameServerAddress));
63      }
64  
65      public DnsNameResolverGroup(
66              Class<? extends DatagramChannel> channelType,
67              Iterable<InetSocketAddress> nameServerAddresses) {
68          this(channelType, ANY_LOCAL_ADDR, nameServerAddresses);
69      }
70  
71      public DnsNameResolverGroup(
72              Class<? extends DatagramChannel> channelType,
73              InetSocketAddress localAddress, Iterable<InetSocketAddress> nameServerAddresses) {
74          this(new ReflectiveChannelFactory<DatagramChannel>(channelType), localAddress, nameServerAddresses);
75      }
76  
77      public DnsNameResolverGroup(
78              ChannelFactory<? extends DatagramChannel> channelFactory,
79              Iterable<InetSocketAddress> nameServerAddresses) {
80          this(channelFactory, ANY_LOCAL_ADDR, nameServerAddresses);
81      }
82  
83      public DnsNameResolverGroup(
84              ChannelFactory<? extends DatagramChannel> channelFactory,
85              InetSocketAddress localAddress, Iterable<InetSocketAddress> nameServerAddresses) {
86          this.channelFactory = channelFactory;
87          this.localAddress = localAddress;
88          this.nameServerAddresses = nameServerAddresses;
89      }
90  
91      @Override
92      protected NameResolver<InetSocketAddress> newResolver(EventExecutor executor) throws Exception {
93          if (!(executor instanceof EventLoop)) {
94              throw new IllegalStateException(
95                      "unsupported executor type: " + StringUtil.simpleClassName(executor) +
96                      " (expected: " + StringUtil.simpleClassName(EventLoop.class));
97          }
98  
99          return new DnsNameResolver((EventLoop) executor, channelFactory, localAddress, nameServerAddresses);
100     }
101 }