diff options
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | build.xml | 79 | ||||
-rw-r--r-- | src/com/isode/stroke/network/PlatformDomainNameResolver.java | 15 | ||||
-rw-r--r-- | src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java | 88 |
5 files changed, 96 insertions, 96 deletions
@@ -1,6 +1,6 @@ all: dist/lib/stroke.jar -DEFINES = -Dxpp-dir=third-party/xpp -Djzlib-dir=third-party/jzlib -Dicu4j-dir=third-party/ -Dstax2-dir=third-party/stax2/ -Daalto-dir=third-party/aalto/ +DEFINES = -Dxpp-dir=third-party/xpp -Djzlib-dir=third-party/jzlib -Dicu4j-dir=third-party/ -Dstax2-dir=third-party/stax2/ -Daalto-dir=third-party/aalto/ -Ddnsjava-dir=third-party/dnsjava JUNIT ?= /usr/share/junit/junit.jar .PHONY : clean @@ -13,7 +13,7 @@ distclean: clean rm -rf third-party .PHONY : dist/lib/stroke.jar -dist/lib/stroke.jar: third-party/jzlib/jzlib.jar third-party/icu4j.jar third-party/aalto/aalto-xml.jar third-party/stax2/stax2-api.jar +dist/lib/stroke.jar: third-party/jzlib/jzlib.jar third-party/icu4j.jar third-party/aalto/aalto-xml.jar third-party/stax2/stax2-api.jar third-party/dnsjava/dnsjava.jar ant ${DEFINES} .PHONY : test @@ -40,6 +40,10 @@ third-party/icu4j.jar: mkdir -p third-party curl http://download.icu-project.org/files/icu4j/4.8.1/icu4j-4_8_1.jar -o third-party/icu4j.jar +third-party/dnsjava/dnsjava.jar: + mkdir -p third-party/dnsjava + curl http://www.dnsjava.org/download/dnsjava-2.1.6.jar -o third-party/dnsjava/dnsjava.jar + third-party/cobertura/cobertura.jar: mkdir -p third-party curl -L 'http://sourceforge.net/projects/cobertura/files/cobertura/1.9.4.1/cobertura-1.9.4.1-bin.tar.bz2/download' -o third-party/cobertura-1.9.4.1-bin.tar.bz2 @@ -9,6 +9,8 @@ It also depends upon http://www.jcraft.com/jzlib/, which is passed to ant in the It also depends upon icu4j from http://site.icu-project.org/ +It also depends upon dnsjava from http://www.dnsjava.org/ + To build, run: ant -Dxpp-dir=third-party/xpp -Djzlib-dir=third-party/jzlib -Dicu4j-dir=third-party/ -Dstax2-dir=third-party/stax2/ -Daalto-dir=third-party/aalto/ @@ -1,5 +1,5 @@ <!-- - * Copyright (c) 2010, Isode Limited, London, England. + * Copyright (c) 2010-2013, Isode Limited, London, England. * All rights reserved. --> <project name="Stroke" default="dist" basedir="."> @@ -20,16 +20,17 @@ <property name="stax2-dir" value="../third-party/stax2"/> <property name="jzlib-dir" value="../third-party/jzlib"/> <property name="icu4j-dir" value="../third-party/icu4j"/> + <property name="dnsjava-dir" value="../third-party/dnsjava"/> <property name="cobertura.dir" value="instrumented"/> <property name="coveragereport.dir" value="coverage"/> <path id="cobertura.classpath"> - <pathelement path="${cobertura-jar}"/> - <pathelement path="${jakarta-oro-jar}"/> - <pathelement path="${log4j-jar}"/> - <pathelement path="${asm-jar}"/> - <pathelement path="${asm-tree-jar}"/> + <pathelement path="${cobertura-jar}"/> + <pathelement path="${jakarta-oro-jar}"/> + <pathelement path="${log4j-jar}"/> + <pathelement path="${asm-jar}"/> + <pathelement path="${asm-tree-jar}"/> </path> <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/> @@ -38,6 +39,7 @@ <fileset dir="${stax2-dir}" includes="stax2-api.jar"/> <fileset dir="${jzlib-dir}" includes="jzlib.jar"/> <fileset dir="${icu4j-dir}" includes="icu4j.jar"/> + <fileset dir="${dnsjava-dir}" includes="dnsjava.jar"/> </path> <target name="init"> <tstamp/> @@ -92,7 +94,7 @@ </target> -<target name="compile-tests" depends="dist" + <target name="compile-tests" depends="dist" description="compile the test sources " > <javac srcdir="${src.tests}" destdir="${src.tests}" debug="${compile.debug}" @@ -120,37 +122,36 @@ <delete dir="${test.results}"/> <mkdir dir="${test.results}"/> <junit fork="yes"> - <formatter type="xml"/> - <classpath> - <pathelement location="${cobertura.dir}"/> - <path refid="cobertura.classpath"/> - </classpath> - <classpath> - <pathelement location="${JUNIT_JAR}"/> - <pathelement location="${jar}"/> - <pathelement location="${src.tests}"/> - <path refid="classpath"/> - </classpath> - <batchtest todir="${test.results}"> - <fileset dir="${src.tests}"> - <include name="**/*Test.java"/> - <!--<exclude name="**/AllTests.java"/>--> - </fileset> - </batchtest> + <formatter type="xml"/> + <classpath> + <pathelement location="${cobertura.dir}"/> + <path refid="cobertura.classpath"/> + </classpath> + <classpath> + <pathelement location="${JUNIT_JAR}"/> + <pathelement location="${jar}"/> + <pathelement location="${src.tests}"/> + <path refid="classpath"/> + </classpath> + <batchtest todir="${test.results}"> + <fileset dir="${src.tests}"> + <include name="**/*Test.java"/> + <!--<exclude name="**/AllTests.java"/>--> + </fileset> + </batchtest> </junit> - </target> <target name="coverage" if="cobertura-jar" depends="instrument, run-tests"> <delete dir="${coveragereport.dir}"/> <mkdir dir="${coveragereport.dir}"/> <cobertura-report format="xml" destdir="${coveragereport.dir}"> - <fileset dir="${src}"> - <include name="**/*.java"/> - </fileset> - <fileset dir="${src.tests}"> - <include name="**/*.java"/> - </fileset> + <fileset dir="${src}"> + <include name="**/*.java"/> + </fileset> + <fileset dir="${src.tests}"> + <include name="**/*.java"/> + </fileset> </cobertura-report> </target> @@ -190,7 +191,7 @@ </target> <target name="clean" - description="clean up" > + description="clean up" > <delete dir="${build}"/> <delete dir="${test.results}"/> <delete dir="${dist}"/> @@ -210,18 +211,18 @@ <!--<classpath> <fileset dir="${xpp-dir}" includes="xpp.jar"/> </classpath>--> - <link href="http://docs.oracle.com/javase/6/docs/api/"/> + <link href="http://docs.oracle.com/javase/6/docs/api/"/> </javadoc> </target> <target name="run" description="Run the demo" depends="dist"> - <java fork="true" classname="${main-class}"> - <classpath> - <path refid="classpath"/> - <path location="${jar}"/> - </classpath> - </java> + <java fork="true" classname="${main-class}"> + <classpath> + <path refid="classpath"/> + <path location="${jar}"/> + </classpath> + </java> </target> </project> diff --git a/src/com/isode/stroke/network/PlatformDomainNameResolver.java b/src/com/isode/stroke/network/PlatformDomainNameResolver.java index 3eef682..32f466d 100644 --- a/src/com/isode/stroke/network/PlatformDomainNameResolver.java +++ b/src/com/isode/stroke/network/PlatformDomainNameResolver.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2012, Isode Limited, London, England. + * Copyright (c) 2010-2013, Isode Limited, London, England. * All rights reserved. */ /* @@ -16,6 +16,8 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collection; +import org.xbill.DNS.Address; + public class PlatformDomainNameResolver extends DomainNameResolver { private class AddressQuery extends DomainNameAddressQuery implements EventOwner { @@ -25,10 +27,11 @@ public class PlatformDomainNameResolver extends DomainNameResolver { public void run() { final Collection<HostAddress> results = new ArrayList<HostAddress>(); try { - for (InetAddress result : InetAddress.getAllByName(hostname)) { + for (InetAddress result : Address.getAllByName(hostname)) { results.add(new HostAddress(result)); } } catch (UnknownHostException ex) { + /* results remains empty */ } eventLoop.postEvent(new Callback() { public void run() { @@ -53,17 +56,17 @@ public class PlatformDomainNameResolver extends DomainNameResolver { } public PlatformDomainNameResolver(EventLoop eventLoop) { - this.eventLoop = eventLoop; + this.eventLoop_ = eventLoop; } @Override public DomainNameServiceQuery createServiceQuery(String name) { - return new PlatformDomainNameServiceQuery(getNormalized(name), eventLoop); + return new PlatformDomainNameServiceQuery(getNormalized(name), eventLoop_); } @Override public DomainNameAddressQuery createAddressQuery(String name) { - return new AddressQuery(getNormalized(name), eventLoop); + return new AddressQuery(getNormalized(name), eventLoop_); } - private final EventLoop eventLoop; + private final EventLoop eventLoop_; } diff --git a/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java b/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java index 0fc7976..37cf1f2 100644 --- a/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java +++ b/src/com/isode/stroke/network/PlatformDomainNameServiceQuery.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2012, Isode Limited, London, England. + * Copyright (c) 2010-2013, Isode Limited, London, England. * All rights reserved. */ /* @@ -9,79 +9,69 @@ */ package com.isode.stroke.network; +import java.util.ArrayList; +import java.util.Collection; + +import org.xbill.DNS.Lookup; +import org.xbill.DNS.Record; +import org.xbill.DNS.SRVRecord; +import org.xbill.DNS.TextParseException; +import org.xbill.DNS.Type; import com.isode.stroke.eventloop.Event.Callback; import com.isode.stroke.eventloop.EventLoop; -import com.isode.stroke.eventloop.EventOwner; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Hashtable; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; +import com.isode.stroke.network.DomainNameServiceQuery; -public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery implements EventOwner { +public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery { + private final String service; + private final EventLoop eventLoop; - private class QueryThread extends Thread { + public PlatformDomainNameServiceQuery(final String service, final EventLoop eventLoop) { + this.service = service; + this.eventLoop = eventLoop; + } + private class QueryThread extends Thread { @Override public void run() { final Collection<Result> results = new ArrayList<Result>(); - Hashtable env = new Hashtable(); - env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); - env.put("java.naming.provider.url", "dns:"); - DirContext ctx = null; + Lookup request; try { - ctx = new InitialDirContext(env); - Attributes attrs = ctx.getAttributes(service, new String[]{"SRV"}); - Attribute attribute = attrs.get("SRV"); - for (int i = 0; attribute != null && i < attribute.size(); i++) { - /* SRV results are going to be returned in the space-separated format - * Priority Weight Port Target - * (See RFC2782) - */ - String[] srvParts = ((String) attribute.get(i)).split(" "); - String host = srvParts[3]; - if (host.endsWith(".")) { - host = host.substring(0, host.length() - 1); + request = new Lookup(service, Type.SRV); + final Record[] records = request.run(); + if (records != null) { + for (final Record record : records) { + /* It's only anticipated that SRVRecords will be + * returned, but check first + */ + if (record instanceof SRVRecord) { + final SRVRecord srv = (SRVRecord) record; + final Result result = new Result(srv.getTarget() + .toString(), srv.getPort(), srv.getPriority(), + srv.getWeight()); + results.add(result); + } } - Result result = new Result(host, Integer.parseInt(srvParts[2]), Integer.parseInt(srvParts[0]), Integer.parseInt(srvParts[1])); - results.add(result); } - } catch (NamingException ex) { - /* Turns out that you get the exception just for not finding a result, so we want to fall through to A lookups and ignore.*/ + } catch (final TextParseException e) { + /* Lookup failed because "service" was not a valid DNS name; + * leave "results" empty + */ } eventLoop.postEvent(new Callback() { + @Override public void run() { onResult.emit(results); } }); - //close the context as otherwise this will lead to open sockets in - //CLOSE_WAIT condition - if(ctx != null) { - try { - ctx.close(); - } catch (NamingException e) { - //at least we try to close the context - } - } } } - public PlatformDomainNameServiceQuery(String service, EventLoop eventLoop) { - this.service = service; - this.eventLoop = eventLoop; - } - @Override public void run() { - QueryThread thread = new QueryThread(); + final QueryThread thread = new QueryThread(); thread.setDaemon(true); thread.start(); } - private final String service; - private final EventLoop eventLoop; } |