jdk/src/share/classes/com/sun/jndi/dns/DnsContextFactory.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 6859 0b0cdd787307
child 6112 b9d1b10c662c
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2004, 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:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public Context getInitialContext(Hashtable<?,?> env) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (env == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            env = new Hashtable(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return urlToContext(getInitCtxUrl(env), env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static DnsContext getContext(String domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                        String[] servers, Hashtable<?,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return new DnsContext(domain, servers, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * "urls" are used to determine the servers, but any domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * components are overridden by "domain".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static DnsContext getContext(String domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                        DnsUrl[] urls, Hashtable env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String[] servers = serversForUrls(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        DnsContext ctx = getContext(domain, servers, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (platformServersUsed(urls)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            ctx.setProviderUrl(constructProviderUrl(domain, servers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Public for use by product test suite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static boolean platformServersAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return !ResolverConfiguration.open().nameservers().isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static Context urlToContext(String url, Hashtable env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        DnsUrl[] urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            urls = DnsUrl.fromList(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new ConfigurationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (urls.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    "Invalid DNS pseudo-URL(s): " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        String domain = urls[0].getDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // If multiple urls, all must have the same domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 1; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (!domain.equalsIgnoreCase(urls[i].getDomain())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        "Conflicting domains: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return getContext(domain, urls, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Returns all the servers specified in a set of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * If a URL has no host (or port), the servers configured on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * underlying platform are used if possible.  If no configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * servers can be found, then fall back to the old behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * using "localhost".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * There must be at least one URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static String[] serversForUrls(DnsUrl[] urls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (urls.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new ConfigurationException("DNS pseudo-URL required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        List servers = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            String server = urls[i].getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            int port = urls[i].getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (server == null && port < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // No server or port given, so look to underlying platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // ResolverConfiguration does some limited caching, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // following is reasonably efficient even if called rapid-fire.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                List platformServers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    ResolverConfiguration.open().nameservers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (!platformServers.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    servers.addAll(platformServers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    continue;  // on to next URL (if any, which is unlikely)
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
            if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                server = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            servers.add((port < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        ? server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        : server + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return (String[]) servers.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                        new String[servers.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns true if serversForUrls(urls) would make use of servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * from the underlying platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static boolean platformServersUsed(DnsUrl[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (!platformServersAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (urls[i].getHost() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                urls[i].getPort() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns a value for the PROVIDER_URL property (space-separated URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Strings) that reflects the given domain and servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Each server is of the form "server[:port]".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * There must be at least one server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * IPv6 literal host names include delimiting brackets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private static String constructProviderUrl(String domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                               String[] servers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        String path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!domain.equals(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                path = "/" + UrlUtil.encode(domain, "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // assert false : "ISO-Latin-1 charset unavailable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        for (int i = 0; i < servers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                buf.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            buf.append("dns://").append(servers[i]).append(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Reads environment to find URL(s) of initial context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Default URL is "dns:".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private static String getInitCtxUrl(Hashtable env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        String url = (String) env.get(Context.PROVIDER_URL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return ((url != null) ? url : DEFAULT_URL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}