~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/net/dns/ResolverConfigurationImpl.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
 
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
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
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
 *
 
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.
 
24
 */
 
25
 
 
26
package sun.net.dns;
 
27
 
 
28
import java.util.List;
 
29
import java.util.LinkedList;
 
30
import java.util.StringTokenizer;
 
31
import java.io.IOException;
 
32
import cli.System.Net.NetworkInformation.IPAddressCollection;
 
33
import cli.System.Net.NetworkInformation.IPInterfaceProperties;
 
34
import cli.System.Net.NetworkInformation.NetworkInterface;
 
35
 
 
36
/*
 
37
 * An implementation of sun.net.ResolverConfiguration for Windows.
 
38
 */
 
39
 
 
40
public class ResolverConfigurationImpl
 
41
    extends ResolverConfiguration
 
42
{
 
43
    // Lock helds whilst loading configuration or checking
 
44
    private static Object lock = new Object();
 
45
 
 
46
    // Resolver options
 
47
    private final Options opts;
 
48
 
 
49
    // Addreses have changed
 
50
    private static boolean changed = false;
 
51
 
 
52
    // Time of last refresh.
 
53
    private static long lastRefresh = -1;
 
54
 
 
55
    // Cache timeout (120 seconds) - should be converted into property
 
56
    // or configured as preference in the future.
 
57
    private static final int TIMEOUT = 120000;
 
58
 
 
59
    // DNS suffix list and name servers populated by native method
 
60
    private static String os_searchlist;
 
61
    private static String os_nameservers;
 
62
 
 
63
    // Cached lists
 
64
    private static LinkedList searchlist;
 
65
    private static LinkedList nameservers;
 
66
 
 
67
    // Parse string that consists of token delimited by space or commas
 
68
    // and return LinkedHashMap
 
69
    private LinkedList<String> stringToList(String str) {
 
70
        LinkedList<String> ll = new LinkedList<>();
 
71
 
 
72
        // comma and space are valid delimites
 
73
        StringTokenizer st = new StringTokenizer(str, ", ");
 
74
        while (st.hasMoreTokens()) {
 
75
            String s = st.nextToken();
 
76
            if (!ll.contains(s)) {
 
77
                ll.add(s);
 
78
            }
 
79
        }
 
80
        return ll;
 
81
    }
 
82
 
 
83
    // Load DNS configuration from OS
 
84
 
 
85
    private void loadConfig() {
 
86
        assert Thread.holdsLock(lock);
 
87
 
 
88
        // if address have changed then DNS probably changed aswell;
 
89
        // otherwise check if cached settings have expired.
 
90
        //
 
91
        if (changed) {
 
92
            changed = false;
 
93
        } else {
 
94
            if (lastRefresh >= 0) {
 
95
                long currTime = System.currentTimeMillis();
 
96
                if ((currTime - lastRefresh) < TIMEOUT) {
 
97
                    return;
 
98
                }
 
99
            }
 
100
        }
 
101
 
 
102
        // load DNS configuration, update timestamp, create
 
103
        // new HashMaps from the loaded configuration
 
104
        //
 
105
        loadDNSconfig0();
 
106
 
 
107
        lastRefresh = System.currentTimeMillis();
 
108
        searchlist = stringToList(os_searchlist);
 
109
        nameservers = stringToList(os_nameservers);
 
110
        os_searchlist = null;                       // can be GC'ed
 
111
        os_nameservers = null;
 
112
    }
 
113
 
 
114
    ResolverConfigurationImpl() {
 
115
        opts = new OptionsImpl();
 
116
    }
 
117
 
 
118
    public List<String> searchlist() {
 
119
        synchronized (lock) {
 
120
            loadConfig();
 
121
 
 
122
            // List is mutable so return a shallow copy
 
123
            return (List)searchlist.clone();
 
124
        }
 
125
    }
 
126
 
 
127
    public List<String> nameservers() {
 
128
        synchronized (lock) {
 
129
            loadConfig();
 
130
 
 
131
            // List is mutable so return a shallow copy
 
132
            return (List)nameservers.clone();
 
133
         }
 
134
    }
 
135
 
 
136
    public Options options() {
 
137
        return opts;
 
138
    }
 
139
 
 
140
    // --- Address Change Listener
 
141
 
 
142
    static class AddressChangeListener extends Thread {
 
143
        public void run() {
 
144
            for (;;) {
 
145
                // wait for configuration to change
 
146
                if (notifyAddrChange0() != 0)
 
147
                    return;
 
148
                synchronized (lock) {
 
149
                    changed = true;
 
150
                }
 
151
            }
 
152
        }
 
153
    }
 
154
 
 
155
 
 
156
    // --- Native methods --
 
157
 
 
158
    static void init0() {
 
159
    }
 
160
 
 
161
    static void loadDNSconfig0() {
 
162
        String searchlist = "";
 
163
        String nameservers = "";
 
164
        for (NetworkInterface iface : NetworkInterface.GetAllNetworkInterfaces()) {
 
165
            IPInterfaceProperties props = iface.GetIPProperties();
 
166
            IPAddressCollection addresses = props.get_DnsAddresses();
 
167
            for (int i = 0; i < addresses.get_Count(); i++) {
 
168
                cli.System.Net.IPAddress addr = addresses.get_Item(i);
 
169
                // no IPv6 support
 
170
                if (addr.get_AddressFamily().Value == cli.System.Net.Sockets.AddressFamily.InterNetwork) {
 
171
                    nameservers = strAppend(nameservers, addr.toString());
 
172
                }
 
173
            }
 
174
            try {
 
175
                if (false) throw new cli.System.PlatformNotSupportedException();
 
176
                searchlist = strAppend(searchlist, props.get_DnsSuffix());
 
177
            }
 
178
            catch (cli.System.PlatformNotSupportedException _) {
 
179
            }
 
180
        }
 
181
        os_searchlist = searchlist;
 
182
        os_nameservers = nameservers;
 
183
    }
 
184
    
 
185
    private static String strAppend(String s, String app) {
 
186
        if (s.equals("")) {
 
187
            return app;
 
188
        }
 
189
        if (app.equals("")) {
 
190
            return s;
 
191
        }
 
192
        return s + " " + app;
 
193
    }
 
194
 
 
195
    static int notifyAddrChange0() {
 
196
        // TODO we could use System.Net.NetworkInformation.NetworkChange to detect changes
 
197
        return -1;
 
198
    }
 
199
 
 
200
    static {
 
201
        java.security.AccessController.doPrivileged(
 
202
            new sun.security.action.LoadLibraryAction("net"));
 
203
        init0();
 
204
 
 
205
        // start the address listener thread
 
206
        AddressChangeListener thr = new AddressChangeListener();
 
207
        thr.setDaemon(true);
 
208
        thr.start();
 
209
    }
 
210
}
 
211
 
 
212
/**
 
213
 * Implementation of {@link ResolverConfiguration.Options}
 
214
 */
 
215
class OptionsImpl extends ResolverConfiguration.Options {
 
216
}