Skip to content

Commit b867c7c

Browse files
committed
null-safe for method DnsCacheManipulator.getDnsCache #21
1 parent 8c865e3 commit b867c7c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/main/java/com/alibaba/dcm/DnsCacheManipulator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Properties;
1010
import java.util.regex.Pattern;
1111

12+
import javax.annotation.Nullable;
13+
1214
/**
1315
* Setting dns (in fact dns cache).
1416
* <p/>
@@ -119,9 +121,10 @@ public static void loadDnsCacheConfig(String propertiesFileName) {
119121
/**
120122
* Get dns cache.
121123
*
122-
* @return dns cache
124+
* @return dns cache. return {@code null} if no entry for host or dns cache is expired.
123125
* @throws DnsCacheManipulatorException Operation fail
124126
*/
127+
@Nullable
125128
public static DnsCacheEntry getDnsCache(String host) {
126129
try {
127130
return InetAddressCacheUtil.getInetAddressCache(host);

src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtil.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,21 @@ public static DnsCacheEntry getInetAddressCache(String host)
181181
throws NoSuchFieldException, IllegalAccessException {
182182
host = host.toLowerCase();
183183

184-
Object cacheEntry;
184+
final Object cacheEntry;
185185
synchronized (getAddressCacheFieldOfInetAddress()) {
186186
cacheEntry = getCacheFiledOfAddressCacheFiledOfInetAddress().get(host);
187187
}
188-
return inetAddress$CacheEntry2DnsCacheEntry(host, cacheEntry);
188+
189+
if (null == cacheEntry) return null;
190+
191+
final DnsCacheEntry dnsCacheEntry = inetAddress$CacheEntry2DnsCacheEntry(host, cacheEntry);
192+
if (isDnsCacheEntryExpired(dnsCacheEntry.getHost())) return null;
193+
194+
return dnsCacheEntry;
195+
}
196+
197+
static boolean isDnsCacheEntryExpired(String host) {
198+
return null == host || "0.0.0.0".equals(host);
189199
}
190200

191201
public static DnsCache listInetAddressCache()
@@ -202,7 +212,7 @@ public static DnsCache listInetAddressCache()
202212
for (Map.Entry<String, Object> entry : cache.entrySet()) {
203213
final String host = entry.getKey();
204214

205-
if ("0.0.0.0".equals(host) || host == null) { // exclude expired entries!
215+
if (isDnsCacheEntryExpired(host)) { // exclude expired entries!
206216
continue;
207217
}
208218
retCache.add(inetAddress$CacheEntry2DnsCacheEntry(host, entry.getValue()));

src/test/java/com/alibaba/dcm/DnsCacheManipulatorTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.alibaba.dcm;
22

33
import org.junit.Before;
4-
import org.junit.BeforeClass;
54
import org.junit.Test;
65

76
import java.net.InetAddress;
@@ -11,6 +10,7 @@
1110
import java.util.List;
1211

1312
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNull;
1414
import static org.junit.Assert.assertTrue;
1515
import static org.junit.Assert.fail;
1616

@@ -150,4 +150,10 @@ public void test_multi_ips_in_config_file() throws Exception {
150150
new String[]{"42.42.41.1", "42.42.41.2", "42.42.41.3", "42.42.41.4"}, new Date(Long.MAX_VALUE));
151151
assertEquals(entryLoose, DnsCacheManipulator.getDnsCache(hostLoose));
152152
}
153+
154+
@Test
155+
public void test_nullSafeForGetDnsCache() throws Exception {
156+
final DnsCacheEntry dnsCache = DnsCacheManipulator.getDnsCache(DOMAIN_NOT_EXISTED);
157+
assertNull(dnsCache);
158+
}
153159
}

0 commit comments

Comments
 (0)