author | redestad |
Wed, 17 Jul 2019 12:35:46 +0200 | |
changeset 55693 | 9a97b1393e72 |
parent 52499 | 768b1c612100 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55693
9a97b1393e72
8227587: Add internal privileged System.loadLibrary
redestad
parents:
52499
diff
changeset
|
2 |
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.dns; |
|
27 |
||
28 |
import java.util.List; |
|
29 |
import java.util.LinkedList; |
|
30 |
import java.util.StringTokenizer; |
|
31 |
||
32 |
/* |
|
33 |
* An implementation of sun.net.ResolverConfiguration for Windows. |
|
34 |
*/ |
|
35 |
||
36 |
public class ResolverConfigurationImpl |
|
37 |
extends ResolverConfiguration |
|
38 |
{ |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47216
diff
changeset
|
39 |
// Lock held whilst loading configuration or checking |
2 | 40 |
private static Object lock = new Object(); |
41 |
||
42 |
// Resolver options |
|
43 |
private final Options opts; |
|
44 |
||
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47216
diff
changeset
|
45 |
// Addresses have changed |
2 | 46 |
private static boolean changed = false; |
47 |
||
48 |
// Time of last refresh. |
|
49 |
private static long lastRefresh = -1; |
|
50 |
||
51 |
// Cache timeout (120 seconds) - should be converted into property |
|
52 |
// or configured as preference in the future. |
|
53 |
private static final int TIMEOUT = 120000; |
|
54 |
||
55 |
// DNS suffix list and name servers populated by native method |
|
56 |
private static String os_searchlist; |
|
57 |
private static String os_nameservers; |
|
58 |
||
59 |
// Cached lists |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
60 |
private static LinkedList<String> searchlist; |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
61 |
private static LinkedList<String> nameservers; |
2 | 62 |
|
63 |
// Parse string that consists of token delimited by space or commas |
|
64 |
// and return LinkedHashMap |
|
6112 | 65 |
private LinkedList<String> stringToList(String str) { |
66 |
LinkedList<String> ll = new LinkedList<>(); |
|
2 | 67 |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47216
diff
changeset
|
68 |
// comma and space are valid delimiters |
2 | 69 |
StringTokenizer st = new StringTokenizer(str, ", "); |
70 |
while (st.hasMoreTokens()) { |
|
71 |
String s = st.nextToken(); |
|
72 |
if (!ll.contains(s)) { |
|
73 |
ll.add(s); |
|
74 |
} |
|
75 |
} |
|
76 |
return ll; |
|
77 |
} |
|
78 |
||
79 |
// Load DNS configuration from OS |
|
80 |
||
81 |
private void loadConfig() { |
|
82 |
assert Thread.holdsLock(lock); |
|
83 |
||
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
47216
diff
changeset
|
84 |
// if address have changed then DNS probably changed as well; |
2 | 85 |
// otherwise check if cached settings have expired. |
86 |
// |
|
87 |
if (changed) { |
|
88 |
changed = false; |
|
89 |
} else { |
|
90 |
if (lastRefresh >= 0) { |
|
91 |
long currTime = System.currentTimeMillis(); |
|
92 |
if ((currTime - lastRefresh) < TIMEOUT) { |
|
93 |
return; |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
// load DNS configuration, update timestamp, create |
|
99 |
// new HashMaps from the loaded configuration |
|
100 |
// |
|
101 |
loadDNSconfig0(); |
|
102 |
||
103 |
lastRefresh = System.currentTimeMillis(); |
|
104 |
searchlist = stringToList(os_searchlist); |
|
105 |
nameservers = stringToList(os_nameservers); |
|
106 |
os_searchlist = null; // can be GC'ed |
|
107 |
os_nameservers = null; |
|
108 |
} |
|
109 |
||
110 |
ResolverConfigurationImpl() { |
|
111 |
opts = new OptionsImpl(); |
|
112 |
} |
|
113 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
114 |
@SuppressWarnings("unchecked") // clone() |
6112 | 115 |
public List<String> searchlist() { |
2 | 116 |
synchronized (lock) { |
117 |
loadConfig(); |
|
118 |
||
119 |
// List is mutable so return a shallow copy |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
120 |
return (List<String>)searchlist.clone(); |
2 | 121 |
} |
122 |
} |
|
123 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
124 |
@SuppressWarnings("unchecked") // clone() |
6112 | 125 |
public List<String> nameservers() { |
2 | 126 |
synchronized (lock) { |
127 |
loadConfig(); |
|
128 |
||
129 |
// List is mutable so return a shallow copy |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
130 |
return (List<String>)nameservers.clone(); |
2 | 131 |
} |
132 |
} |
|
133 |
||
134 |
public Options options() { |
|
135 |
return opts; |
|
136 |
} |
|
137 |
||
138 |
// --- Address Change Listener |
|
139 |
||
140 |
static class AddressChangeListener extends Thread { |
|
141 |
public void run() { |
|
142 |
for (;;) { |
|
143 |
// wait for configuration to change |
|
144 |
if (notifyAddrChange0() != 0) |
|
145 |
return; |
|
146 |
synchronized (lock) { |
|
147 |
changed = true; |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
||
154 |
// --- Native methods -- |
|
155 |
||
156 |
static native void init0(); |
|
157 |
||
158 |
static native void loadDNSconfig0(); |
|
159 |
||
160 |
static native int notifyAddrChange0(); |
|
161 |
||
162 |
static { |
|
55693
9a97b1393e72
8227587: Add internal privileged System.loadLibrary
redestad
parents:
52499
diff
changeset
|
163 |
jdk.internal.loader.BootLoader.loadLibrary("net"); |
2 | 164 |
init0(); |
165 |
||
166 |
// start the address listener thread |
|
167 |
AddressChangeListener thr = new AddressChangeListener(); |
|
168 |
thr.setDaemon(true); |
|
169 |
thr.start(); |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Implementation of {@link ResolverConfiguration.Options} |
|
175 |
*/ |
|
176 |
class OptionsImpl extends ResolverConfiguration.Options { |
|
177 |
} |