// getting DNS Records using JNDI

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;

...

   /**
     * Gets all matching dns records as an array of strings.
     *
     * @param domain domain, e.g. oberon.ark.com or oberon.com which you want
     *               the DNS records.
     * @param types  e.g."MX","A" to describe which types of record you want.
     *
     * @return ArrayList of Strings
     *
     * @throws NamingException if DNS lookup fails.
     */
    @SuppressWarnings ( {"PointlessBooleanExpression", "ConstantConditions"} )
    private static ArrayList<String> getDNSRecs( String domain,
                                                 String... types ) throws NamingException
        {
        ArrayList<String> results = new ArrayList<String>( 15 );

        //        Old Java 1.3 style required you to provide an explicit DNS server.
        //        DirContext ictx = new InitialDirContext();
        //        Attributes attrs =
        //                ictx.getAttributes( "dns://" + DNS_SERVER + "/" + domain,
        //                                    types );

        Hashtable<String,String> env = new Hashtable<String,String>();
        env.put( "java.naming.factory.initial",
                 "com.sun.jndi.dns.DnsContextFactory" );
        DirContext ictx = new InitialDirContext( env );
        Attributes attrs = ictx.getAttributes( domain, types );
        for ( Enumeration e = attrs.getAll(); e.hasMoreElements(); )
            {
            Attribute a = (Attribute) e.nextElement();
            int size = a.size();
            for ( int i = 0; i < size; i++ )
                {
                // MX string has priority (lower better) followed by associated mailserver
                // A string is just IP
                results.add( (String) a.get( i ) );
                }// end inner for
            }// end  outer for
        if ( DEBUGGING && results.size() == 0 )
            {
            System.err
                    .println( "Failed to find any DNS records for domain "
                              + domain );
            }
        return results;
        }