001: /*
002: * Copyright 2000-2002 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jndi.dns;
027:
028: import java.lang.ref.SoftReference;
029: import java.util.Date;
030: import java.util.Vector;
031:
032: /**
033: * ZoneNode extends NameNode to represent a tree of the zones in the
034: * DNS namespace, along with any intermediate nodes between zones.
035: * A ZoneNode that represents a zone may be "populated" with a
036: * NameNode tree containing the zone's contents.
037: *
038: * <p> A populated zone's contents will be flagged as having expired after
039: * the time specified by the minimum TTL value in the zone's SOA record.
040: *
041: * <p> Since zone cuts aren't directly modeled by a tree of ZoneNodes,
042: * ZoneNode.isZoneCut() always returns false.
043: *
044: * <p> The synchronization strategy is documented in DnsContext.java.
045: *
046: * <p> The zone's contents are accessed via a soft reference, so its
047: * heap space may be reclaimed when necessary. The zone may be
048: * repopulated later.
049: *
050: * @author Scott Seligman
051: * @version 1.17 07/05/05
052: */
053:
054: class ZoneNode extends NameNode {
055:
056: private SoftReference contentsRef = null; // the zone's namespace
057: private long serialNumber = -1; // the zone data's serial number
058: private Date expiration = null; // time when the zone's data expires
059:
060: ZoneNode(String label) {
061: super (label);
062: }
063:
064: protected NameNode newNameNode(String label) {
065: return new ZoneNode(label);
066: }
067:
068: /*
069: * Clears the contents of this node. If the node was flagged as
070: * expired, it remains so.
071: */
072: synchronized void depopulate() {
073: contentsRef = null;
074: serialNumber = -1;
075: }
076:
077: /*
078: * Is this node currently populated?
079: */
080: synchronized boolean isPopulated() {
081: return (getContents() != null);
082: }
083:
084: /*
085: * Returns the zone's contents, or null if the zone is not populated.
086: */
087: synchronized NameNode getContents() {
088: return (contentsRef != null) ? (NameNode) contentsRef.get()
089: : null;
090: }
091:
092: /*
093: * Has this zone's data expired?
094: */
095: synchronized boolean isExpired() {
096: return ((expiration != null) && expiration.before(new Date()));
097: }
098:
099: /*
100: * Returns the deepest populated zone on the path specified by a
101: * fully-qualified domain name, or null if there is no populated
102: * zone on that path. Note that a node may be depopulated after
103: * being returned.
104: */
105: ZoneNode getDeepestPopulated(DnsName fqdn) {
106: ZoneNode znode = this ;
107: ZoneNode popNode = isPopulated() ? this : null;
108: for (int i = 1; i < fqdn.size(); i++) { // "i=1" to skip root label
109: znode = (ZoneNode) znode.get(fqdn.getKey(i));
110: if (znode == null) {
111: break;
112: } else if (znode.isPopulated()) {
113: popNode = znode;
114: }
115: }
116: return popNode;
117: }
118:
119: /*
120: * Populates (or repopulates) a zone given its own fully-qualified
121: * name and its resource records. Returns the zone's new contents.
122: */
123: NameNode populate(DnsName zone, ResourceRecords rrs) {
124: // assert zone.get(0).equals(""); // zone has root label
125: // assert (zone.size() == (depth() + 1)); // +1 due to root label
126:
127: NameNode newContents = new NameNode(null);
128:
129: for (int i = 0; i < rrs.answer.size(); i++) {
130: ResourceRecord rr = (ResourceRecord) rrs.answer
131: .elementAt(i);
132: DnsName n = rr.getName();
133:
134: // Ignore resource records whose names aren't within the zone's
135: // domain. Also skip records of the zone's top node, since
136: // the zone's root NameNode is already in place.
137: if ((n.size() > zone.size()) && n.startsWith(zone)) {
138: NameNode nnode = newContents.add(n, zone.size());
139: if (rr.getType() == ResourceRecord.TYPE_NS) {
140: nnode.setZoneCut(true);
141: }
142: }
143: }
144: // The zone's SOA record is the first record in the answer section.
145: ResourceRecord soa = (ResourceRecord) rrs.answer.firstElement();
146: synchronized (this ) {
147: contentsRef = new SoftReference(newContents);
148: serialNumber = getSerialNumber(soa);
149: setExpiration(getMinimumTtl(soa));
150: return newContents;
151: }
152: }
153:
154: /*
155: * Set this zone's data to expire in <tt>secsToExpiration</tt> seconds.
156: */
157: private void setExpiration(long secsToExpiration) {
158: expiration = new Date(System.currentTimeMillis() + 1000
159: * secsToExpiration);
160: }
161:
162: /*
163: * Returns an SOA record's minimum TTL field.
164: */
165: private static long getMinimumTtl(ResourceRecord soa) {
166: String rdata = (String) soa.getRdata();
167: int pos = rdata.lastIndexOf(' ') + 1;
168: return Long.parseLong(rdata.substring(pos));
169: }
170:
171: /*
172: * Compares this zone's serial number with that of an SOA record.
173: * Zone must be populated.
174: * Returns a negative, zero, or positive integer as this zone's
175: * serial number is less than, equal to, or greater than the SOA
176: * record's.
177: * See ResourceRecord.compareSerialNumbers() for a description of
178: * serial number arithmetic.
179: */
180: int compareSerialNumberTo(ResourceRecord soa) {
181: // assert isPopulated();
182: return ResourceRecord.compareSerialNumbers(serialNumber,
183: getSerialNumber(soa));
184: }
185:
186: /*
187: * Returns an SOA record's serial number.
188: */
189: private static long getSerialNumber(ResourceRecord soa) {
190: String rdata = (String) soa.getRdata();
191:
192: // An SOA record ends with: serial refresh retry expire minimum.
193: // Set "beg" to the space before serial, and "end" to the space after.
194: // We go "backward" to avoid dealing with escaped spaces in names.
195: int beg = rdata.length();
196: int end = -1;
197: for (int i = 0; i < 5; i++) {
198: end = beg;
199: beg = rdata.lastIndexOf(' ', end - 1);
200: }
201: return Long.parseLong(rdata.substring(beg + 1, end));
202: }
203: }
|