jdk/src/share/classes/sun/security/krb5/KrbServiceLocator.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 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: 1946
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: 1946
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: 1946
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1946
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1946
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.security.krb5;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.spi.NamingManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class discovers the location of Kerberos services by querying DNS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * as defined in RFC 4120.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Seema Malkani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.7
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 KrbServiceLocator {
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
    private static final String[] SRV_RR_ATTR = new String[] {SRV_RR};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final String SRV_TXT = "TXT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String[] SRV_TXT_ATTR = new String[] {SRV_TXT};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private KrbServiceLocator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Locates the KERBEROS service for a given domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Queries DNS for a list of KERBEROS Service Text Records (TXT) for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * given domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Information on the mapping of DNS hostnames and domain names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * to Kerberos realms is stored using DNS TXT records
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param domainName A string domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param environment The possibly null environment of the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return An ordered list of hostports for the Kerberos service or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *          the service has not been located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static String[] getKerberosService(String realmName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // search realm in SRV TXT records
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String dnsUrl = "dns:///_kerberos." + realmName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        String[] records = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // Create the DNS context using NamingManager rather than using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // the initial context constructor. This avoids having the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // context constructor call itself (when processing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            // argument in the getAttributes call).
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    80
            Context ctx = NamingManager.getURLContext("dns", new Hashtable<>(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (!(ctx instanceof DirContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return null; // cannot create a DNS context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Attributes attrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                ((DirContext)ctx).getAttributes(dnsUrl, SRV_TXT_ATTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (attrs != null && ((attr = attrs.get(SRV_TXT)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                int numValues = attr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                int numRecords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                String[] txtRecords = new String[numValues];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // gather the text records
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                while (i < numValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        txtRecords[j] = (String)attr.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        // ignore bad value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                numRecords = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                // trim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (numRecords < numValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    String[] trimmed = new String[numRecords];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    System.arraycopy(txtRecords, 0, trimmed, 0, numRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    records = trimmed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    records = txtRecords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return records;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Locates the KERBEROS service for a given domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Queries DNS for a list of KERBEROS Service Location Records (SRV) for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * given domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param domainName A string domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return An ordered list of hostports for the Kerberos service or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *          the service has not been located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static String[] getKerberosService(String realmName, String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
1946
2e6e15ca4d56 6552334: Enable DNS in Kerberos by default
weijun
parents: 2
diff changeset
   133
        String dnsUrl = "dns:///_kerberos." + protocol + "." + realmName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String[] hostports = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // Create the DNS context using NamingManager rather than using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // the initial context constructor. This avoids having the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // context constructor call itself (when processing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // argument in the getAttributes call).
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   141
            Context ctx = NamingManager.getURLContext("dns", new Hashtable<>(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (!(ctx instanceof DirContext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return null; // cannot create a DNS context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Attributes attrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ((DirContext)ctx).getAttributes(dnsUrl, SRV_RR_ATTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Attribute attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (attrs != null && ((attr = attrs.get(SRV_RR)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                int numValues = attr.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                int numRecords = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                SrvRecord[] srvRecords = new SrvRecord[numValues];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                // create the service records
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                while (i < numValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        srvRecords[j] = new SrvRecord((String) attr.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        // ignore bad value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                numRecords = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // trim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (numRecords < numValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    SrvRecord[] trimmed = new SrvRecord[numRecords];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    System.arraycopy(srvRecords, 0, trimmed, 0, numRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    srvRecords = trimmed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                // Sort the service records in ascending order of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // priority value. For records with equal priority, move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // those with weight 0 to the top of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (numRecords > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    Arrays.sort(srvRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // extract the host and port number from each service record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                hostports = extractHostports(srvRecords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return hostports;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Extract hosts and port numbers from a list of SRV records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * An array of hostports is returned or null if none were found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static String[] extractHostports(SrvRecord[] srvRecords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String[] hostports = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int head = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int tail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int sublistLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (int i = 0; i < srvRecords.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (hostports == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                hostports = new String[srvRecords.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // find the head and tail of the list of records having the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // priority value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            head = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            while (i < srvRecords.length - 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                srvRecords[i].priority == srvRecords[i + 1].priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            tail = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // select hostports from the sublist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            sublistLength = (tail - head) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            for (int j = 0; j < sublistLength; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                hostports[k++] = selectHostport(srvRecords, head, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return hostports;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Randomly select a service record in the range [head, tail] and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * its hostport value. Follows the algorithm in RFC 2782.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private static String selectHostport(SrvRecord[] srvRecords, int head,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            int tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (head == tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return srvRecords[head].hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // compute the running sum for records between head and tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i = head; i <= tail; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (srvRecords[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                sum += srvRecords[i].weight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                srvRecords[i].sum = sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        String hostport = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // If all records have zero weight, select first available one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // otherwise, randomly select a record according to its weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int target = (sum == 0 ? 0 : random.nextInt(sum + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (int i = head; i <= tail; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (srvRecords[i] != null && srvRecords[i].sum >= target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                hostport = srvRecords[i].hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                srvRecords[i] = null; // make this record unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * This class holds a DNS service (SRV) record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * See http://www.ietf.org/rfc/rfc2782.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   263
static class SrvRecord implements Comparable<SrvRecord> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    int priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    int weight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    int sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    String hostport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Creates a service record object from a string record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * DNS supplies the string record in the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          <Priority> " " <Weight> " " <Port> " " <Host>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    SrvRecord(String srvRecord) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        StringTokenizer tokenizer = new StringTokenizer(srvRecord, " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        String port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (tokenizer.countTokens() == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            priority = Integer.parseInt(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            weight = Integer.parseInt(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            port = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            hostport = tokenizer.nextToken() + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Sort records in ascending order of priority value. For records with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * equal priority move those with weight 0 to the top of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   295
    public int compareTo(SrvRecord that) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (priority > that.priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return 1; // this > that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else if (priority < that.priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return -1; // this < that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else if (weight == 0 && that.weight != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return -1; // this < that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else if (weight != 0 && that.weight == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return 1; // this > that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return 0; // this == that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}