jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java
author xuelei
Tue, 21 Feb 2012 05:44:29 -0800
changeset 11913 be61e1597cc6
parent 10324 e28265130e4f
child 24969 afa6934dd8e8
permissions -rw-r--r--
7147407: remove never used debug code in DnsClient.java Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
     2
 * Copyright (c) 2000, 2011, 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 com.sun.jndi.dns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jndi.toolkit.url.UrlUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.net.dns.ResolverConfiguration;       // available since 1.4.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A DnsContextFactory serves as the initial context factory for DNS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p> When an initial context is being created, the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * property "java.naming.provider.url" should contain a DNS pseudo-URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * (see DnsUrl) or a space-separated list of them.  Multiple URLs must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * all have the same domain value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * If the property is not set, the default "dns:" is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class DnsContextFactory implements InitialContextFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final String DEFAULT_URL = "dns:";
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
    57
    private static final int DEFAULT_PORT = 53;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public Context getInitialContext(Hashtable<?,?> env) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (env == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
    62
            env = new Hashtable<>(5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return urlToContext(getInitCtxUrl(env), env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static DnsContext getContext(String domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                        String[] servers, Hashtable<?,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return new DnsContext(domain, servers, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * "urls" are used to determine the servers, but any domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * components are overridden by "domain".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static DnsContext getContext(String domain,
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
    78
                                        DnsUrl[] urls, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String[] servers = serversForUrls(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        DnsContext ctx = getContext(domain, servers, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (platformServersUsed(urls)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ctx.setProviderUrl(constructProviderUrl(domain, servers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Public for use by product test suite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static boolean platformServersAvailable() {
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
    93
        return !filterNameServers(
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
    94
                    ResolverConfiguration.open().nameservers(), true
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
    95
                ).isEmpty();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
    98
    private static Context urlToContext(String url, Hashtable<?,?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        DnsUrl[] urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            urls = DnsUrl.fromList(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new ConfigurationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (urls.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    "Invalid DNS pseudo-URL(s): " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        String domain = urls[0].getDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // If multiple urls, all must have the same domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i = 1; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (!domain.equalsIgnoreCase(urls[i].getDomain())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        "Conflicting domains: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return getContext(domain, urls, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Returns all the servers specified in a set of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * If a URL has no host (or port), the servers configured on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * underlying platform are used if possible.  If no configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * servers can be found, then fall back to the old behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * using "localhost".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * There must be at least one URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static String[] serversForUrls(DnsUrl[] urls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (urls.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new ConfigurationException("DNS pseudo-URL required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   138
        List<String> servers = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String server = urls[i].getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int port = urls[i].getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (server == null && port < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // No server or port given, so look to underlying platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // ResolverConfiguration does some limited caching, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // following is reasonably efficient even if called rapid-fire.
6879
chegar
parents: 6112 6859
diff changeset
   148
                List<String> platformServers = filterNameServers(
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   149
                    ResolverConfiguration.open().nameservers(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if (!platformServers.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    servers.addAll(platformServers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    continue;  // on to next URL (if any, which is unlikely)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                server = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            servers.add((port < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        ? server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        : server + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   163
        return servers.toArray(new String[servers.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns true if serversForUrls(urls) would make use of servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * from the underlying platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static boolean platformServersUsed(DnsUrl[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (!platformServersAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (urls[i].getHost() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                urls[i].getPort() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns a value for the PROVIDER_URL property (space-separated URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Strings) that reflects the given domain and servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Each server is of the form "server[:port]".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * There must be at least one server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * IPv6 literal host names include delimiting brackets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static String constructProviderUrl(String domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                               String[] servers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        String path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!domain.equals(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                path = "/" + UrlUtil.encode(domain, "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // assert false : "ISO-Latin-1 charset unavailable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        for (int i = 0; i < servers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                buf.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            buf.append("dns://").append(servers[i]).append(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Reads environment to find URL(s) of initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Default URL is "dns:".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   215
    private static String getInitCtxUrl(Hashtable<?,?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        String url = (String) env.get(Context.PROVIDER_URL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return ((url != null) ? url : DEFAULT_URL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   219
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   220
    /**
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   221
     * Removes any DNS server that's not permitted to access
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   222
     * @param input the input server[:port] list, must not be null
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   223
     * @param oneIsEnough return output once there exists one ok
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   224
     * @return the filtered list, all non-permitted input removed
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   225
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   226
    private static List<String> filterNameServers(List<String> input, boolean oneIsEnough) {
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   227
        SecurityManager security = System.getSecurityManager();
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   228
        if (security == null || input == null || input.isEmpty()) {
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   229
            return input;
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   230
        } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   231
            List<String> output = new ArrayList<>();
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   232
            for (String platformServer: input) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   233
                int colon = platformServer.indexOf(':',
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   234
                        platformServer.indexOf(']') + 1);
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   235
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   236
                int p = (colon < 0)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   237
                    ? DEFAULT_PORT
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   238
                    : Integer.parseInt(
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   239
                        platformServer.substring(colon + 1));
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   240
                String s = (colon < 0)
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   241
                    ? platformServer
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   242
                    : platformServer.substring(0, colon);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   243
                try {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   244
                    security.checkConnect(s, p);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   245
                    output.add(platformServer);
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   246
                    if (oneIsEnough) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   247
                        return output;
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   248
                    }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   249
                } catch (SecurityException se) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 6879
diff changeset
   250
                    continue;
6859
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   251
                }
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   252
            }
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   253
            return output;
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   254
        }
0b0cdd787307 6957564: Disclosure of DNS server IP address
weijun
parents: 5506
diff changeset
   255
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}