jdk/src/share/classes/com/sun/jndi/dns/DnsContext.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3485 bc05765c365a
child 10324 e28265130e4f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3485
diff changeset
     2
 * Copyright (c) 2000, 2009, 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: 3485
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: 3485
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: 3485
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3485
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3485
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.dns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.spi.DirectoryManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jndi.toolkit.ctx.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A DnsContext is a directory context representing a DNS node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class DnsContext extends ComponentDirContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    DnsName domain;             // fully-qualified domain name of this context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                // with a root (empty) label at position 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    Hashtable environment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean envShared;  // true if environment is possibly shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                // and so must be copied on write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private boolean parentIsDns;        // was this DnsContext created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                        // another?  see composeName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String[] servers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Resolver resolver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean authoritative;      // must all responses be authoritative?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean recursion;          // request recursion on queries?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int timeout;                // initial timeout on UDP queries in ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int retries;                // number of UDP retries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final NameParser nameParser = new DnsNameParser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Timeouts for UDP queries use exponential backoff:  each retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // is for twice as long as the last.  The following constants set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the defaults for the initial timeout (in ms) and the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // retries, and name the environment properties used to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // these defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int DEFAULT_INIT_TIMEOUT = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int DEFAULT_RETRIES = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final String INIT_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                          "com.sun.jndi.dns.timeout.initial";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String RETRIES = "com.sun.jndi.dns.timeout.retries";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // The resource record type and class to use for lookups, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // property used to modify them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private CT lookupCT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final String LOOKUP_ATTR = "com.sun.jndi.dns.lookup.attr";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Property used to disallow recursion on queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final String RECURSION = "com.sun.jndi.dns.recursion";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // ANY == ResourceRecord.QCLASS_STAR == ResourceRecord.QTYPE_STAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final int ANY = ResourceRecord.QTYPE_STAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // The zone tree used for list operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final ZoneNode zoneTree = new ZoneNode(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Returns a DNS context for a given domain and servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Each server is of the form "server[:port]".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * IPv6 literal host names include delimiting brackets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * There must be at least one server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The environment must not be null; it is cloned before being stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public DnsContext(String domain, String[] servers, Hashtable environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.domain = new DnsName(domain.endsWith(".")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                  ? domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                  : domain + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.servers = servers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.environment = (Hashtable) environment.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        envShared = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        parentIsDns = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        resolver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        initFromEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns a clone of a DNS context, just like DnsContext(DnsContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * but with a different domain name and with parentIsDns set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    DnsContext(DnsContext ctx, DnsName domain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.domain = domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        parentIsDns = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Returns a clone of a DNS context.  The context's modifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * private state is independent of the original's (so closing one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * context, for example, won't close the other).  The two contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * share <tt>environment</tt>, but it's copy-on-write so there's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * no conflict.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private DnsContext(DnsContext ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        environment = ctx.environment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        envShared = ctx.envShared = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        parentIsDns = ctx.parentIsDns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        domain = ctx.domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        servers = ctx.servers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        resolver = ctx.resolver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        authoritative = ctx.authoritative;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        recursion = ctx.recursion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        timeout = ctx.timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        retries = ctx.retries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        lookupCT = ctx.lookupCT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (resolver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            resolver.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            resolver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    //---------- Environment operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Override default with a noncloning version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected Hashtable p_getEnvironment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return environment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public Hashtable getEnvironment() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return (Hashtable) environment.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Object addToEnvironment(String propName, Object propVal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (propName.equals(LOOKUP_ATTR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            lookupCT = getLookupCT((String) propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else if (propName.equals(Context.AUTHORITATIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            authoritative = "true".equalsIgnoreCase((String) propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else if (propName.equals(RECURSION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            recursion = "true".equalsIgnoreCase((String) propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else if (propName.equals(INIT_TIMEOUT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int val = Integer.parseInt((String) propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (timeout != val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                timeout = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                resolver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } else if (propName.equals(RETRIES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int val = Integer.parseInt((String) propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (retries != val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                retries = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                resolver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (!envShared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return environment.put(propName, propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else if (environment.get(propName) != propVal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            // copy on write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            environment = (Hashtable) environment.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            envShared = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return environment.put(propName, propVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return propVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public Object removeFromEnvironment(String propName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (propName.equals(LOOKUP_ATTR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            lookupCT = getLookupCT(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (propName.equals(Context.AUTHORITATIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            authoritative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else if (propName.equals(RECURSION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            recursion = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else if (propName.equals(INIT_TIMEOUT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (timeout != DEFAULT_INIT_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                timeout = DEFAULT_INIT_TIMEOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                resolver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else if (propName.equals(RETRIES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (retries != DEFAULT_RETRIES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                retries = DEFAULT_RETRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                resolver = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (!envShared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return environment.remove(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else if (environment.get(propName) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // copy-on-write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            environment = (Hashtable) environment.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            envShared = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return environment.remove(propName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Update PROVIDER_URL property.  Call this only when environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * is not being shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    void setProviderUrl(String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // assert !envShared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        environment.put(Context.PROVIDER_URL, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Read environment properties and set parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private void initFromEnvironment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throws InvalidAttributeIdentifierException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        lookupCT = getLookupCT((String) environment.get(LOOKUP_ATTR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        authoritative = "true".equalsIgnoreCase((String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                       environment.get(Context.AUTHORITATIVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        String val = (String) environment.get(RECURSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        recursion = ((val == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                     "true".equalsIgnoreCase(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        val = (String) environment.get(INIT_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        timeout = (val == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ? DEFAULT_INIT_TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            : Integer.parseInt(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        val = (String) environment.get(RETRIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        retries = (val == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            ? DEFAULT_RETRIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            : Integer.parseInt(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private CT getLookupCT(String attrId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throws InvalidAttributeIdentifierException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return (attrId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            ? new CT(ResourceRecord.CLASS_INTERNET, ResourceRecord.TYPE_TXT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            : fromAttrId(attrId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    //---------- Naming operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public Object c_lookup(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        cont.setSuccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            DnsContext ctx = new DnsContext(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            ctx.resolver = new Resolver(servers, timeout, retries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                // clone for parallelism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            DnsName fqdn = fullyQualify(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            ResourceRecords rrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                getResolver().query(fqdn, lookupCT.rrclass, lookupCT.rrtype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                    recursion, authoritative);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            Attributes attrs = rrsToAttrs(rrs, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            DnsContext ctx = new DnsContext(this, fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return DirectoryManager.getObjectInstance(ctx, name, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                                      environment, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            NamingException ne = new NamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    "Problem generating object using object factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw cont.fillInException(ne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public Object c_lookupLink(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return c_lookup(name, cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public NamingEnumeration c_list(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        cont.setSuccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            DnsName fqdn = fullyQualify(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            NameNode nnode = getNameNode(fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            DnsContext ctx = new DnsContext(this, fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return new NameClassPairEnumeration(ctx, nnode.getChildren());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public NamingEnumeration c_listBindings(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        cont.setSuccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            DnsName fqdn = fullyQualify(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            NameNode nnode = getNameNode(fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            DnsContext ctx = new DnsContext(this, fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return new BindingEnumeration(ctx, nnode.getChildren());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void c_bind(Name name, Object obj, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void c_rebind(Name name, Object obj, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public void c_unbind(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public void c_rename(Name oldname, Name newname, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        cont.setError(this, oldname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public Context c_createSubcontext(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void c_destroySubcontext(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public NameParser c_getNameParser(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        cont.setSuccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return nameParser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    //---------- Directory operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public void c_bind(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                       Object obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                       Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                       Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public void c_rebind(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                         Object obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                         Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                         Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public DirContext c_createSubcontext(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                         Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                         Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public Attributes c_getAttributes(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                      String[] attrIds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        cont.setSuccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            DnsName fqdn = fullyQualify(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            CT[] cts = attrIdsToClassesAndTypes(attrIds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            CT ct = getClassAndTypeToQuery(cts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            ResourceRecords rrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                getResolver().query(fqdn, ct.rrclass, ct.rrtype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                    recursion, authoritative);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return rrsToAttrs(rrs, cts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            throw cont.fillInException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void c_modifyAttributes(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                   int mod_op,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                   Attributes attrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                   Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public void c_modifyAttributes(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                   ModificationItem[] mods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                   Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public NamingEnumeration c_search(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                      Attributes matchingAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                      String[] attributesToReturn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        throw new OperationNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public NamingEnumeration c_search(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                                      String filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                      SearchControls cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        throw new OperationNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public NamingEnumeration c_search(Name name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                      String filterExpr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                                      Object[] filterArgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                      SearchControls cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                      Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        throw new OperationNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public DirContext c_getSchema(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public DirContext c_getSchemaClassDefinition(Name name, Continuation cont)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        cont.setError(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throw cont.fillInException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                new OperationNotSupportedException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    //---------- Name-related operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public String getNameInNamespace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return domain.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public Name composeName(Name name, Name prefix) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        Name result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // Any name that's not a CompositeName is assumed to be a DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // compound name.  Convert each to a DnsName for syntax checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (!(prefix instanceof DnsName || prefix instanceof CompositeName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            prefix = (new DnsName()).addAll(prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (!(name instanceof DnsName || name instanceof CompositeName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            name = (new DnsName()).addAll(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // Each of prefix and name is now either a DnsName or a CompositeName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        // If we have two DnsNames, simply join them together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if ((prefix instanceof DnsName) && (name instanceof DnsName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            result = (DnsName) (prefix.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            result.addAll(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return new CompositeName().add(result.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // Wrap compound names in composite names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        Name prefixC = (prefix instanceof CompositeName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            ? prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            : new CompositeName().add(prefix.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        Name nameC = (name instanceof CompositeName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            ? name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            : new CompositeName().add(name.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int prefixLast = prefixC.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // Let toolkit do the work at namespace boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (nameC.isEmpty() || nameC.get(0).equals("") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                prefixC.isEmpty() || prefixC.get(prefixLast).equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return super.composeName(nameC, prefixC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        result = (prefix == prefixC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            ? (CompositeName) prefixC.clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            : prefixC;                  // prefixC is already a clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        result.addAll(nameC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (parentIsDns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            DnsName dnsComp = (prefix instanceof DnsName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                           ? (DnsName) prefix.clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                           : new DnsName(prefixC.get(prefixLast));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            dnsComp.addAll((name instanceof DnsName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                           ? name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                           : new DnsName(nameC.get(0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            result.remove(prefixLast + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            result.remove(prefixLast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            result.add(prefixLast, dnsComp.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    //---------- Helper methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Resolver is not created until needed, to allow time for updates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * to the environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private synchronized Resolver getResolver() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (resolver == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            resolver = new Resolver(servers, timeout, retries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return resolver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Returns the fully-qualified domain name of a name given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * relative to this context.  Result includes a root label (an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * empty component at position 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    DnsName fullyQualify(Name name) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (name.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        DnsName dnsName = (name instanceof CompositeName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            ? new DnsName(name.get(0))                  // parse name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            : (DnsName) (new DnsName()).addAll(name);   // clone & check syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (dnsName.hasRootLabel()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // Be overly generous and allow root label if we're in root domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (domain.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                return dnsName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                throw new InvalidNameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                       "DNS name " + dnsName + " not relative to " + domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return (DnsName) dnsName.addAll(0, domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Converts resource records to an attribute set.  Only resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * records in the answer section are used, and only those that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * match the classes and types in cts (see classAndTypeMatch()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * for matching rules).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private static Attributes rrsToAttrs(ResourceRecords rrs, CT[] cts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        BasicAttributes attrs = new BasicAttributes(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        for (int i = 0; i < rrs.answer.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            ResourceRecord rr = (ResourceRecord) rrs.answer.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            int rrtype  = rr.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            int rrclass = rr.getRrclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (!classAndTypeMatch(rrclass, rrtype, cts)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            String attrId = toAttrId(rrclass, rrtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            Attribute attr = attrs.get(attrId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                attr = new BasicAttribute(attrId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                attrs.put(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            attr.add(rr.getRdata());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Returns true if rrclass and rrtype match some element of cts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * A match occurs if corresponding classes and types are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * or if the array value is ANY.  If cts is null, then any class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * and type match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    private static boolean classAndTypeMatch(int rrclass, int rrtype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                             CT[] cts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (cts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        for (int i = 0; i < cts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            CT ct = cts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            boolean classMatch = (ct.rrclass == ANY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                 (ct.rrclass == rrclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            boolean typeMatch  = (ct.rrtype == ANY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                 (ct.rrtype == rrtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (classMatch && typeMatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Returns the attribute ID for a resource record given its class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * and type.  If the record is in the internet class, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * corresponding attribute ID is the record's type name (or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * integer type value if the name is not known).  If the record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * not in the internet class, the class name (or integer class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * value) is prepended to the attribute ID, separated by a space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * A class or type value of ANY represents an indeterminate class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * or type, and is represented within the attribute ID by "*".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * For example, the attribute ID "IN *" represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * any type in the internet class, and "* NS" represents an NS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * record of any class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private static String toAttrId(int rrclass, int rrtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        String attrId = ResourceRecord.getTypeName(rrtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (rrclass != ResourceRecord.CLASS_INTERNET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            attrId = ResourceRecord.getRrclassName(rrclass) + " " + attrId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return attrId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Returns the class and type values corresponding to an attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * ID.  An indeterminate class or type is represented by ANY.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * toAttrId() for the format of attribute IDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @throws InvalidAttributeIdentifierException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *          if class or type is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    private static CT fromAttrId(String attrId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            throws InvalidAttributeIdentifierException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (attrId.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            throw new InvalidAttributeIdentifierException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    "Attribute ID cannot be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        int rrclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        int rrtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        int space = attrId.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        // class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (space < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            rrclass = ResourceRecord.CLASS_INTERNET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            String className = attrId.substring(0, space);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            rrclass = ResourceRecord.getRrclass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if (rrclass < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                throw new InvalidAttributeIdentifierException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        "Unknown resource record class '" + className + '\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        String typeName = attrId.substring(space + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        rrtype = ResourceRecord.getType(typeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (rrtype < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            throw new InvalidAttributeIdentifierException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    "Unknown resource record type '" + typeName + '\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return new CT(rrclass, rrtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Returns an array of the classes and types corresponding to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * set of attribute IDs.  See toAttrId() for the format of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * attribute IDs, and classAndTypeMatch() for the format of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * array returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private static CT[] attrIdsToClassesAndTypes(String[] attrIds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            throws InvalidAttributeIdentifierException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (attrIds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        CT[] cts = new CT[attrIds.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        for (int i = 0; i < attrIds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            cts[i] = fromAttrId(attrIds[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        return cts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Returns the most restrictive resource record class and type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * that may be used to query for records matching cts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * See classAndTypeMatch() for matching rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    private static CT getClassAndTypeToQuery(CT[] cts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        int rrclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        int rrtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (cts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            // Query all records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            rrclass = ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            rrtype  = ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        } else if (cts.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            // No records are requested, but we need to ask for something.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            rrclass = ResourceRecord.CLASS_INTERNET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            rrtype  = ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            rrclass = cts[0].rrclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            rrtype  = cts[0].rrtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            for (int i = 1; i < cts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                if (rrclass != cts[i].rrclass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    rrclass = ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                if (rrtype != cts[i].rrtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    rrtype = ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return new CT(rrclass, rrtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    //---------- Support for list operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Synchronization notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Any access to zoneTree that walks the tree, whether it modifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * the tree or not, is synchronized on zoneTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * [%%% Note:  a read/write lock would allow increased concurrency.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * The depth of a ZoneNode can thereafter be accessed without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * further synchronization.  Access to other fields and methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * should be synchronized on the node itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * A zone's contents is a NameNode tree that, once created, is never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * modified.  The only synchronization needed is to ensure that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * gets flushed into shared memory after being created, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * accomplished by ZoneNode.populate().  The contents are accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * via a soft reference, so a ZoneNode may be seen to be populated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * one moment and unpopulated the next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Returns the node in the zone tree corresponding to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * fully-qualified domain name.  If the desired portion of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * tree has not yet been populated or has been outdated, a zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * transfer is done to populate the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    private NameNode getNameNode(DnsName fqdn) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        dprint("getNameNode(" + fqdn + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        // Find deepest related zone in zone tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        ZoneNode znode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        DnsName zone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        synchronized (zoneTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            znode = zoneTree.getDeepestPopulated(fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        dprint("Deepest related zone in zone tree: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
               ((znode != null) ? znode.getLabel() : "[none]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        NameNode topOfZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        NameNode nnode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (znode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            synchronized (znode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                topOfZone = znode.getContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            // If fqdn is in znode's zone, is not at a zone cut, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            // is current, we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (topOfZone != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                nnode = topOfZone.get(fqdn, znode.depth() + 1); // +1 for root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                if ((nnode != null) && !nnode.isZoneCut()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    dprint("Found node " + fqdn + " in zone tree");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    zone = (DnsName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                        fqdn.getPrefix(znode.depth() + 1);      // +1 for root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    boolean current = isZoneCurrent(znode, zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    boolean restart = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    synchronized (znode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                        if (topOfZone != znode.getContents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                            // Zone was modified while we were examining it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                            // All bets are off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                            restart = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        } else if (!current) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                            znode.depopulate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                            return nnode;                       // cache hit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    dprint("Zone not current; discarding node");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    if (restart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                        return getNameNode(fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // Cache miss...  do it the expensive way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        dprint("Adding node " + fqdn + " to zone tree");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        // Find fqdn's zone and add it to the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        zone = getResolver().findZoneName(fqdn, ResourceRecord.CLASS_INTERNET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                          recursion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        dprint("Node's zone is " + zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        synchronized (zoneTree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            znode = (ZoneNode) zoneTree.add(zone, 1);   // "1" to skip root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // If znode is now populated we know -- because the first half of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // getNodeName() didn't find it -- that it was populated by another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // thread during this method call.  Assume then that it's current.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        synchronized (znode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            topOfZone = znode.isPopulated()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                ? znode.getContents()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                : populateZone(znode, zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        // Desired node should now be in znode's populated zone.  Find it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        nnode = topOfZone.get(fqdn, zone.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (nnode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            throw new ConfigurationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    "DNS error: node not found in its own zone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        dprint("Found node in newly-populated zone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        return nnode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * Does a zone transfer to [re]populate a zone in the zone tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Returns the zone's new contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    private NameNode populateZone(ZoneNode znode, DnsName zone)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        dprint("Populating zone " + zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        // assert Thread.holdsLock(znode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        ResourceRecords rrs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            getResolver().queryZone(zone,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                                    ResourceRecord.CLASS_INTERNET, recursion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        dprint("zone xfer complete: " + rrs.answer.size() + " records");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        return znode.populate(zone, rrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * Determine if a ZoneNode's data is current.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * We base this on a comparison between the cached serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * number and the latest SOA record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * If there is no SOA record, znode is not (or is no longer) a zone:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * depopulate znode and return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Since this method may perform a network operation, it is best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * to call it with znode unlocked.  Caller must then note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * result may be outdated by the time this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    private boolean isZoneCurrent(ZoneNode znode, DnsName zone)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // former version:  return !znode.isExpired();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (!znode.isPopulated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        ResourceRecord soa =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            getResolver().findSoa(zone, ResourceRecord.CLASS_INTERNET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                  recursion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        synchronized (znode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            if (soa == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                znode.depopulate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return (znode.isPopulated() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    znode.compareSerialNumberTo(soa) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    //---------- Debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
3471
9eaa69076815 6657619: DnsContext.debug is public static mutable (findbugs)
vinnie
parents: 2
diff changeset
   925
    private static final boolean debug = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    private static final void dprint(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            System.err.println("** " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
//----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
 * A pairing of a resource record class and a resource record type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
 * A value of ANY in either field represents an indeterminate value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
class CT {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    int rrclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    int rrtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    CT(int rrclass, int rrtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        this.rrclass = rrclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        this.rrtype = rrtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
//----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
 * An enumeration of name/classname pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
 * Nodes that have children or that are zone cuts are returned with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
 * classname DirContext.  Other nodes are returned with classname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
 * Object even though they are DirContexts as well, since this might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
 * make the namespace easier to browse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
class NameClassPairEnumeration implements NamingEnumeration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    protected Enumeration nodes;    // nodes to be enumerated, or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    protected DnsContext ctx;       // context being enumerated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    NameClassPairEnumeration(DnsContext ctx, Hashtable nodes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        this.ctx = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        this.nodes = (nodes != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            ? nodes.elements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    /*
3485
bc05765c365a 6585239: Regression: 2 DNS tests fail with JDK 5.0u13 b01 and pass with 5.0u12fcs
xuelei
parents: 3471
diff changeset
   975
     * ctx will be set to null when no longer needed by the enumeration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
3485
bc05765c365a 6585239: Regression: 2 DNS tests fail with JDK 5.0u13 b01 and pass with 5.0u12fcs
xuelei
parents: 3471
diff changeset
   977
    public void close() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        nodes = null;
3485
bc05765c365a 6585239: Regression: 2 DNS tests fail with JDK 5.0u13 b01 and pass with 5.0u12fcs
xuelei
parents: 3471
diff changeset
   979
        ctx = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    public boolean hasMore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        boolean more = ((nodes != null) && nodes.hasMoreElements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (!more) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        return more;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    public Object next() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (!hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            throw new java.util.NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        NameNode nnode = (NameNode) nodes.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        String className = (nnode.isZoneCut() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                            (nnode.getChildren() != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            ? "javax.naming.directory.DirContext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            : "java.lang.Object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        String label = nnode.getLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        Name compName = (new DnsName()).add(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        Name cname = (new CompositeName()).add(compName.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        NameClassPair ncp = new NameClassPair(cname.toString(), className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        ncp.setNameInNamespace(ctx.fullyQualify(cname).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return ncp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return hasMore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            throw (new java.util.NoSuchElementException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    "javax.naming.NamingException was thrown: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
 * An enumeration of Bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
class BindingEnumeration extends NameClassPairEnumeration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    BindingEnumeration(DnsContext ctx, Hashtable nodes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        super(ctx, nodes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    // Finalizer not needed since it's safe to leave ctx unclosed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
//  protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
//      close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    public Object next() throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (!hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            throw (new java.util.NoSuchElementException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        NameNode nnode = (NameNode) nodes.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        String label = nnode.getLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        Name compName = (new DnsName()).add(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        String compNameStr = compName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        Name cname = (new CompositeName()).add(compNameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        String cnameStr = cname.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        DnsName fqdn = ctx.fullyQualify(compName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        // Clone ctx to create the child context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        DnsContext child = new DnsContext(ctx, fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            Object obj = DirectoryManager.getObjectInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    child, cname, ctx, child.environment, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            Binding binding = new Binding(cnameStr, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            binding.setNameInNamespace(ctx.fullyQualify(cname).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            return binding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            NamingException ne = new NamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    "Problem generating object using object factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            ne.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            throw ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
}