1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

Make resolver-dnsjava, -minidns initializer

and OSGi components.

There is really no need to treat them different then
resolver-javax. This also allows the removal of the DNSUtil.init() method.
This commit is contained in:
Florian Schmaus 2014-08-11 19:21:43 +02:00
parent a5eebf3840
commit 0c0737942c
10 changed files with 55 additions and 50 deletions

View file

@ -238,7 +238,7 @@ subprojects {
} }
} }
['smack-resolver-javax', 'smack-sasl-javax', 'smack-sasl-provided'].each { name -> ['smack-resolver-javax', 'smack-resolver-dnsjava', 'smack-resolver-minidns', 'smack-sasl-javax', 'smack-sasl-provided'].each { name ->
project(":$name") { project(":$name") {
jar { jar {
manifest { manifest {

View file

@ -38,7 +38,6 @@ import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import org.jivesoftware.smack.initializer.SmackInitializer; import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.parsing.ExceptionThrowingCallback; import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback; import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.FileUtils;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -169,9 +168,6 @@ public final class SmackConfiguration {
catch (Exception e) { catch (Exception e) {
// Ignore. // Ignore.
} }
// Initialize the DNS resolvers
DNSUtil.init();
} }
/** /**

View file

@ -16,6 +16,8 @@
*/ */
package org.jivesoftware.smack.initializer; package org.jivesoftware.smack.initializer;
import java.util.List;
public abstract class SmackAndOsgiInitializer implements SmackInitializer { public abstract class SmackAndOsgiInitializer implements SmackInitializer {
/** /**
@ -25,4 +27,10 @@ public abstract class SmackAndOsgiInitializer implements SmackInitializer {
public final void activate() { public final void activate() {
initialize(); initialize();
} }
@Override
public List<Exception> initialize(ClassLoader classLoader) {
return initialize();
}
} }

View file

@ -16,8 +16,6 @@
*/ */
package org.jivesoftware.smack.util; package org.jivesoftware.smack.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
@ -41,31 +39,6 @@ public class DNSUtil {
private static final Logger LOGGER = Logger.getLogger(DNSUtil.class.getName()); private static final Logger LOGGER = Logger.getLogger(DNSUtil.class.getName());
private static DNSResolver dnsResolver = null; 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. * Set the DNS resolver that should be used to perform DNS lookups.
* *

View file

@ -9,6 +9,8 @@
<optionalStartupClasses> <optionalStartupClasses>
<className>org.jivesoftware.smack.util.dns.javax.JavaxResolver</className> <className>org.jivesoftware.smack.util.dns.javax.JavaxResolver</className>
<className>org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver</className>
<className>org.jivesoftware.smack.util.dns.dnsjava.DNSJavaResolver</className>
<className>org.jivesoftware.smack.initializer.extensions.ExtensionsInitializer</className> <className>org.jivesoftware.smack.initializer.extensions.ExtensionsInitializer</className>
<className>org.jivesoftware.smack.initializer.experimental.ExperimentalInitializer</className> <className>org.jivesoftware.smack.initializer.experimental.ExperimentalInitializer</className>
<className>org.jivesoftware.smack.initializer.legacy.LegacyInitializer</className> <className>org.jivesoftware.smack.initializer.legacy.LegacyInitializer</className>

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smack.util.dns.dnsjava;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.initializer.SmackAndOsgiInitializer;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.dns.DNSResolver; import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.dns.SRVRecord; import org.jivesoftware.smack.util.dns.SRVRecord;
import org.xbill.DNS.Lookup; import org.xbill.DNS.Lookup;
@ -30,13 +32,10 @@ import org.xbill.DNS.Type;
* This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses. * This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses.
* *
*/ */
public class DNSJavaResolver implements DNSResolver { public class DNSJavaResolver extends SmackAndOsgiInitializer implements DNSResolver {
private static DNSJavaResolver instance = new DNSJavaResolver(); private static DNSJavaResolver instance = new DNSJavaResolver();
private DNSJavaResolver() {
}
public static DNSResolver getInstance() { public static DNSResolver getInstance() {
return instance; return instance;
} }
@ -65,4 +64,15 @@ public class DNSJavaResolver implements DNSResolver {
return res; return res;
} }
public static void setup() {
DNSUtil.setDNSResolver(getInstance());
}
@Override
public List<Exception> initialize() {
setup();
return null;
}
} }

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"
enabled="true" immediate="true" name="Smack Resolver JavaX API">
<implementation
class="org.jivesoftware.smack.util.dns.dnsjava.DNSJavaResolver" />
</scr:component>

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smack.util.dns.javax; package org.jivesoftware.smack.util.dns.javax;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
@ -57,10 +56,6 @@ public class JavaxResolver extends SmackAndOsgiInitializer implements DNSResolve
setup(); setup();
} }
public JavaxResolver() {
}
public static synchronized DNSResolver getInstance() { public static synchronized DNSResolver getInstance() {
if (instance == null && isSupported()) { if (instance == null && isSupported()) {
instance = new JavaxResolver(); instance = new JavaxResolver();
@ -102,12 +97,8 @@ public class JavaxResolver extends SmackAndOsgiInitializer implements DNSResolve
@Override @Override
public List<Exception> initialize() { public List<Exception> initialize() {
return initialize(null); setup();
return null;
} }
@Override
public List<Exception> initialize(ClassLoader classLoader) {
setup();
return Collections.emptyList();
}
} }

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smack.util.dns.minidns;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.initializer.SmackAndOsgiInitializer;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.dns.DNSResolver; import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.dns.SRVRecord; import org.jivesoftware.smack.util.dns.SRVRecord;
import org.jxmpp.util.cache.ExpirationCache; import org.jxmpp.util.cache.ExpirationCache;
@ -37,14 +39,14 @@ import de.measite.minidns.record.SRV;
* This implementation uses the <a href="https://github.com/rtreffer/minidns/">minidns</a> implementation for * This implementation uses the <a href="https://github.com/rtreffer/minidns/">minidns</a> implementation for
* resolving DNS addresses. * resolving DNS addresses.
*/ */
public class MiniDnsResolver implements DNSResolver { public class MiniDnsResolver extends SmackAndOsgiInitializer implements DNSResolver {
private static final long ONE_DAY = 24*60*60*1000; private static final long ONE_DAY = 24*60*60*1000;
private static final MiniDnsResolver instance = new MiniDnsResolver(); private static final MiniDnsResolver instance = new MiniDnsResolver();
private static final ExpirationCache<Question, DNSMessage> cache = new ExpirationCache<Question, DNSMessage>(10, ONE_DAY); private static final ExpirationCache<Question, DNSMessage> cache = new ExpirationCache<Question, DNSMessage>(10, ONE_DAY);
private final Client client; private final Client client;
private MiniDnsResolver() { public MiniDnsResolver() {
client = new Client(new DNSCache() { client = new Client(new DNSCache() {
@Override @Override
@ -81,4 +83,15 @@ public class MiniDnsResolver implements DNSResolver {
} }
return res; return res;
} }
public static void setup() {
DNSUtil.setDNSResolver(getInstance());
}
@Override
public List<Exception> initialize() {
setup();
return null;
}
} }

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"
enabled="true" immediate="true" name="Smack Resolver JavaX API">
<implementation
class="org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver" />
</scr:component>