src/jdk.dns.client/windows/classes/jdk/dns/conf/DnsResolverConfiguration.java
branchaefimov-dns-client-branch
changeset 59100 b92aac38b046
parent 58870 35c438a6d45c
equal deleted inserted replaced
59099:fcdb8e7ead8f 59100:b92aac38b046
    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<>();
    65             }
    68             }
    66         }
    69         }
    67         return ll;
    70         return ll;
    68     }
    71     }
    69 
    72 
       
    73     public static String getDefaultHostsFileLocation() {
       
    74         return Paths.get(System.getenv("SystemRoot"))
       
    75                 .resolve("System32")
       
    76                 .resolve("drivers")
       
    77                 .resolve("etc")
       
    78                 .resolve("hosts")
       
    79                 .toString();
       
    80     }
       
    81 
    70     // Load DNS configuration from OS
    82     // Load DNS configuration from OS
    71 
    83 
    72     private void loadConfig() {
    84     private void loadConfig() {
    73         assert lock.isHeldByCurrentThread();
    85         assert lock.isHeldByCurrentThread();
    74 
    86 
    77         //
    89         //
    78         if (changed) {
    90         if (changed) {
    79             changed = false;
    91             changed = false;
    80         } else {
    92         } else {
    81             if (lastRefresh >= 0) {
    93             if (lastRefresh >= 0) {
    82                 long currTime = System.currentTimeMillis();
    94                 long currTime = System.nanoTime();
    83                 if ((currTime - lastRefresh) < TIMEOUT) {
    95                 if ((currTime - lastRefresh) < TIMEOUT) {
    84                     return;
    96                     return;
    85                 }
    97                 }
    86             }
    98             }
    87         }
    99         }
    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();
   114         }
   131         }
   115     }
   132     }
   116 
   133 
   117     @SuppressWarnings("unchecked") // clone()
   134     @SuppressWarnings("unchecked") // clone()
   118     public List<String> nameservers() {
   135     public List<String> nameservers() {
   119         lock.lock()
   136         lock.lock();
   120         try {
   137         try {
   121             loadConfig();
   138             loadConfig();
   122 
   139 
   123             // List is mutable so return a shallow copy
   140             // List is mutable so return a shallow copy
   124             return (List<String>) nameservers.clone();
   141             return (List<String>) nameservers.clone();
       
   142         } finally {
       
   143             lock.unlock();
       
   144         }
       
   145     }
       
   146 
       
   147     public String domain() {
       
   148         lock.lock();
       
   149         try {
       
   150             loadConfig();
       
   151             return domain;
   125         } finally {
   152         } finally {
   126             lock.unlock();
   153             lock.unlock();
   127         }
   154         }
   128     }
   155     }
   129 
   156