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 );
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++ )
{
results.add( (String) a.get( i ) );
}
}
if ( DEBUGGING && results.size() == 0 )
{
System.err
.println( "Failed to find any DNS records for domain "
+ domain );
}
return results;
}