Home | History | Annotate | Download | only in dns
      1 /**
      2  * Copyright 2013 Florian Schmaus
      3  *
      4  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.jivesoftware.smack.util.dns;
     17 
     18 import java.util.ArrayList;
     19 import java.util.List;
     20 
     21 import org.xbill.DNS.Lookup;
     22 import org.xbill.DNS.Record;
     23 import org.xbill.DNS.Type;
     24 
     25 /**
     26  * This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses.
     27  *
     28  */
     29 public class DNSJavaResolver implements DNSResolver {
     30 
     31     private static DNSJavaResolver instance = new DNSJavaResolver();
     32 
     33     private DNSJavaResolver() {
     34 
     35     }
     36 
     37     public static DNSResolver getInstance() {
     38         return instance;
     39     }
     40 
     41     @Override
     42     public List<SRVRecord> lookupSRVRecords(String name) {
     43         List<SRVRecord> res = new ArrayList<SRVRecord>();
     44 
     45         try {
     46             Lookup lookup = new Lookup(name, Type.SRV);
     47             Record recs[] = lookup.run();
     48             if (recs == null)
     49                 return res;
     50 
     51             for (Record record : recs) {
     52                 org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
     53                 if (srvRecord != null && srvRecord.getTarget() != null) {
     54                     String host = srvRecord.getTarget().toString();
     55                     int port = srvRecord.getPort();
     56                     int priority = srvRecord.getPriority();
     57                     int weight = srvRecord.getWeight();
     58 
     59                     SRVRecord r;
     60                     try {
     61                         r = new SRVRecord(host, port, priority, weight);
     62                     } catch (Exception e) {
     63                         continue;
     64                     }
     65                     res.add(r);
     66                 }
     67             }
     68 
     69         } catch (Exception e) {
     70         }
     71         return res;
     72     }
     73 }
     74