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