View Javadoc
1   /*
2    * Copyright 2017 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  package io.netty.resolver.dns;
17  
18  import io.netty.channel.ChannelFuture;
19  import io.netty.handler.codec.dns.DnsQuestion;
20  import io.netty.handler.codec.dns.DnsResponseCode;
21  
22  import java.net.InetSocketAddress;
23  import java.util.List;
24  
25  import static io.netty.util.internal.ObjectUtil.checkNotNull;
26  
27  /**
28   * Combines two {@link DnsQueryLifecycleObserver} into a single {@link DnsQueryLifecycleObserver}.
29   */
30  public final class BiDnsQueryLifecycleObserver implements DnsQueryLifecycleObserver {
31      private final DnsQueryLifecycleObserver a;
32      private final DnsQueryLifecycleObserver b;
33  
34      /**
35       * Create a new instance.
36       * @param a The {@link DnsQueryLifecycleObserver} that will receive events first.
37       * @param b The {@link DnsQueryLifecycleObserver} that will receive events second.
38       */
39      public BiDnsQueryLifecycleObserver(DnsQueryLifecycleObserver a, DnsQueryLifecycleObserver b) {
40          this.a = checkNotNull(a, "a");
41          this.b = checkNotNull(b, "b");
42      }
43  
44      @Override
45      public void queryWritten(InetSocketAddress dnsServerAddress, ChannelFuture future) {
46          try {
47              a.queryWritten(dnsServerAddress, future);
48          } finally {
49              b.queryWritten(dnsServerAddress, future);
50          }
51      }
52  
53      @Override
54      public void queryCancelled(int queriesRemaining) {
55          try {
56              a.queryCancelled(queriesRemaining);
57          } finally {
58              b.queryCancelled(queriesRemaining);
59          }
60      }
61  
62      @Override
63      public DnsQueryLifecycleObserver queryRedirected(List<InetSocketAddress> nameServers) {
64          try {
65              a.queryRedirected(nameServers);
66          } finally {
67              b.queryRedirected(nameServers);
68          }
69          return this;
70      }
71  
72      @Override
73      public DnsQueryLifecycleObserver queryCNAMEd(DnsQuestion cnameQuestion) {
74          try {
75              a.queryCNAMEd(cnameQuestion);
76          } finally {
77              b.queryCNAMEd(cnameQuestion);
78          }
79          return this;
80      }
81  
82      @Override
83      public DnsQueryLifecycleObserver queryNoAnswer(DnsResponseCode code) {
84          try {
85              a.queryNoAnswer(code);
86          } finally {
87              b.queryNoAnswer(code);
88          }
89          return this;
90      }
91  
92      @Override
93      public void queryFailed(Throwable cause) {
94          try {
95              a.queryFailed(cause);
96          } finally {
97              b.queryFailed(cause);
98          }
99      }
100 
101     @Override
102     public void querySucceed() {
103         try {
104             a.querySucceed();
105         } finally {
106             b.querySucceed();
107         }
108     }
109 }