1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import io.netty.channel.EventLoop;
19 import io.netty.util.internal.PlatformDependent;
20
21 import java.net.InetSocketAddress;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.List;
25 import java.util.concurrent.ConcurrentMap;
26
27 import static io.netty.util.internal.ObjectUtil.*;
28
29
30
31
32 public class DefaultAuthoritativeDnsServerCache implements AuthoritativeDnsServerCache {
33
34 private final int minTtl;
35 private final int maxTtl;
36 private final Comparator<InetSocketAddress> comparator;
37 private final Cache<InetSocketAddress> resolveCache = new Cache<InetSocketAddress>() {
38 @Override
39 protected boolean shouldReplaceAll(InetSocketAddress entry) {
40 return false;
41 }
42
43 @Override
44 protected boolean equals(InetSocketAddress entry, InetSocketAddress otherEntry) {
45 if (PlatformDependent.javaVersion() >= 7) {
46 return entry.getHostString().equalsIgnoreCase(otherEntry.getHostString());
47 }
48 return entry.getHostName().equalsIgnoreCase(otherEntry.getHostName());
49 }
50
51 @Override
52 protected void sortEntries(String hostname, List<InetSocketAddress> entries) {
53 if (comparator != null) {
54 Collections.sort(entries, comparator);
55 }
56 }
57 };
58
59
60
61
62 public DefaultAuthoritativeDnsServerCache() {
63 this(0, Cache.MAX_SUPPORTED_TTL_SECS, null);
64 }
65
66
67
68
69
70
71
72
73
74 public DefaultAuthoritativeDnsServerCache(int minTtl, int maxTtl, Comparator<InetSocketAddress> comparator) {
75 this.minTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositiveOrZero(minTtl, "minTtl"));
76 this.maxTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositive(maxTtl, "maxTtl"));
77 if (minTtl > maxTtl) {
78 throw new IllegalArgumentException(
79 "minTtl: " + minTtl + ", maxTtl: " + maxTtl + " (expected: 0 <= minTtl <= maxTtl)");
80 }
81 this.comparator = comparator;
82 }
83
84 @SuppressWarnings("unchecked")
85 @Override
86 public DnsServerAddressStream get(String hostname) {
87 checkNotNull(hostname, "hostname");
88
89 List<? extends InetSocketAddress> addresses = resolveCache.get(hostname);
90 if (addresses == null || addresses.isEmpty()) {
91 return null;
92 }
93 return new SequentialDnsServerAddressStream(addresses, 0);
94 }
95
96 @Override
97 public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
98 checkNotNull(hostname, "hostname");
99 checkNotNull(address, "address");
100 checkNotNull(loop, "loop");
101
102 if (PlatformDependent.javaVersion() >= 7 && address.getHostString() == null) {
103
104
105 return;
106 }
107
108 resolveCache.cache(hostname, address, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop);
109 }
110
111 @Override
112 public void clear() {
113 resolveCache.clear();
114 }
115
116 @Override
117 public boolean clear(String hostname) {
118 return resolveCache.clear(checkNotNull(hostname, "hostname"));
119 }
120
121 @Override
122 public String toString() {
123 return "DefaultAuthoritativeDnsServerCache(minTtl=" + minTtl + ", maxTtl=" + maxTtl + ", cached nameservers=" +
124 resolveCache.size() + ')';
125 }
126
127
128 int minTtl() {
129 return minTtl;
130 }
131
132
133 int maxTtl() {
134 return maxTtl;
135 }
136 }