jdk/src/share/classes/com/sun/jndi/dns/NameNode.java
author xuelei
Tue, 21 Feb 2012 05:44:29 -0800
changeset 11913 be61e1597cc6
parent 10324 e28265130e4f
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: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.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.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A NameNode represents a node in the DNS namespace.  Each node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * has a label, which is its name relative to its parent (so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * node at Sun.COM has label "Sun").  Each node has a hashtable of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * children indexed by their labels converted to lower-case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> A node may be addressed from another by giving a DnsName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * consisting of the sequence of labels from one node to the other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> Each node also has an <tt>isZoneCut</tt> flag, used to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * if the node is a zone cut.  A zone cut is a node with an NS record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that is contained in one zone, but that actually belongs to a child zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> All access is unsynchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class NameNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String label;               // name of this node relative to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                        // parent, or null for root of a tree
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    55
    private Hashtable<String,NameNode> children = null;  // child nodes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean isZoneCut = false;  // true if this node is a zone cut
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int depth = 0;              // depth in tree (0 for root)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    NameNode(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Returns a newly-allocated NameNode.  Used to allocate new nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * in a tree.  Should be overridden in a subclass to return an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * of the subclass's type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected NameNode newNameNode(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return new NameNode(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns the name of this node relative to its parent, or null for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the root of a tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    String getLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns the depth of this node in the tree.  The depth of the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int depth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    boolean isZoneCut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return isZoneCut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    void setZoneCut(boolean isZoneCut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.isZoneCut = isZoneCut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Returns the children of this node, or null if there are none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The caller must not modify the Hashtable returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   100
    Hashtable<String,NameNode> getChildren() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return children;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns the child node given the hash key (the down-cased label)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * for its name relative to this node, or null if there is no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    NameNode get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return (children != null)
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   111
            ? children.get(key)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns the node at the end of a path, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * node does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The path is specified by the labels of <tt>name</tt>, beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * at index idx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    NameNode get(DnsName name, int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        NameNode node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int i = idx; i < name.size() && node != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            node = node.get(name.getKey(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the node at the end of a path, creating it and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * intermediate nodes as needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The path is specified by the labels of <tt>name</tt>, beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * at index idx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    NameNode add(DnsName name, int idx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        NameNode node = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (int i = idx; i < name.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            String label = name.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            String key = name.getKey(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            NameNode child = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (node.children == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   143
                node.children = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } else {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   145
                child = node.children.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                child = newNameNode(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                child.depth = node.depth + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                node.children.put(key, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            node = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}