From 609c22586514a435db19977e75b45e0936cf1264 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 22 Jun 2014 22:35:38 +0200 Subject: [PATCH] Add smack-resolver-minidns --- build.gradle | 1 + settings.gradle | 1 + .../smack/SmackConfiguration.java | 4 + .../org/jivesoftware/smack/util/DNSUtil.java | 27 ++++++ smack-resolver-minidns/build.gradle | 10 +++ .../util/dns/minidns/MiniDnsResolver.java | 84 +++++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 smack-resolver-minidns/build.gradle create mode 100644 smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsResolver.java diff --git a/build.gradle b/build.gradle index 42c3f7a1b..2e0f5e76b 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ allprojects { sonatypeStagingUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2' buildDate = (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(new Date()) oneLineDesc = 'An Open Source XMPP (Jabber) client library' + jxmppVersion = "0.1.0-alpha1-SNAPSHOT" } group = 'org.igniterealtime.smack' sourceCompatibility = 1.7 diff --git a/settings.gradle b/settings.gradle index 39b03c03d..eefb8bac3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,6 +4,7 @@ include 'smack-core', 'smack-experimental', 'smack-debug', 'smack-resolver-dnsjava', + 'smack-resolver-minidns', 'smack-resolver-javax', 'smack-compression-jzlib', 'smack-legacy', diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java index f666b9d48..2b51cd69e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java @@ -36,6 +36,7 @@ import org.jivesoftware.smack.compression.XMPPInputOutputStream; import org.jivesoftware.smack.initializer.SmackInitializer; import org.jivesoftware.smack.parsing.ExceptionThrowingCallback; import org.jivesoftware.smack.parsing.ParsingExceptionCallback; +import org.jivesoftware.smack.util.DNSUtil; import org.jivesoftware.smack.util.FileUtils; import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParser; @@ -167,6 +168,9 @@ public final class SmackConfiguration { catch (Exception e) { // Ignore. } + + // Initialize the DNS resolvers + DNSUtil.init(); } /** diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java index 0027e7a85..dcd2d60de 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java @@ -16,6 +16,8 @@ */ package org.jivesoftware.smack.util; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; @@ -39,6 +41,31 @@ public class DNSUtil { private static final Logger LOGGER = Logger.getLogger(DNSUtil.class.getName()); private static DNSResolver dnsResolver = null; + /** + * Initializes DNSUtil. This method is automatically called by SmackConfiguration, you don't + * have to call it manually. + */ + public static void init() { + final String[] RESOLVERS = new String[] { "javax.JavaxResolver", "minidns.MiniDnsResolver", + "dnsjava.DNSJavaResolver" }; + for (String resolver :RESOLVERS) { + DNSResolver availableResolver = null; + String resolverFull = "org.jivesoftware.smack.util.dns" + resolver; + try { + Class resolverClass = Class.forName(resolverFull); + Method getInstanceMethod = resolverClass.getMethod("getInstance"); + availableResolver = (DNSResolver) getInstanceMethod.invoke(null); + if (availableResolver != null) { + setDNSResolver(availableResolver); + break; + } + } + catch (ClassNotFoundException|NoSuchMethodException|SecurityException|IllegalAccessException|IllegalArgumentException|InvocationTargetException e) { + LOGGER.log(Level.FINE, "Exception on init", e); + } + } + } + /** * Set the DNS resolver that should be used to perform DNS lookups. * diff --git a/smack-resolver-minidns/build.gradle b/smack-resolver-minidns/build.gradle new file mode 100644 index 000000000..e707bb7c5 --- /dev/null +++ b/smack-resolver-minidns/build.gradle @@ -0,0 +1,10 @@ +description = """\ +DNS SRV with minidns +Use minidns for DNS SRV lookups. For platforms that don't provide the +javax.naming API (e.g. Android).""" + +dependencies { + compile project(path: ':smack-core', configuration: 'dns') + compile 'de.measite.minidns:minidns:0.1.1' + compile "org.igniterealtime.jxmpp:jxmpp-util-cache:$jxmppVersion" +} diff --git a/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsResolver.java b/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsResolver.java new file mode 100644 index 000000000..b290e96b2 --- /dev/null +++ b/smack-resolver-minidns/src/main/java/org/jivesoftware/smack/util/dns/minidns/MiniDnsResolver.java @@ -0,0 +1,84 @@ +/** + * + * Copyright 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smack.util.dns.minidns; + +import java.util.LinkedList; +import java.util.List; + +import org.jivesoftware.smack.util.dns.DNSResolver; +import org.jivesoftware.smack.util.dns.SRVRecord; +import org.jxmpp.util.cache.ExpirationCache; + +import de.measite.minidns.Client; +import de.measite.minidns.DNSCache; +import de.measite.minidns.DNSMessage; +import de.measite.minidns.Question; +import de.measite.minidns.Record; +import de.measite.minidns.Record.CLASS; +import de.measite.minidns.Record.TYPE; +import de.measite.minidns.record.SRV; + + +/** + * This implementation uses the minidns implementation for + * resolving DNS addresses. + */ +public class MiniDnsResolver implements DNSResolver { + + private static final long ONE_DAY = 24*60*60*1000; + private static final MiniDnsResolver instance = new MiniDnsResolver(); + private static final ExpirationCache cache = new ExpirationCache(10, ONE_DAY); + private final Client client; + + private MiniDnsResolver() { + client = new Client(new DNSCache() { + + @Override + public DNSMessage get(Question question) { + return cache.get(question); + } + + @Override + public void put(Question question, DNSMessage message) { + long expirationTime = ONE_DAY; + for (Record record : message.getAnswers()) { + if (record.isAnswer(question)) { + expirationTime = record.getTtl(); + break; + } + } + cache.put(question, message, expirationTime); + } + + }); + } + + public static DNSResolver getInstance() { + return instance; + } + + @Override + public List lookupSRVRecords(String name) { + List res = new LinkedList(); + DNSMessage message = client.query(name, TYPE.SRV, CLASS.IN); + for (Record record : message.getAnswers()) { + SRV srv = (SRV) record.getPayload(); + res.add(new SRVRecord(srv.getName(), srv.getPort(), srv.getPriority(), srv.getWeight())); + } + return res; + } +}