• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / java-dns-cache-manipulator / 1267

pending completion
1267

push

Appveyor

oldratlee
chore(ci): upgrade CI JDK ๐Ÿค–

527 of 645 relevant lines covered (81.71%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.06
/library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilCommons.java
1
package com.alibaba.dcm.internal;
2

3
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
4
import org.jetbrains.annotations.ApiStatus;
5
import sun.net.InetAddressCachePolicy;
6

7
import javax.annotation.Nullable;
8
import javax.annotation.ParametersAreNonnullByDefault;
9
import java.lang.reflect.Field;
10
import java.net.InetAddress;
11
import java.net.UnknownHostException;
12

13
/**
14
 * Util class to manipulate dns cache.
15
 *
16
 * @author Jerry Lee (oldratlee at gmail dot com)
17
 * @since 1.6.0
18
 */
19
@ParametersAreNonnullByDefault
20
@ReturnValuesAreNonnullByDefault
21
@ApiStatus.Internal
22
public final class InetAddressCacheUtilCommons {
23
    /**
24
     * We never really have "never".
25
     * <p>
26
     * {@code Long.MAX_VALUE(~9e18)} nanoseconds is ~292 years.
27
     * <p>
28
     * {@code Long.MAX_VALUE / 1e9 / 3600 / 24 / 365 ~= 292.47}.
29
     *
30
     * @see System#nanoTime()
31
     */
32
    public static final long NEVER_EXPIRATION = Long.MAX_VALUE;
33

34
    static InetAddress[] toInetAddressArray(String host, String[] ips) throws UnknownHostException {
35
        InetAddress[] addresses = new InetAddress[ips.length];
1โœ”
36
        for (int i = 0; i < addresses.length; i++) {
1โœ”
37
            addresses[i] = InetAddress.getByAddress(host, IpParserUtil.ip2ByteArray(ips[i]));
1โœ”
38
        }
39
        return addresses;
1โœ”
40
    }
41

42
    static String[] getIpFromInetAddress(@Nullable InetAddress[] inetAddresses) {
43
        if (inetAddresses == null) return new String[0];
1โœ”
44

45
        final String[] ips = new String[inetAddresses.length];
1โœ”
46
        for (int i = 0; i < inetAddresses.length; i++) {
1โœ”
47
            ips[i] = inetAddresses[i].getHostAddress();
1โœ”
48
        }
49
        return ips;
1โœ”
50
    }
51

52
    /**
53
     * Set JVM DNS cache policy.
54
     *
55
     * @param cacheSeconds set default dns cache time. Special input case:
56
     *                     <ul>
57
     *                     <li> {@link InetAddressCachePolicy#FOREVER}({@code -1}) means never expired.(In effect, all negative value)</li>
58
     *                     <li> {@link InetAddressCachePolicy#NEVER}(@code 0) never cached.</li>
59
     *                     </ul>
60
     * @see InetAddressCachePolicy
61
     * @see InetAddressCachePolicy#cachePolicy
62
     * @see InetAddressCachePolicy#get()
63
     * @see InetAddressCachePolicy#FOREVER
64
     * @see InetAddressCachePolicy#NEVER
65
     * @see InetAddressCachePolicy#DEFAULT_POSITIVE
66
     */
67
    public static void setDnsCachePolicy(int cacheSeconds)
68
            throws NoSuchFieldException, IllegalAccessException {
69
        setCachePolicy0(false, cacheSeconds);
1โœ”
70
    }
1โœ”
71

72
    /**
73
     * Get JVM DNS cache policy.
74
     *
75
     * @see InetAddressCachePolicy#get()
76
     * @see InetAddressCachePolicy#FOREVER
77
     * @see InetAddressCachePolicy#NEVER
78
     * @see InetAddressCachePolicy#DEFAULT_POSITIVE
79
     */
80
    public static int getDnsCachePolicy() {
81
        return InetAddressCachePolicy.get();
1โœ”
82
    }
83

84
    /**
85
     * Set JVM DNS negative cache policy.
86
     *
87
     * @param negativeCacheSeconds set default dns cache time. Special input case:
88
     *                             <ul>
89
     *                             <li> {@link InetAddressCachePolicy#FOREVER}({@code -1}) means never expired.(In effect, all negative value)</li>
90
     *                             <li> {@link InetAddressCachePolicy#NEVER}(@code 0) never cached.</li>
91
     *                             </ul>
92
     * @see InetAddressCachePolicy
93
     * @see InetAddressCachePolicy#negativeCachePolicy
94
     * @see InetAddressCachePolicy#FOREVER
95
     * @see InetAddressCachePolicy#NEVER
96
     */
97
    public static void setDnsNegativeCachePolicy(int negativeCacheSeconds)
98
            throws NoSuchFieldException, IllegalAccessException {
99
        setCachePolicy0(true, negativeCacheSeconds);
1โœ”
100
    }
1โœ”
101

102
    /**
103
     * Get JVM DNS negative cache policy.
104
     *
105
     * @see InetAddressCachePolicy#getNegative()
106
     * @see InetAddressCachePolicy#FOREVER
107
     * @see InetAddressCachePolicy#NEVER
108
     */
109
    public static int getDnsNegativeCachePolicy() {
110
        return InetAddressCachePolicy.getNegative();
1โœ”
111
    }
112

113
    private static void setCachePolicy0(boolean isNegative, int seconds)
114
            throws NoSuchFieldException, IllegalAccessException {
115
        if (seconds < 0) {
1โœ”
116
            seconds = -1;
ร—
117
        }
118

119
        initFieldsOfInetAddressCachePolicy();
1โœ”
120

121
        synchronized (InetAddressCachePolicy.class) { // static synchronized method!
1โœ”
122
            if (isNegative) {
1โœ”
123
                negativeCachePolicyFiledOfInetAddressCachePolicy.setInt(null, seconds);
1โœ”
124
                negativeSetOfInetAddressCachePolicy.setBoolean(null, true);
1โœ”
125
            } else {
126
                cachePolicyFiledOfInetAddressCachePolicy.setInt(null, seconds);
1โœ”
127
                setFiledOfInetAddressCachePolicy.setBoolean(null, true);
1โœ”
128
            }
129
        }
1โœ”
130
    }
1โœ”
131

132
    /**
133
     * {@link InetAddressCachePolicy.cachePolicy}
134
     */
135
    private static volatile Field cachePolicyFiledOfInetAddressCachePolicy = null;
1โœ”
136
    /**
137
     * {@link InetAddressCachePolicy.negativeCachePolicy}
138
     */
139
    private static volatile Field negativeCachePolicyFiledOfInetAddressCachePolicy = null;
1โœ”
140
    /**
141
     * {@link InetAddressCachePolicy.propertySet}
142
     * or {@link InetAddressCachePolicy.set}
143
     */
144
    private static volatile Field setFiledOfInetAddressCachePolicy = null;
1โœ”
145
    /**
146
     * {@link InetAddressCachePolicy.propertyNegativeSet}
147
     * or {@link InetAddressCachePolicy.negativeSet}
148
     */
149
    private static volatile Field negativeSetOfInetAddressCachePolicy = null;
1โœ”
150

151
    @SuppressWarnings("JavaReflectionMemberAccess")
152
    private static void initFieldsOfInetAddressCachePolicy() throws NoSuchFieldException {
153
        if (negativeSetOfInetAddressCachePolicy != null) return;
1โœ”
154

155
        final Class<?> clazz = InetAddressCachePolicy.class;
1โœ”
156
        synchronized (InetAddressCacheUtilCommons.class) {
1โœ”
157
            // double check
158
            if (negativeSetOfInetAddressCachePolicy != null) return;
1โœ”
159

160
            Field f = clazz.getDeclaredField("cachePolicy");
1โœ”
161
            f.setAccessible(true);
1โœ”
162
            cachePolicyFiledOfInetAddressCachePolicy = f;
1โœ”
163

164
            f = clazz.getDeclaredField("negativeCachePolicy");
1โœ”
165
            f.setAccessible(true);
1โœ”
166
            negativeCachePolicyFiledOfInetAddressCachePolicy = f;
1โœ”
167

168
            try {
169
                f = clazz.getDeclaredField("propertySet");
1โœ”
170
            } catch (NoSuchFieldException e) {
ร—
171
                f = clazz.getDeclaredField("set");
ร—
172
            }
1โœ”
173
            f.setAccessible(true);
1โœ”
174
            setFiledOfInetAddressCachePolicy = f;
1โœ”
175

176
            try {
177
                f = clazz.getDeclaredField("propertyNegativeSet");
1โœ”
178
            } catch (NoSuchFieldException e) {
ร—
179
                f = clazz.getDeclaredField("negativeSet");
ร—
180
            }
1โœ”
181
            f.setAccessible(true);
1โœ”
182
            negativeSetOfInetAddressCachePolicy = f;
1โœ”
183
        }
1โœ”
184
    }
1โœ”
185

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;
1โœ”
194

195
        synchronized (InetAddressCacheUtilCommons.class) {
1โœ”
196
            // double check
197
            if (isNew != null) return isNew;
1โœ”
198

199
            try {
200
                InetAddress.class.getDeclaredField("expirySet");
1โœ”
201
                isNew = true;
1โœ”
202
            } catch (NoSuchFieldException e) {
1โœ”
203
                isNew = false;
1โœ”
204
            }
1โœ”
205

206
            return isNew;
1โœ”
207
        }
208
    }
209

210
    private InetAddressCacheUtilCommons() {
211
    }
212
}
STATUS ยท Troubleshooting ยท Open an Issue ยท Sales ยท Support ยท CAREERS ยท ENTERPRISE ยท START FREE ยท SCHEDULE DEMO
ANNOUNCEMENTS ยท TWITTER ยท TOS & SLA ยท Supported CI Services ยท What's a CI service? ยท Automated Testing

ยฉ 2025 Coveralls, Inc