Skip to content

Commit c1db394

Browse files
committed
Merge branch 'fix#115'
Issue on GitHub Actions with latest Zulu 8u352 JDK #115
2 parents 2ca8499 + 1d68b9f commit c1db394

File tree

12 files changed

+98
-477
lines changed

12 files changed

+98
-477
lines changed

.github/workflows/ci.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
# https://docs.github.com/en/actions/quickstart
33

44
name: CI
5-
on: [ push, pull_request ]
5+
on: [ push, pull_request, workflow_dispatch ]
6+
67
jobs:
8+
79
test:
810
runs-on: ${{ matrix.os }}
911
timeout-minutes: 10
1012
strategy:
1113
matrix:
1214
os: [ ubuntu-latest, windows-latest ]
13-
java: [ 8, 11, 17, 18, 19, 20-ea ]
15+
java: [ 8.0.345, 8, 11, 17, 19, 20-ea ]
1416
fail-fast: false
1517
max-parallel: 64
16-
name: Test JDK ${{ matrix.java }}, ${{ matrix.os }}
18+
name: Test on JDK ${{ matrix.java }} OS ${{ matrix.os }}
1719

1820
steps:
1921
- uses: actions/checkout@v3
2022
- uses: actions/setup-java@v3
2123
with:
2224
java-version: ${{ matrix.java }}
23-
distribution: 'zulu'
24-
cache: 'maven'
25-
- name: Build with Maven
26-
run: ./mvnw -V --no-transfer-progress -Pgen-code-cov clean package
27-
- name: Upload coverage to Codecov
28-
# https://github.com/marketplace/actions/codecov
29-
uses: codecov/codecov-action@v3
25+
distribution: zulu
26+
cache: maven
27+
- run: ./mvnw -V --no-transfer-progress -Pgen-code-cov clean package
28+
# https://github.com/marketplace/actions/codecov
29+
- uses: codecov/codecov-action@v3
3030
with:
3131
name: codecov-umbrella
3232
token: ${{ secrets.CODECOV_TOKEN }}

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.alibaba.dcm;
22

33
import com.alibaba.dcm.internal.InetAddressCacheUtilCommons;
4-
import com.alibaba.dcm.internal.InetAddressCacheUtilForJava8;
5-
import com.alibaba.dcm.internal.InetAddressCacheUtilForJava9Plus;
4+
import com.alibaba.dcm.internal.InetAddressCacheUtilForOld;
5+
import com.alibaba.dcm.internal.InetAddressCacheUtilForNew;
66
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
77
import sun.net.InetAddressCachePolicy;
88

@@ -16,7 +16,7 @@
1616
import java.util.regex.Pattern;
1717

1818
import static com.alibaba.dcm.internal.InetAddressCacheUtilCommons.NEVER_EXPIRATION;
19-
import static com.alibaba.dcm.internal.JavaVersionUtil.isJavaVersionAtMost8;
19+
import static com.alibaba.dcm.internal.InetAddressCacheUtilCommons.isNewInetAddressImpl;
2020

2121

2222
/**
@@ -42,10 +42,10 @@ public final class DnsCacheManipulator {
4242
*/
4343
public static void setDnsCache(String host, String... ips) {
4444
try {
45-
if (isJavaVersionAtMost8()) {
46-
InetAddressCacheUtilForJava8.setInetAddressCache(host, ips, NEVER_EXPIRATION);
45+
if (isNewInetAddressImpl()) {
46+
InetAddressCacheUtilForNew.setInetAddressCache(host, ips, NEVER_EXPIRATION);
4747
} else {
48-
InetAddressCacheUtilForJava9Plus.setInetAddressCache(host, ips, NEVER_EXPIRATION);
48+
InetAddressCacheUtilForOld.setInetAddressCache(host, ips, NEVER_EXPIRATION);
4949
}
5050
} catch (Exception e) {
5151
final String message = String.format("Fail to setDnsCache for host %s ip %s, cause: %s",
@@ -64,10 +64,10 @@ public static void setDnsCache(String host, String... ips) {
6464
*/
6565
public static void setDnsCache(long expireMillis, String host, String... ips) {
6666
try {
67-
if (isJavaVersionAtMost8()) {
68-
InetAddressCacheUtilForJava8.setInetAddressCache(host, ips, expireMillis);
67+
if (isNewInetAddressImpl()) {
68+
InetAddressCacheUtilForNew.setInetAddressCache(host, ips, expireMillis);
6969
} else {
70-
InetAddressCacheUtilForJava9Plus.setInetAddressCache(host, ips, expireMillis);
70+
InetAddressCacheUtilForOld.setInetAddressCache(host, ips, expireMillis);
7171
}
7272
} catch (Exception e) {
7373
final String message = String.format("Fail to setDnsCache for host %s ip %s expireMillis %s, cause: %s",
@@ -152,10 +152,10 @@ public static void loadDnsCacheConfig(String propertiesFileName) {
152152
@Nullable
153153
public static DnsCacheEntry getDnsCache(String host) {
154154
try {
155-
if (isJavaVersionAtMost8()) {
156-
return InetAddressCacheUtilForJava8.getInetAddressCache(host);
155+
if (isNewInetAddressImpl()) {
156+
return InetAddressCacheUtilForNew.getInetAddressCache(host);
157157
} else {
158-
return InetAddressCacheUtilForJava9Plus.getInetAddressCache(host);
158+
return InetAddressCacheUtilForOld.getInetAddressCache(host);
159159
}
160160
} catch (Exception e) {
161161
throw new DnsCacheManipulatorException("Fail to getDnsCache, cause: " + e, e);
@@ -174,10 +174,10 @@ public static DnsCacheEntry getDnsCache(String host) {
174174
*/
175175
public static DnsCache getWholeDnsCache() {
176176
try {
177-
if (isJavaVersionAtMost8()) {
178-
return InetAddressCacheUtilForJava8.listInetAddressCache();
177+
if (isNewInetAddressImpl()) {
178+
return InetAddressCacheUtilForNew.listInetAddressCache();
179179
} else {
180-
return InetAddressCacheUtilForJava9Plus.listInetAddressCache();
180+
return InetAddressCacheUtilForOld.listInetAddressCache();
181181
}
182182
} catch (Exception e) {
183183
throw new DnsCacheManipulatorException("Fail to getWholeDnsCache, cause: " + e, e);
@@ -235,10 +235,10 @@ public static List<DnsCacheEntry> listDnsNegativeCache() {
235235
*/
236236
public static void removeDnsCache(String host) {
237237
try {
238-
if (isJavaVersionAtMost8()) {
239-
InetAddressCacheUtilForJava8.removeInetAddressCache(host);
238+
if (isNewInetAddressImpl()) {
239+
InetAddressCacheUtilForNew.removeInetAddressCache(host);
240240
} else {
241-
InetAddressCacheUtilForJava9Plus.removeInetAddressCache(host);
241+
InetAddressCacheUtilForOld.removeInetAddressCache(host);
242242
}
243243
} catch (Exception e) {
244244
final String message = String.format("Fail to removeDnsCache for host %s, cause: %s", host, e);
@@ -253,10 +253,10 @@ public static void removeDnsCache(String host) {
253253
*/
254254
public static void clearDnsCache() {
255255
try {
256-
if (isJavaVersionAtMost8()) {
257-
InetAddressCacheUtilForJava8.clearInetAddressCache();
256+
if (isNewInetAddressImpl()) {
257+
InetAddressCacheUtilForNew.clearInetAddressCache();
258258
} else {
259-
InetAddressCacheUtilForJava9Plus.clearInetAddressCache();
259+
InetAddressCacheUtilForOld.clearInetAddressCache();
260260
}
261261
} catch (Exception e) {
262262
throw new DnsCacheManipulatorException("Fail to clearDnsCache, cause: " + e, e);

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilCommons.java

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

33
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
4+
import org.jetbrains.annotations.ApiStatus;
45
import sun.net.InetAddressCachePolicy;
56

67
import javax.annotation.Nullable;
@@ -17,6 +18,7 @@
1718
*/
1819
@ParametersAreNonnullByDefault
1920
@ReturnValuesAreNonnullByDefault
21+
@ApiStatus.Internal
2022
public final class InetAddressCacheUtilCommons {
2123
/**
2224
* We never really have "never".
@@ -181,6 +183,30 @@ private static void initFieldsOfInetAddressCachePolicy() throws NoSuchFieldExcep
181183
}
182184
}
183185

186+
private static volatile Boolean isNew;
187+
188+
/**
189+
* Check the new or old implementation of {@link InetAddress}
190+
* by whether the field {@link InetAddress.expirySet} is existed or not.
191+
*/
192+
public static boolean isNewInetAddressImpl() {
193+
if (isNew != null) return isNew;
194+
195+
synchronized (InetAddressCacheUtilCommons.class) {
196+
// double check
197+
if (isNew != null) return isNew;
198+
199+
try {
200+
InetAddress.class.getDeclaredField("expirySet");
201+
isNew = true;
202+
} catch (NoSuchFieldException e) {
203+
isNew = false;
204+
}
205+
206+
return isNew;
207+
}
208+
}
209+
184210
private InetAddressCacheUtilCommons() {
185211
}
186212
}

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJava9Plus.java renamed to library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForNew.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.dcm.DnsCache;
44
import com.alibaba.dcm.DnsCacheEntry;
55
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
6+
import org.jetbrains.annotations.ApiStatus;
67

78
import javax.annotation.Nullable;
89
import javax.annotation.ParametersAreNonnullByDefault;
@@ -23,7 +24,7 @@
2324
import static com.alibaba.dcm.internal.TimeUtil.getNanoTimeAfterMs;
2425

2526
/**
26-
* Util class to manipulate dns cache for {@code JDK 9+}.
27+
* Util class to manipulate dns cache for new {@code JDK 9+}.
2728
* <p>
2829
* dns cache is {@link InetAddress#cache}.
2930
*
@@ -33,7 +34,8 @@
3334
*/
3435
@ParametersAreNonnullByDefault
3536
@ReturnValuesAreNonnullByDefault
36-
public final class InetAddressCacheUtilForJava9Plus {
37+
@ApiStatus.Internal
38+
public final class InetAddressCacheUtilForNew {
3739
/**
3840
* {@link InetAddress.CachedAddresses}
3941
* <p>
@@ -120,7 +122,7 @@ private static void removeHostFromExpirySetOfInetAddress(String host)
120122
private static String getHostOfInetAddress$CacheAddress(Object cachedAddresses)
121123
throws NoSuchFieldException, IllegalAccessException {
122124
if (hostFieldOfInetAddress$CacheAddress == null) {
123-
synchronized (InetAddressCacheUtilForJava9Plus.class) {
125+
synchronized (InetAddressCacheUtilForNew.class) {
124126
if (hostFieldOfInetAddress$CacheAddress == null) { // double check
125127
final Field f = cachedAddresses.getClass().getDeclaredField("host");
126128
f.setAccessible(true);
@@ -169,7 +171,7 @@ private static Object[] getCacheAndExpirySetOfInetAddress0()
169171
throws NoSuchFieldException, IllegalAccessException {
170172
if (ADDRESS_CACHE_AND_EXPIRY_SET != null) return ADDRESS_CACHE_AND_EXPIRY_SET;
171173

172-
synchronized (InetAddressCacheUtilForJava9Plus.class) {
174+
synchronized (InetAddressCacheUtilForNew.class) {
173175
if (ADDRESS_CACHE_AND_EXPIRY_SET != null) return ADDRESS_CACHE_AND_EXPIRY_SET;
174176

175177
final Field cacheField = InetAddress.class.getDeclaredField("cache");
@@ -266,7 +268,7 @@ public static DnsCache listInetAddressCache()
266268
private static void initFieldsOfAddresses() throws ClassNotFoundException, NoSuchFieldException {
267269
if (inetAddressesFieldOfInetAddress$CacheAddress != null) return;
268270

269-
synchronized (InetAddressCacheUtilForJava9Plus.class) {
271+
synchronized (InetAddressCacheUtilForNew.class) {
270272
if (inetAddressesFieldOfInetAddress$CacheAddress != null) return;
271273

272274
///////////////////////////////////////////////
@@ -289,6 +291,6 @@ public static void clearInetAddressCache() throws NoSuchFieldException, IllegalA
289291
getCacheOfInetAddress().clear();
290292
}
291293

292-
private InetAddressCacheUtilForJava9Plus() {
294+
private InetAddressCacheUtilForNew() {
293295
}
294296
}

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJava8.java renamed to library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForOld.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.dcm.DnsCache;
44
import com.alibaba.dcm.DnsCacheEntry;
55
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
6+
import org.jetbrains.annotations.ApiStatus;
67

78
import javax.annotation.Nullable;
89
import javax.annotation.ParametersAreNonnullByDefault;
@@ -20,7 +21,7 @@
2021
import static com.alibaba.dcm.internal.InetAddressCacheUtilCommons.*;
2122

2223
/**
23-
* Util class to manipulate dns cache for {@code JDK 8-}.
24+
* Util class to manipulate dns cache for old {@code JDK 8-}.
2425
* <p>
2526
* dns cache is {@link InetAddress.Cache#cache} in {@link InetAddress#addressCache}.
2627
* <p>
@@ -39,7 +40,8 @@
3940
*/
4041
@ParametersAreNonnullByDefault
4142
@ReturnValuesAreNonnullByDefault
42-
public final class InetAddressCacheUtilForJava8 {
43+
@ApiStatus.Internal
44+
public final class InetAddressCacheUtilForOld {
4345
/**
4446
* Need convert host to lowercase, see {@link InetAddress#cacheAddresses(String, InetAddress[], boolean)}.
4547
*/
@@ -134,7 +136,7 @@ private static Map<String, Object> getNegativeCache()
134136
private static Map<String, Object> getCacheOfInetAddress$Cache0(Object inetAddressCache)
135137
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
136138
if (cacheMapFieldOfInetAddress$Cache == null) {
137-
synchronized (InetAddressCacheUtilForJava8.class) {
139+
synchronized (InetAddressCacheUtilForOld.class) {
138140
if (cacheMapFieldOfInetAddress$Cache == null) { // double check
139141
final Class<?> clazz = Class.forName("java.net.InetAddress$Cache");
140142
final Field f = clazz.getDeclaredField("cache");
@@ -172,7 +174,7 @@ private static Object[] getAddressCacheAndNegativeCacheOfInetAddress0()
172174
throws NoSuchFieldException, IllegalAccessException {
173175
if (ADDRESS_CACHE_AND_NEGATIVE_CACHE != null) return ADDRESS_CACHE_AND_NEGATIVE_CACHE;
174176

175-
synchronized (InetAddressCacheUtilForJava8.class) {
177+
synchronized (InetAddressCacheUtilForOld.class) {
176178
// double check
177179
if (ADDRESS_CACHE_AND_NEGATIVE_CACHE != null) return ADDRESS_CACHE_AND_NEGATIVE_CACHE;
178180

@@ -262,7 +264,7 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache)
262264
private static void initFieldsOfInetAddress$CacheEntry() throws ClassNotFoundException, NoSuchFieldException {
263265
if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null) return;
264266

265-
synchronized (InetAddressCacheUtilForJava8.class) {
267+
synchronized (InetAddressCacheUtilForOld.class) {
266268
final Class<?> cacheEntryClass = Class.forName("java.net.InetAddress$CacheEntry");
267269
// double check
268270
if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null)
@@ -297,6 +299,6 @@ public static void clearInetAddressCache()
297299
}
298300
}
299301

300-
private InetAddressCacheUtilForJava8() {
302+
private InetAddressCacheUtilForOld() {
301303
}
302304
}

0 commit comments

Comments
 (0)