jdk/src/share/classes/com/sun/jndi/ldap/ServiceLocator.java
author prappo
Fri, 01 Aug 2014 22:32:51 +0100
changeset 25808 e113d0a0fde0
parent 24969 afa6934dd8e8
permissions -rw-r--r--
8054158: Fix typos in JNDI-related packages Reviewed-by: rriggs, 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: 5506
diff changeset
     2
 * Copyright (c) 2002, 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.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.StringTokenizer;
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.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.naming.spi.NamingManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.naming.ldap.LdapName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.naming.ldap.Rdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class discovers the location of LDAP services by querying DNS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * See http://www.ietf.org/internet-drafts/draft-ietf-ldapext-locate-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class ServiceLocator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final String SRV_RR = "SRV";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final String[] SRV_RR_ATTR = new String[]{SRV_RR};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private ServiceLocator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Processes a sequence of RDNs having a DC attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The special RDN "DC=." denotes the root of the domain tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * RDN "DC=." all reset the domain name and processing continues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param dn A string distinguished name (RFC 2253).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @return A domain name or null if none can be derived.
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 24969
diff changeset
    65
     * @throw InvalidNameException If the distinguished name is invalid.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static String mapDnToDomainName(String dn) throws InvalidNameException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (dn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 10324
diff changeset
    71
        StringBuilder domain = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        LdapName ldapName = new LdapName(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // process RDNs left-to-right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        //List<Rdn> rdnList = ldapName.getRdns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    77
        List<Rdn> rdnList = ldapName.getRdns();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (int i = rdnList.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            //Rdn rdn = rdnList.get(i);
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    80
            Rdn rdn = rdnList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // single-valued RDN with a DC attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if ((rdn.size() == 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                ("dc".equalsIgnoreCase(rdn.getType()) )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                Object attrval = rdn.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (attrval instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    if (attrval.equals(".") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        (domain.length() == 1 && domain.charAt(0) == '.')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        domain.setLength(0); // reset (when current or previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                             //        RDN value is "DC=.")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    if (domain.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        domain.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    domain.append(attrval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    domain.setLength(0); // reset (when binary-valued attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                domain.setLength(0); // reset (when multi-valued RDN or non-DC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return (domain.length() != 0) ? domain.toString() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Locates the LDAP service for a given domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Queries DNS for a list of LDAP Service Location Records (SRV) for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * given domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param domainName A string domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param environment The possibly null environment of the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return An ordered list of hostports for the LDAP service or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *         the service has not been located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   116
    static String[] getLdapService(String domainName, Hashtable<?,?> environment) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (domainName == null || domainName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        String dnsUrl = "dns:///_ldap._tcp." + domainName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String[] hostports = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
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 (when processing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // argument in the getAttributes call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            Context ctx = NamingManager.getURLContext("dns", environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (!(ctx instanceof DirContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                return null; // cannot create a DNS context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Attributes attrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                ((DirContext)ctx).getAttributes(dnsUrl, SRV_RR_ATTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (attrs != null && ((attr = attrs.get(SRV_RR)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                int numValues = attr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                int numRecords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                SrvRecord[] srvRecords = new SrvRecord[numValues];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // create the service records
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                while (i < numValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        srvRecords[j] = new SrvRecord((String) attr.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        // ignore bad value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                numRecords = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // trim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (numRecords < numValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    SrvRecord[] trimmed = new SrvRecord[numRecords];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    System.arraycopy(srvRecords, 0, trimmed, 0, numRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    srvRecords = trimmed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                // Sort the service records in ascending order of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                // priority value. For records with equal priority, move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // those with weight 0 to the top of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (numRecords > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    Arrays.sort(srvRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                // extract the host and port number from each service record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                hostports = extractHostports(srvRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return hostports;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Extract hosts and port numbers from a list of SRV records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * An array of hostports is returned or null if none were found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static String[] extractHostports(SrvRecord[] srvRecords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String[] hostports = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int head = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int tail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int sublistLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (int i = 0; i < srvRecords.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (hostports == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                hostports = new String[srvRecords.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // find the head and tail of the list of records having the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // priority value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            head = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            while (i < srvRecords.length - 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                srvRecords[i].priority == srvRecords[i + 1].priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            tail = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // select hostports from the sublist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            sublistLength = (tail - head) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (int j = 0; j < sublistLength; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                hostports[k++] = selectHostport(srvRecords, head, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return hostports;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Randomly select a service record in the range [head, tail] and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * its hostport value. Follows the algorithm in RFC 2782.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private static String selectHostport(SrvRecord[] srvRecords, int head,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            int tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (head == tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return srvRecords[head].hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // compute the running sum for records between head and tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (int i = head; i <= tail; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (srvRecords[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                sum += srvRecords[i].weight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                srvRecords[i].sum = sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        String hostport = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // If all records have zero weight, select first available one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // otherwise, randomly select a record according to its weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int target = (sum == 0 ? 0 : random.nextInt(sum + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (int i = head; i <= tail; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (srvRecords[i] != null && srvRecords[i].sum >= target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                hostport = srvRecords[i].hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                srvRecords[i] = null; // make this record unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * This class holds a DNS service (SRV) record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * See http://www.ietf.org/rfc/rfc2782.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   251
static class SrvRecord implements Comparable<SrvRecord> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    int priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    int weight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    int sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    String hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Creates a service record object from a string record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * DNS supplies the string record in the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *     <Priority> " " <Weight> " " <Port> " " <Host>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    SrvRecord(String srvRecord) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        StringTokenizer tokenizer = new StringTokenizer(srvRecord, " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        String port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (tokenizer.countTokens() == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            priority = Integer.parseInt(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            weight = Integer.parseInt(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            port = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            hostport = tokenizer.nextToken() + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Sort records in ascending order of priority value. For records with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * equal priority move those with weight 0 to the top of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   283
    public int compareTo(SrvRecord that) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (priority > that.priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return 1; // this > that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else if (priority < that.priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return -1; // this < that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } else if (weight == 0 && that.weight != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return -1; // this < that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else if (weight != 0 && that.weight == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return 1; // this > that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return 0; // this == that
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
}