23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package jdk.dns.conf; |
26 package jdk.dns.conf; |
27 |
27 |
|
28 import java.nio.file.Paths; |
28 import java.util.LinkedList; |
29 import java.util.LinkedList; |
29 import java.util.List; |
30 import java.util.List; |
30 import java.util.StringTokenizer; |
31 import java.util.StringTokenizer; |
|
32 import java.util.concurrent.locks.ReentrantLock; |
31 |
33 |
32 public class DnsResolverConfiguration { |
34 public class DnsResolverConfiguration { |
33 // Lock held whilst loading configuration or checking |
35 // Lock held whilst loading configuration or checking |
34 private static ReentrantLock lock = new ReentrantLock(); |
36 private static ReentrantLock lock = new ReentrantLock(); |
35 |
37 |
37 private static boolean changed = false; |
39 private static boolean changed = false; |
38 |
40 |
39 // Time of last refresh. |
41 // Time of last refresh. |
40 private static long lastRefresh = -1; |
42 private static long lastRefresh = -1; |
41 |
43 |
42 // Cache timeout (120 seconds) - should be converted into property |
44 // Cache timeout (120 seconds in nanoseconds) - should be converted into property |
43 // or configured as preference in the future. |
45 // or configured as preference in the future. |
44 private static final int TIMEOUT = 120000; |
46 private static final long TIMEOUT = 120_000_000_000L; |
45 |
47 |
46 // DNS suffix list and name servers populated by native method |
48 // DNS suffix list and name servers populated by native method |
47 private static String os_searchlist; |
49 private static String os_searchlist; |
48 private static String os_nameservers; |
50 private static String os_nameservers; |
49 |
51 |
50 // Cached lists |
52 // Cached lists |
51 private static LinkedList<String> searchlist; |
53 private static LinkedList<String> searchlist; |
52 private static LinkedList<String> nameservers; |
54 private static LinkedList<String> nameservers; |
|
55 private volatile String domain = ""; |
53 |
56 |
54 // Parse string that consists of token delimited by space or commas |
57 // Parse string that consists of token delimited by space or commas |
55 // and return LinkedHashMap |
58 // and return LinkedHashMap |
56 private LinkedList<String> stringToList(String str) { |
59 private LinkedList<String> stringToList(String str) { |
57 LinkedList<String> ll = new LinkedList<>(); |
60 LinkedList<String> ll = new LinkedList<>(); |
89 // load DNS configuration, update timestamp, create |
101 // load DNS configuration, update timestamp, create |
90 // new HashMaps from the loaded configuration |
102 // new HashMaps from the loaded configuration |
91 // |
103 // |
92 loadDNSconfig0(); |
104 loadDNSconfig0(); |
93 |
105 |
94 lastRefresh = System.currentTimeMillis(); |
106 lastRefresh = System.nanoTime(); |
95 searchlist = stringToList(os_searchlist); |
107 searchlist = stringToList(os_searchlist); |
|
108 if (searchlist.size() > 0) { |
|
109 domain = searchlist.get(0); |
|
110 } else { |
|
111 domain = ""; |
|
112 } |
96 nameservers = stringToList(os_nameservers); |
113 nameservers = stringToList(os_nameservers); |
97 os_searchlist = null; // can be GC'ed |
114 os_searchlist = null; // can be GC'ed |
98 os_nameservers = null; |
115 os_nameservers = null; |
99 } |
116 } |
100 |
117 |
101 DnsResolverConfiguration() { |
118 public DnsResolverConfiguration() { |
102 } |
119 } |
103 |
120 |
104 @SuppressWarnings("unchecked") // clone() |
121 @SuppressWarnings("unchecked") // clone() |
105 public List<String> searchlist() { |
122 public List<String> searchlist() { |
106 lock.lock(); |
123 lock.lock(); |