001/**
002 *
003 * Copyright 2013-2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util.dns.dnsjava;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.jivesoftware.smack.util.dns.DNSResolver;
023import org.jivesoftware.smack.util.dns.SRVRecord;
024import org.xbill.DNS.Lookup;
025import org.xbill.DNS.Record;
026import org.xbill.DNS.TextParseException;
027import org.xbill.DNS.Type;
028
029/**
030 * This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses.
031 *
032 */
033public class DNSJavaResolver implements DNSResolver {
034    
035    private static DNSJavaResolver instance = new DNSJavaResolver();
036    
037    private DNSJavaResolver() {
038    }
039    
040    public static DNSResolver getInstance() {
041        return instance;
042    }
043
044    @Override
045    public List<SRVRecord> lookupSRVRecords(String name) throws TextParseException {
046        List<SRVRecord> res = new ArrayList<SRVRecord>();
047
048        Lookup lookup = new Lookup(name, Type.SRV);
049        Record recs[] = lookup.run();
050        if (recs == null)
051            return res;
052
053        for (Record record : recs) {
054            org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
055            if (srvRecord != null && srvRecord.getTarget() != null) {
056                String host = srvRecord.getTarget().toString();
057                int port = srvRecord.getPort();
058                int priority = srvRecord.getPriority();
059                int weight = srvRecord.getWeight();
060
061                SRVRecord r = new SRVRecord(host, port, priority, weight);
062                res.add(r);
063            }
064        }
065
066        return res;
067    }
068}