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.net.MalformedURLException;
029: import java.util.Hashtable;
030: import java.util.StringTokenizer;
031:
032: import com.sun.jndi.toolkit.url.Uri;
033: import com.sun.jndi.toolkit.url.UrlUtil;
034:
035: /**
036: * A DnsUrl represents a DNS pseudo-URL of the form
037: * <pre>
038: * dns://[host][:port][/[domain]]
039: * or
040: * dns:[/][domain]
041: * </pre>
042: * The host names a DNS server. If the host is not provided, it
043: * indicates that the underlying platform's DNS server(s) should be
044: * used if possible, or that "localhost" should be used otherwise. If
045: * the port is not provided, the DNS default port 53 will be used.
046: * The domain indicates the domain name of the context, and is not
047: * necessarily related to the domain of the server; if it is not
048: * provided, the root domain "." is used. Special characters in
049: * the domain name must be %-escaped as described in RFC 2396.
050: *
051: * @author Scott Seligman
052: * @version 1.14 07/05/05
053: */
054:
055: public class DnsUrl extends Uri {
056:
057: private String domain; // domain name of the context
058:
059: /**
060: * Given a space-separated list of DNS URLs, returns an array of DnsUrl
061: * objects.
062: */
063: public static DnsUrl[] fromList(String urlList)
064: throws MalformedURLException {
065:
066: DnsUrl[] urls = new DnsUrl[(urlList.length() + 1) / 2];
067: int i = 0; // next available index in urls
068: StringTokenizer st = new StringTokenizer(urlList, " ");
069:
070: while (st.hasMoreTokens()) {
071: urls[i++] = new DnsUrl(st.nextToken());
072: }
073: DnsUrl[] trimmed = new DnsUrl[i];
074: System.arraycopy(urls, 0, trimmed, 0, i);
075: return trimmed;
076: }
077:
078: public DnsUrl(String url) throws MalformedURLException {
079: super (url);
080:
081: if (!scheme.equals("dns")) {
082: throw new MalformedURLException(url
083: + " is not a valid DNS pseudo-URL");
084: }
085:
086: domain = path.startsWith("/") ? path.substring(1) : path;
087: domain = domain.equals("") ? "." : UrlUtil.decode(domain);
088:
089: // Debug
090: // System.out.println("host=" + host + " port=" + port +
091: // " domain=" + domain);
092: }
093:
094: /**
095: * Returns the domain of this URL, or "." if none is provided.
096: * Never null.
097: */
098: public String getDomain() {
099: return domain;
100: }
101:
102: /*
103: // Debug
104: public static void main(String args[]) throws MalformedURLException {
105: DnsUrl[] urls = fromList(args[0]);
106: for (int i = 0; i < urls.length; i++) {
107: System.out.println(urls[i].toString());
108: }
109: }
110: */
111: }
|