0N/A/*
2362N/A * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.dns;
0N/A
0N/A
0N/Aimport java.lang.ref.SoftReference;
0N/Aimport java.util.Date;
0N/Aimport java.util.Vector;
0N/A
0N/A
0N/A/**
0N/A * ZoneNode extends NameNode to represent a tree of the zones in the
0N/A * DNS namespace, along with any intermediate nodes between zones.
0N/A * A ZoneNode that represents a zone may be "populated" with a
0N/A * NameNode tree containing the zone's contents.
0N/A *
0N/A * <p> A populated zone's contents will be flagged as having expired after
0N/A * the time specified by the minimum TTL value in the zone's SOA record.
0N/A *
0N/A * <p> Since zone cuts aren't directly modeled by a tree of ZoneNodes,
0N/A * ZoneNode.isZoneCut() always returns false.
0N/A *
0N/A * <p> The synchronization strategy is documented in DnsContext.java.
0N/A *
0N/A * <p> The zone's contents are accessed via a soft reference, so its
0N/A * heap space may be reclaimed when necessary. The zone may be
0N/A * repopulated later.
0N/A *
0N/A * @author Scott Seligman
0N/A */
0N/A
0N/A
0N/Aclass ZoneNode extends NameNode {
0N/A
0N/A private SoftReference contentsRef = null; // the zone's namespace
0N/A private long serialNumber = -1; // the zone data's serial number
0N/A private Date expiration = null; // time when the zone's data expires
0N/A
0N/A ZoneNode(String label) {
0N/A super(label);
0N/A }
0N/A
0N/A protected NameNode newNameNode(String label) {
0N/A return new ZoneNode(label);
0N/A }
0N/A
0N/A /*
0N/A * Clears the contents of this node. If the node was flagged as
0N/A * expired, it remains so.
0N/A */
0N/A synchronized void depopulate() {
0N/A contentsRef = null;
0N/A serialNumber = -1;
0N/A }
0N/A
0N/A /*
0N/A * Is this node currently populated?
0N/A */
0N/A synchronized boolean isPopulated() {
0N/A return (getContents() != null);
0N/A }
0N/A
0N/A /*
0N/A * Returns the zone's contents, or null if the zone is not populated.
0N/A */
0N/A synchronized NameNode getContents() {
0N/A return (contentsRef != null)
0N/A ? (NameNode) contentsRef.get()
0N/A : null;
0N/A }
0N/A
0N/A /*
0N/A * Has this zone's data expired?
0N/A */
0N/A synchronized boolean isExpired() {
0N/A return ((expiration != null) && expiration.before(new Date()));
0N/A }
0N/A
0N/A /*
0N/A * Returns the deepest populated zone on the path specified by a
0N/A * fully-qualified domain name, or null if there is no populated
0N/A * zone on that path. Note that a node may be depopulated after
0N/A * being returned.
0N/A */
0N/A ZoneNode getDeepestPopulated(DnsName fqdn) {
0N/A ZoneNode znode = this;
0N/A ZoneNode popNode = isPopulated() ? this : null;
0N/A for (int i = 1; i < fqdn.size(); i++) { // "i=1" to skip root label
0N/A znode = (ZoneNode) znode.get(fqdn.getKey(i));
0N/A if (znode == null) {
0N/A break;
0N/A } else if (znode.isPopulated()) {
0N/A popNode = znode;
0N/A }
0N/A }
0N/A return popNode;
0N/A }
0N/A
0N/A /*
0N/A * Populates (or repopulates) a zone given its own fully-qualified
0N/A * name and its resource records. Returns the zone's new contents.
0N/A */
0N/A NameNode populate(DnsName zone, ResourceRecords rrs) {
0N/A // assert zone.get(0).equals(""); // zone has root label
0N/A // assert (zone.size() == (depth() + 1)); // +1 due to root label
0N/A
0N/A NameNode newContents = new NameNode(null);
0N/A
0N/A for (int i = 0; i < rrs.answer.size(); i++) {
0N/A ResourceRecord rr = (ResourceRecord) rrs.answer.elementAt(i);
0N/A DnsName n = rr.getName();
0N/A
0N/A // Ignore resource records whose names aren't within the zone's
0N/A // domain. Also skip records of the zone's top node, since
0N/A // the zone's root NameNode is already in place.
0N/A if ((n.size() > zone.size()) && n.startsWith(zone)) {
0N/A NameNode nnode = newContents.add(n, zone.size());
0N/A if (rr.getType() == ResourceRecord.TYPE_NS) {
0N/A nnode.setZoneCut(true);
0N/A }
0N/A }
0N/A }
0N/A // The zone's SOA record is the first record in the answer section.
0N/A ResourceRecord soa = (ResourceRecord) rrs.answer.firstElement();
0N/A synchronized (this) {
0N/A contentsRef = new SoftReference(newContents);
0N/A serialNumber = getSerialNumber(soa);
0N/A setExpiration(getMinimumTtl(soa));
0N/A return newContents;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Set this zone's data to expire in <tt>secsToExpiration</tt> seconds.
0N/A */
0N/A private void setExpiration(long secsToExpiration) {
0N/A expiration = new Date(System.currentTimeMillis() +
0N/A 1000 * secsToExpiration);
0N/A }
0N/A
0N/A /*
0N/A * Returns an SOA record's minimum TTL field.
0N/A */
0N/A private static long getMinimumTtl(ResourceRecord soa) {
0N/A String rdata = (String) soa.getRdata();
0N/A int pos = rdata.lastIndexOf(' ') + 1;
0N/A return Long.parseLong(rdata.substring(pos));
0N/A }
0N/A
0N/A /*
0N/A * Compares this zone's serial number with that of an SOA record.
0N/A * Zone must be populated.
0N/A * Returns a negative, zero, or positive integer as this zone's
0N/A * serial number is less than, equal to, or greater than the SOA
0N/A * record's.
0N/A * See ResourceRecord.compareSerialNumbers() for a description of
0N/A * serial number arithmetic.
0N/A */
0N/A int compareSerialNumberTo(ResourceRecord soa) {
0N/A // assert isPopulated();
0N/A return ResourceRecord.compareSerialNumbers(serialNumber,
0N/A getSerialNumber(soa));
0N/A }
0N/A
0N/A /*
0N/A * Returns an SOA record's serial number.
0N/A */
0N/A private static long getSerialNumber(ResourceRecord soa) {
0N/A String rdata = (String) soa.getRdata();
0N/A
0N/A // An SOA record ends with: serial refresh retry expire minimum.
0N/A // Set "beg" to the space before serial, and "end" to the space after.
0N/A // We go "backward" to avoid dealing with escaped spaces in names.
0N/A int beg = rdata.length();
0N/A int end = -1;
0N/A for (int i = 0; i < 5; i++) {
0N/A end = beg;
0N/A beg = rdata.lastIndexOf(' ', end - 1);
0N/A }
0N/A return Long.parseLong(rdata.substring(beg + 1, end));
0N/A }
0N/A}