src/jdk.naming.dns/share/classes/com/sun/jndi/dns/ZoneNode.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
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.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ZoneNode extends NameNode to represent a tree of the zones in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * DNS namespace, along with any intermediate nodes between zones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A ZoneNode that represents a zone may be "populated" with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * NameNode tree containing the zone's contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> A populated zone's contents will be flagged as having expired after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the time specified by the minimum TTL value in the zone's SOA record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> Since zone cuts aren't directly modeled by a tree of ZoneNodes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ZoneNode.isZoneCut() always returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> The synchronization strategy is documented in DnsContext.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p> The zone's contents are accessed via a soft reference, so its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * heap space may be reclaimed when necessary.  The zone may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * repopulated later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Scott Seligman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
class ZoneNode extends NameNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    58
    private SoftReference<NameNode> contentsRef = null;   // the zone's namespace
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private long serialNumber = -1;     // the zone data's serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Date expiration = null;     // time when the zone's data expires
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    ZoneNode(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected NameNode newNameNode(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return new ZoneNode(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Clears the contents of this node.  If the node was flagged as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * expired, it remains so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    synchronized void depopulate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        contentsRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        serialNumber = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Is this node currently populated?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    synchronized boolean isPopulated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return (getContents() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Returns the zone's contents, or null if the zone is not populated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    synchronized NameNode getContents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return (contentsRef != null)
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    91
                ? contentsRef.get()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Has this zone's data expired?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    synchronized boolean isExpired() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return ((expiration != null) && expiration.before(new Date()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Returns the deepest populated zone on the path specified by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * fully-qualified domain name, or null if there is no populated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * zone on that path.  Note that a node may be depopulated after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * being returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    ZoneNode getDeepestPopulated(DnsName fqdn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ZoneNode znode = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ZoneNode popNode = isPopulated() ? this : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 1; i < fqdn.size(); i++) { //     "i=1" to skip root label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            znode = (ZoneNode) znode.get(fqdn.getKey(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (znode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else if (znode.isPopulated()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                popNode = znode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return popNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Populates (or repopulates) a zone given its own fully-qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * name and its resource records.  Returns the zone's new contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    NameNode populate(DnsName zone, ResourceRecords rrs) {
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   127
        // assert zone.get(0).isEmpty();               // zone has root label
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // assert (zone.size() == (depth() + 1));       // +1 due to root label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        NameNode newContents = new NameNode(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        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
   133
            ResourceRecord rr = rrs.answer.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            DnsName n = rr.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // Ignore resource records whose names aren't within the zone's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // domain.  Also skip records of the zone's top node, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // the zone's root NameNode is already in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if ((n.size() > zone.size()) && n.startsWith(zone)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                NameNode nnode = newContents.add(n, zone.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (rr.getType() == ResourceRecord.TYPE_NS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    nnode.setZoneCut(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // The zone's SOA record is the first record in the answer section.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   147
        ResourceRecord soa = rrs.answer.firstElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        synchronized (this) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   149
            contentsRef = new SoftReference<NameNode>(newContents);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            serialNumber = getSerialNumber(soa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            setExpiration(getMinimumTtl(soa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return newContents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /*
32210
958d823579c3 8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents: 25859
diff changeset
   157
     * Set this zone's data to expire in {@code secsToExpiration} seconds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private void setExpiration(long secsToExpiration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        expiration = new Date(System.currentTimeMillis() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                              1000 * secsToExpiration);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns an SOA record's minimum TTL field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static long getMinimumTtl(ResourceRecord soa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String rdata = (String) soa.getRdata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int pos = rdata.lastIndexOf(' ') + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return Long.parseLong(rdata.substring(pos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Compares this zone's serial number with that of an SOA record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Zone must be populated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns a negative, zero, or positive integer as this zone's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * serial number is less than, equal to, or greater than the SOA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * record's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * See ResourceRecord.compareSerialNumbers() for a description of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * serial number arithmetic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    int compareSerialNumberTo(ResourceRecord soa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // assert isPopulated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return ResourceRecord.compareSerialNumbers(serialNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                                   getSerialNumber(soa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Returns an SOA record's serial number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static long getSerialNumber(ResourceRecord soa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        String rdata = (String) soa.getRdata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // An SOA record ends with:  serial refresh retry expire minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Set "beg" to the space before serial, and "end" to the space after.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // We go "backward" to avoid dealing with escaped spaces in names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int beg = rdata.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        for (int i = 0; i < 5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            end = beg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            beg = rdata.lastIndexOf(' ', end - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return Long.parseLong(rdata.substring(beg + 1, end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}