Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jndi/dns/ZoneNode.java
38924 views
/*1* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.dns;262728import java.lang.ref.SoftReference;29import java.util.Date;30import java.util.Vector;313233/**34* ZoneNode extends NameNode to represent a tree of the zones in the35* DNS namespace, along with any intermediate nodes between zones.36* A ZoneNode that represents a zone may be "populated" with a37* NameNode tree containing the zone's contents.38*39* <p> A populated zone's contents will be flagged as having expired after40* the time specified by the minimum TTL value in the zone's SOA record.41*42* <p> Since zone cuts aren't directly modeled by a tree of ZoneNodes,43* ZoneNode.isZoneCut() always returns false.44*45* <p> The synchronization strategy is documented in DnsContext.java.46*47* <p> The zone's contents are accessed via a soft reference, so its48* heap space may be reclaimed when necessary. The zone may be49* repopulated later.50*51* @author Scott Seligman52*/535455class ZoneNode extends NameNode {5657private SoftReference<NameNode> contentsRef = null; // the zone's namespace58private long serialNumber = -1; // the zone data's serial number59private Date expiration = null; // time when the zone's data expires6061ZoneNode(String label) {62super(label);63}6465protected NameNode newNameNode(String label) {66return new ZoneNode(label);67}6869/*70* Clears the contents of this node. If the node was flagged as71* expired, it remains so.72*/73synchronized void depopulate() {74contentsRef = null;75serialNumber = -1;76}7778/*79* Is this node currently populated?80*/81synchronized boolean isPopulated() {82return (getContents() != null);83}8485/*86* Returns the zone's contents, or null if the zone is not populated.87*/88synchronized NameNode getContents() {89return (contentsRef != null)90? contentsRef.get()91: null;92}9394/*95* Has this zone's data expired?96*/97synchronized boolean isExpired() {98return ((expiration != null) && expiration.before(new Date()));99}100101/*102* Returns the deepest populated zone on the path specified by a103* fully-qualified domain name, or null if there is no populated104* zone on that path. Note that a node may be depopulated after105* being returned.106*/107ZoneNode getDeepestPopulated(DnsName fqdn) {108ZoneNode znode = this;109ZoneNode popNode = isPopulated() ? this : null;110for (int i = 1; i < fqdn.size(); i++) { // "i=1" to skip root label111znode = (ZoneNode) znode.get(fqdn.getKey(i));112if (znode == null) {113break;114} else if (znode.isPopulated()) {115popNode = znode;116}117}118return popNode;119}120121/*122* Populates (or repopulates) a zone given its own fully-qualified123* name and its resource records. Returns the zone's new contents.124*/125NameNode populate(DnsName zone, ResourceRecords rrs) {126// assert zone.get(0).equals(""); // zone has root label127// assert (zone.size() == (depth() + 1)); // +1 due to root label128129NameNode newContents = new NameNode(null);130131for (int i = 0; i < rrs.answer.size(); i++) {132ResourceRecord rr = rrs.answer.elementAt(i);133DnsName n = rr.getName();134135// Ignore resource records whose names aren't within the zone's136// domain. Also skip records of the zone's top node, since137// the zone's root NameNode is already in place.138if ((n.size() > zone.size()) && n.startsWith(zone)) {139NameNode nnode = newContents.add(n, zone.size());140if (rr.getType() == ResourceRecord.TYPE_NS) {141nnode.setZoneCut(true);142}143}144}145// The zone's SOA record is the first record in the answer section.146ResourceRecord soa = rrs.answer.firstElement();147synchronized (this) {148contentsRef = new SoftReference<NameNode>(newContents);149serialNumber = getSerialNumber(soa);150setExpiration(getMinimumTtl(soa));151return newContents;152}153}154155/*156* Set this zone's data to expire in <tt>secsToExpiration</tt> seconds.157*/158private void setExpiration(long secsToExpiration) {159expiration = new Date(System.currentTimeMillis() +1601000 * secsToExpiration);161}162163/*164* Returns an SOA record's minimum TTL field.165*/166private static long getMinimumTtl(ResourceRecord soa) {167String rdata = (String) soa.getRdata();168int pos = rdata.lastIndexOf(' ') + 1;169return Long.parseLong(rdata.substring(pos));170}171172/*173* Compares this zone's serial number with that of an SOA record.174* Zone must be populated.175* Returns a negative, zero, or positive integer as this zone's176* serial number is less than, equal to, or greater than the SOA177* record's.178* See ResourceRecord.compareSerialNumbers() for a description of179* serial number arithmetic.180*/181int compareSerialNumberTo(ResourceRecord soa) {182// assert isPopulated();183return ResourceRecord.compareSerialNumbers(serialNumber,184getSerialNumber(soa));185}186187/*188* Returns an SOA record's serial number.189*/190private static long getSerialNumber(ResourceRecord soa) {191String rdata = (String) soa.getRdata();192193// An SOA record ends with: serial refresh retry expire minimum.194// Set "beg" to the space before serial, and "end" to the space after.195// We go "backward" to avoid dealing with escaped spaces in names.196int beg = rdata.length();197int end = -1;198for (int i = 0; i < 5; i++) {199end = beg;200beg = rdata.lastIndexOf(' ', end - 1);201}202return Long.parseLong(rdata.substring(beg + 1, end));203}204}205206207