jdk/src/share/classes/sun/net/spi/nameservice/dns/DNSNameService.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 6112 b9d1b10c662c
child 23333 b0af2c7c8c91
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
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 6112
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 sun.net.spi.nameservice.dns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.spi.NamingManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.dns.ResolverConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.net.spi.nameservice.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.action.*;
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 name service provider based on JNDI-DNS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class DNSNameService implements NameService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // List of domains specified by property
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    48
    private LinkedList<String> domainList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // JNDI-DNS URL for name servers specified via property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String nameProviderUrl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Per-thread soft cache of the last temporary context
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    54
    private static ThreadLocal<SoftReference<ThreadContext>> contextRef =
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    55
            new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Simple class to encapsulate the temporary context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static class ThreadContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        private DirContext dirCtxt;
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    60
        private List<String> nsList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    62
        public ThreadContext(DirContext dirCtxt, List<String> nsList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            this.dirCtxt = dirCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.nsList = nsList;
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 DirContext dirContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return dirCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    71
        public List<String> nameservers() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return nsList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Returns a per-thread DirContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private DirContext getTemporaryContext() throws NamingException {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    78
        SoftReference<ThreadContext> ref = contextRef.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        ThreadContext thrCtxt = null;
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    80
        List<String> nsList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // if no property specified we need to obtain the list of servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (nameProviderUrl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            nsList = ResolverConfiguration.open().nameservers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // if soft reference hasn't been gc'ed no property has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // specified then we need to check if the DNS configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        //
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
    91
        if ((ref != null) && ((thrCtxt = ref.get()) != null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (nameProviderUrl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (!thrCtxt.nameservers().equals(nsList)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    // DNS configuration has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    thrCtxt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // new thread context needs to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (thrCtxt == null) {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   102
            final Hashtable<String,Object> env = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            env.put("java.naming.factory.initial",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    "com.sun.jndi.dns.DnsContextFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // If no nameservers property specified we create provider URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // based on system configured name servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            String provUrl = nameProviderUrl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (provUrl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                provUrl = createProviderURL(nsList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (provUrl.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    throw new RuntimeException("bad nameserver configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            env.put("java.naming.provider.url", provUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // Need to create directory context in privileged block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // as JNDI-DNS needs to resolve the name servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            DirContext dirCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            try {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   123
                dirCtxt = java.security.AccessController.doPrivileged(
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   124
                        new java.security.PrivilegedExceptionAction<DirContext>() {
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   125
                            public DirContext run() throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                // Create the DNS context using NamingManager rather than using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                // the initial context constructor. This avoids having the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                // context constructor call itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                Context ctx = NamingManager.getInitialContext(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                if (!(ctx instanceof DirContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    return null; // cannot create a DNS context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                }
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   133
                                return (DirContext)ctx;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw (NamingException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // create new soft reference to our thread context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            thrCtxt = new ThreadContext(dirCtxt, nsList);
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 6112
diff changeset
   143
            contextRef.set(new SoftReference<ThreadContext>(thrCtxt));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return thrCtxt.dirContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Resolves the specified entry in DNS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Canonical name records are recursively resolved (to a maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * of 5 to avoid performance hit and potential CNAME loops).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param   ctx     JNDI directory context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param   name    name to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param   ids     record types to search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param   depth   call depth - pass as 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return  array list with results (will have at least on entry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws  UnknownHostException if lookup fails or other error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   164
    private ArrayList<String> resolve(final DirContext ctx, final String name,
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   165
                                      final String[] ids, int depth)
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   166
            throws UnknownHostException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   168
        ArrayList<String> results = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Attributes attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // do the query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   173
            attrs = java.security.AccessController.doPrivileged(
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   174
                    new java.security.PrivilegedExceptionAction<Attributes>() {
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   175
                        public Attributes run() throws NamingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            return ctx.getAttributes(name, ids);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new UnknownHostException(pae.getException().getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // non-requested type returned so enumeration is empty
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   184
        NamingEnumeration<? extends Attribute> ne = attrs.getAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!ne.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new UnknownHostException("DNS record not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // iterate through the returned attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        UnknownHostException uhe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            while (ne.hasMoreElements()) {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   193
                Attribute attr = ne.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                String attrID = attr.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 6112
diff changeset
   196
                for (NamingEnumeration<?> e = attr.getAll(); e.hasMoreElements();) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    String addr = (String)e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // for canoncical name records do recursive lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    // - also check for CNAME loops to avoid stack overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    if (attrID.equals("CNAME")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        if (depth > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            throw new UnknownHostException(name + ": possible CNAME loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            results.addAll(resolve(ctx, addr, ids, depth+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        } catch (UnknownHostException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            // canonical name can't be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            if (uhe == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                uhe = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        results.add(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } catch (NamingException nx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new UnknownHostException(nx.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // pending exception as canonical name could not be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (results.isEmpty() && uhe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw uhe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public DNSNameService() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // default domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        String domain = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            new GetPropertyAction("sun.net.spi.nameservice.domain"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (domain != null && domain.length() > 0) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 6112
diff changeset
   236
            domainList = new LinkedList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            domainList.add(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // name servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        String nameservers = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            new GetPropertyAction("sun.net.spi.nameservice.nameservers"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (nameservers != null && nameservers.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            nameProviderUrl = createProviderURL(nameservers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (nameProviderUrl.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                throw new RuntimeException("malformed nameservers property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // no property specified so check host DNS resolver configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // with at least one nameserver in dotted notation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            //
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   254
            List<String> nsList = ResolverConfiguration.open().nameservers();
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   255
            if (nsList.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                throw new RuntimeException("no nameservers provided");
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   257
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            boolean found = false;
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   259
            for (String addr: nsList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (IPAddressUtil.isIPv4LiteralAddress(addr) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    IPAddressUtil.isIPv6LiteralAddress(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new RuntimeException("bad nameserver configuration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // DNS records that we search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        String[] ids = {"A", "AAAA", "CNAME"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // first get directory context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        DirContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            ctx = getTemporaryContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } catch (NamingException nx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new Error(nx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 6112
diff changeset
   285
        ArrayList<String> results = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        UnknownHostException uhe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // If host already contains a domain name then just look it up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (host.indexOf('.') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                results = resolve(ctx, host, ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } catch (UnknownHostException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                uhe = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // Here we try to resolve the host using the domain suffix or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // the domain suffix search list. If the host cannot be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // using the domain suffix then we attempt devolution of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // the suffix - eg: if we are searching for "foo" and our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // domain suffix is "eng.sun.com" we will try to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // "foo.eng.sun.com" and "foo.sun.com".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // It's not normal to attempt devolation with domains on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // domain suffix search list - however as ResolverConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // doesn't distinguish domain or search list in the list it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // returns we approximate by doing devolution on the domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // suffix if the list has one entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (results == null) {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   310
            List<String> searchList = null;
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   311
            Iterator<String> i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            boolean usingSearchList = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (domainList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                i = domainList.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                searchList = ResolverConfiguration.open().searchlist();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (searchList.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    usingSearchList = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                i = searchList.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // iterator through each domain suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            while (i.hasNext()) {
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   326
                String parentDomain = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                while ((start = parentDomain.indexOf(".")) != -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                       && start < parentDomain.length() -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        results = resolve(ctx, host+"."+parentDomain, ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    } catch (UnknownHostException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        uhe = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        if (usingSearchList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        // devolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        parentDomain = parentDomain.substring(start+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (results != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // finally try the host if it doesn't have a domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (results == null && (host.indexOf('.') < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            results = resolve(ctx, host, ids, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // if not found then throw the (last) exception thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (results == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            assert uhe != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw uhe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * Convert the array list into a byte aray list - this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * filters out any invalid IPv4/IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        assert results.size() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        InetAddress[] addrs = new InetAddress[results.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        for (int i=0; i<results.size(); i++) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 6112
diff changeset
   368
            String addrString = results.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            byte addr[] = IPAddressUtil.textToNumericFormatV4(addrString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                addr = IPAddressUtil.textToNumericFormatV6(addrString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                addrs[count++] = InetAddress.getByAddress(host, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * If addresses are filtered then we need to resize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         * array. Additionally if all addresses are filtered then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * we throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new UnknownHostException(host + ": no valid DNS records");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (count < results.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            InetAddress[] tmp = new InetAddress[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            for (int i=0; i<count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                tmp[i] = addrs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            addrs = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Reverse lookup code. I.E: find a host name from an IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * IPv4 addresses are mapped in the IN-ADDR.ARPA. top domain, while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * IPv6 addresses can be in IP6.ARPA or IP6.INT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * In both cases the address has to be converted into a dotted form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public String getHostByAddr(byte[] addr) throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        String host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            String literalip = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            String[] ids = { "PTR" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            DirContext ctx;
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   409
            ArrayList<String> results = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                ctx = getTemporaryContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            } catch (NamingException nx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                throw new Error(nx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (addr.length == 4) { // IPv4 Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                for (int i = addr.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    literalip += (addr[i] & 0xff) +".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                literalip += "IN-ADDR.ARPA.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                results = resolve(ctx, literalip, ids, 0);
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   422
                host = results.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else if (addr.length == 16) { // IPv6 Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                 * Because RFC 3152 changed the root domain name for reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                 * lookups from IP6.INT. to IP6.ARPA., we need to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                 * both. I.E. first the new one, IP6.ARPA, then if it fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                 * the older one, IP6.INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                for (int i = addr.length-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    literalip += Integer.toHexString((addr[i] & 0x0f)) +"."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        +Integer.toHexString((addr[i] & 0xf0) >> 4) +".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                String ip6lit = literalip + "IP6.ARPA.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    results = resolve(ctx, ip6lit, ids, 0);
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   439
                    host = results.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    // IP6.ARPA lookup failed, let's try the older IP6.INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    ip6lit = literalip + "IP6.INT.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    results = resolve(ctx, ip6lit, ids, 0);
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   447
                    host = results.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new UnknownHostException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // Either we couldn't find it or the address was neither IPv4 or IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (host == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw new UnknownHostException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // remove trailing dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (host.endsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            host = host.substring(0, host.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    // ---------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private static void appendIfLiteralAddress(String addr, StringBuffer sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (IPAddressUtil.isIPv4LiteralAddress(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            sb.append("dns://" + addr + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (IPAddressUtil.isIPv6LiteralAddress(addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                sb.append("dns://[" + addr + "] ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @return String containing the JNDI-DNS provider URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *         corresponding to the supplied List of nameservers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   480
    private static String createProviderURL(List<String> nsList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        StringBuffer sb = new StringBuffer();
6112
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   482
        for (String s: nsList) {
b9d1b10c662c 6969683: Generify ResolverConfiguration codes
weijun
parents: 5506
diff changeset
   483
            appendIfLiteralAddress(s, sb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @return String containing the JNDI-DNS provider URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *         corresponding to the list of nameservers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *         contained in the provided str.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private static String createProviderURL(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        StringTokenizer st = new StringTokenizer(str, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            appendIfLiteralAddress(st.nextToken(), sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
}