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