public static int doLookup ( String hostName , String dnsType ) throws DNSLookupException { // JNDI cannot take two-byte chars, so we convert the hostname into Punycode hostName = UniPunyCode . toPunycodeIfPossible ( hostName ) ; Hashtable < String , String > env = new Hashtable < String , String > ( ) ; env . put ( "java.naming.factory.initial" , "com.sun.jndi.dns.DnsContextFactory" ) ; DirContext ictx ; try { ictx = new InitialDirContext ( env ) ; } catch ( NamingException e ) { throw new DNSInitialContextException ( e ) ; } Attributes attrs ; try { attrs = ictx . getAttributes ( hostName , new String [ ] { dnsType } ) ; } catch ( NameNotFoundException e ) { // The hostname was not found or is invalid return - 1 ; } catch ( InvalidAttributeIdentifierException e ) { // The DNS type is invalid throw new DNSInvalidTypeException ( e ) ; } catch ( NamingException e ) { // Unknown reason throw new DNSLookupException ( e ) ; } Attribute attr = attrs . get ( dnsType ) ; if ( attr == null ) { return 0 ; } return attr . size ( ) ; }
Counts the number of records found for hostname and the specific type . Outputs 0 if no record is found or - 1 if the hostname is unknown invalid!