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