1 package SnowMailClient.MailEngine; 2 3 4 import javax.naming.*; 5 import javax.naming.directory.*; 6 import java.net.*; 7 import java.util.*; 8 9 10 public final class MXReader 11 { 12 13 14 public static final Vector<MXRecord> getMXRecordWithoutCache(String domainName) throws Exception 15 { 16 final Vector<MXRecord> records = new Vector<MXRecord>(); 17 18 DirContext ictx = new InitialDirContext(); 19 Attributes attributes = ictx.getAttributes("dns:/"+domainName, new String [] {"MX"}); 20 if (attributes != null) 21 { 22 NamingEnumeration attribNames = attributes.getIDs(); 23 while (attribNames.hasMore()) 24 { 25 String name = (String ) attribNames.next(); 26 Attribute attr = attributes.get(name); 27 System.out.println(attr); 28 for(int i=0; i<attr.size(); i++) 29 { 30 Scanner s = new Scanner(""+attr.get(i)); 31 int priority = s.nextInt(); 32 String server = s.next(); 33 if(server.endsWith(".")) server = server.substring(0, server.length()-1); 34 records.add( new MXRecord(priority, server) ); 35 } 36 } 37 } 38 39 Collections.sort( records ); 40 41 return records; 42 } 43 44 45 public static void main(String [] a) 46 { 47 try 48 { 49 System.out.println( getMXRecordWithoutCache("gmx.ch") ); 50 System.out.println( getMXRecordWithoutCache("bluewin.ch") ); 51 System.out.println( getMXRecordWithoutCache("snowraver.org") ); 52 } 53 catch(Exception e) 54 { 55 e.printStackTrace(); 56 } 57 } 58 59 public final static class MXRecord implements Comparable <MXRecord> 60 { 61 final public int priority; 62 final public String server; 63 64 public MXRecord(int priority, String server) 65 { 66 this.priority = priority; 67 this.server = server; 68 } 69 70 public final int compareTo(MXRecord mr) 71 { 72 if(mr.priority > priority) return 1; 74 if(mr.priority < priority) return -1; 75 return 0; 76 } 77 78 public String toString() 79 { 80 return server+" ("+priority+")"; 81 } 82 } 83 84 85 } | Popular Tags |