Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/net/dns/ResolverConfigurationImpl.java
32288 views
/*1* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.dns;2627import java.util.List;28import java.util.LinkedList;29import java.util.StringTokenizer;3031/*32* An implementation of sun.net.ResolverConfiguration for Windows.33*/3435public class ResolverConfigurationImpl36extends ResolverConfiguration37{38// Lock helds whilst loading configuration or checking39private static Object lock = new Object();4041// Resolver options42private final Options opts;4344// Addreses have changed45private static boolean changed = false;4647// Time of last refresh.48private static long lastRefresh = -1;4950// Cache timeout (120 seconds) - should be converted into property51// or configured as preference in the future.52private static final int TIMEOUT = 120000;5354// DNS suffix list and name servers populated by native method55private static String os_searchlist;56private static String os_nameservers;5758// Cached lists59private static LinkedList<String> searchlist;60private static LinkedList<String> nameservers;6162// Parse string that consists of token delimited by space or commas63// and return LinkedHashMap64private LinkedList<String> stringToList(String str) {65LinkedList<String> ll = new LinkedList<>();6667// comma and space are valid delimites68StringTokenizer st = new StringTokenizer(str, ", ");69while (st.hasMoreTokens()) {70String s = st.nextToken();71if (!ll.contains(s)) {72ll.add(s);73}74}75return ll;76}7778// Load DNS configuration from OS7980private void loadConfig() {81assert Thread.holdsLock(lock);8283// if address have changed then DNS probably changed aswell;84// otherwise check if cached settings have expired.85//86if (changed) {87changed = false;88} else {89if (lastRefresh >= 0) {90long currTime = System.currentTimeMillis();91if ((currTime - lastRefresh) < TIMEOUT) {92return;93}94}95}9697// load DNS configuration, update timestamp, create98// new HashMaps from the loaded configuration99//100loadDNSconfig0();101102lastRefresh = System.currentTimeMillis();103searchlist = stringToList(os_searchlist);104nameservers = stringToList(os_nameservers);105os_searchlist = null; // can be GC'ed106os_nameservers = null;107}108109ResolverConfigurationImpl() {110opts = new OptionsImpl();111}112113@SuppressWarnings("unchecked") // clone()114public List<String> searchlist() {115synchronized (lock) {116loadConfig();117118// List is mutable so return a shallow copy119return (List<String>)searchlist.clone();120}121}122123@SuppressWarnings("unchecked") // clone()124public List<String> nameservers() {125synchronized (lock) {126loadConfig();127128// List is mutable so return a shallow copy129return (List<String>)nameservers.clone();130}131}132133public Options options() {134return opts;135}136137// --- Address Change Listener138139static class AddressChangeListener extends Thread {140public void run() {141for (;;) {142// wait for configuration to change143if (notifyAddrChange0() != 0)144return;145synchronized (lock) {146changed = true;147}148}149}150}151152153// --- Native methods --154155static native void init0();156157static native void loadDNSconfig0();158159static native int notifyAddrChange0();160161static {162java.security.AccessController.doPrivileged(163new java.security.PrivilegedAction<Void>() {164public Void run() {165System.loadLibrary("net");166return null;167}168});169init0();170171// start the address listener thread172AddressChangeListener thr = new AddressChangeListener();173thr.setDaemon(true);174thr.start();175}176}177178/**179* Implementation of {@link ResolverConfiguration.Options}180*/181class OptionsImpl extends ResolverConfiguration.Options {182}183184185