01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: *
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: /**
20: * @author Alexei Y. Zakharov
21: * @version $Revision: 1.1.2.4 $
22: */package org.apache.harmony.jndi.provider.dns;
23:
24: import java.util.Hashtable;
25:
26: import javax.naming.Context;
27: import javax.naming.NamingEnumeration;
28: import javax.naming.NamingException;
29: import javax.naming.directory.Attribute;
30: import javax.naming.directory.Attributes;
31: import javax.naming.directory.DirContext;
32: import javax.naming.directory.InitialDirContext;
33:
34: /**
35: * Simple NS lookup utility.
36: * @author Alexei Zakharov
37: * @version $Revision: 1.1.2.4 $
38: */
39: public class NSLookup {
40:
41: // add real IP of your DNS server here
42: public static final String DNS_SERVER = "127.0.0.1";
43:
44: /**
45: * Empty constructor
46: */
47: public NSLookup() {
48: }
49:
50: /**
51: * Main method.
52: * @param args the hostname we want to get information for should be given
53: * as an argument
54: */
55: public static void main(String[] args) throws NamingException {
56: Hashtable<String, String> env = new Hashtable<String, String>();
57: DirContext ctx = null;
58: Attributes attrs = null;
59: String[] attrNames = new String[1];
60: NamingEnumeration<?> attrEnum;
61:
62: if (args.length == 0) {
63: System.out
64: .println("USAGE: java "
65: + "org.apache.harmony.jndi.provider.dns.NSLookup <hostname>");
66: return;
67: }
68: env
69: .put(Context.INITIAL_CONTEXT_FACTORY,
70: "org.apache.harmony.jndi.provider.dns.DNSContextFactory");
71: env.put(Context.PROVIDER_URL, "dns://" + DNS_SERVER + "/.");
72: env.put(Context.AUTHORITATIVE, "false");
73: env.put(DNSContext.RECURSION, "true");
74: ctx = new InitialDirContext(env);
75: attrNames[0] = "A";
76: attrs = ctx.getAttributes(args[0], attrNames);
77: attrEnum = attrs.getAll();
78: while (attrEnum.hasMoreElements()) {
79: Attribute attr = (Attribute) attrEnum.nextElement();
80: NamingEnumeration<?> vals = attr.getAll();
81:
82: while (vals.hasMoreElements()) {
83: System.out.println(attr.getID() + " "
84: + vals.nextElement());
85: }
86: }
87: }
88: }
|