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