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.initializer.SmackInitializer; 023import org.jivesoftware.smack.util.DNSUtil; 024import org.jivesoftware.smack.util.dns.DNSResolver; 025import org.jivesoftware.smack.util.dns.SRVRecord; 026import org.xbill.DNS.Lookup; 027import org.xbill.DNS.Record; 028import org.xbill.DNS.TextParseException; 029import org.xbill.DNS.Type; 030 031/** 032 * This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses. 033 * 034 */ 035public class DNSJavaResolver implements SmackInitializer, DNSResolver { 036 037 private static DNSJavaResolver instance = new DNSJavaResolver(); 038 039 public static DNSResolver getInstance() { 040 return instance; 041 } 042 043 @Override 044 public List<SRVRecord> lookupSRVRecords(String name) throws TextParseException { 045 List<SRVRecord> res = new ArrayList<SRVRecord>(); 046 047 Lookup lookup = new Lookup(name, Type.SRV); 048 Record[] recs = lookup.run(); 049 if (recs == null) 050 return res; 051 052 for (Record record : recs) { 053 org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record; 054 if (srvRecord != null && srvRecord.getTarget() != null) { 055 String host = srvRecord.getTarget().toString(); 056 int port = srvRecord.getPort(); 057 int priority = srvRecord.getPriority(); 058 int weight = srvRecord.getWeight(); 059 060 SRVRecord r = new SRVRecord(host, port, priority, weight); 061 res.add(r); 062 } 063 } 064 065 return res; 066 } 067 068 public static void setup() { 069 DNSUtil.setDNSResolver(getInstance()); 070 } 071 072 @Override 073 public List<Exception> initialize() { 074 setup(); 075 return null; 076 } 077 078}