1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-14 03:32:06 +01:00

SMACK-225 Converted abstract class to interface, added missing hashcode method, fixed typos and some minor name changes and added licensing text

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13600 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
rcollier 2013-04-01 13:40:02 +00:00
parent 0a1e72bb5f
commit 9da54ecbce
7 changed files with 81 additions and 23 deletions

View file

@ -203,6 +203,36 @@ last release.
to use these icons outside of Smack.</li> to use these icons outside of Smack.</li>
<li>Third-party source code is licensed as noted in their source files. <li>Third-party source code is licensed as noted in their source files.
<li>Third-party binary code is licensed as follows.
<pre>
<b>dnsjava</b> <a href="http://dnsjava.org">(http://dnsjava.org)</a>
Copyright (c) 1999-2005, Brian Wellington
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the dnsjava project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre></li>
</ul> </ul>
</div> </div>

View file

@ -22,18 +22,19 @@ import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record; import org.xbill.DNS.Record;
import org.xbill.DNS.Type; import org.xbill.DNS.Type;
public class DNSJavaResolver extends DNSResolver { /**
* This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses.
*
*/
public class DNSJavaResolver implements DNSResolver {
private static DNSJavaResolver instance; private static DNSJavaResolver instance = new DNSJavaResolver();
private DNSJavaResolver() { private DNSJavaResolver() {
} }
public static DNSResolver getInstance() { public static DNSResolver getInstance() {
if (instance == null) {
instance = new DNSJavaResolver();
}
return instance; return instance;
} }

View file

@ -17,8 +17,17 @@ package org.jivesoftware.smack.util.dns;
import java.util.List; import java.util.List;
public abstract class DNSResolver { /**
* Implementations of this interface define a class that is capable of resolving DNS addresses.
*
*/
public interface DNSResolver {
public abstract List<SRVRecord> lookupSRVRecords(String name); /**
* Gets a list of service records for the specified service.
* @param name The symbolic name of the service.
* @return The list of SRV records mapped to the service name.
*/
List<SRVRecord> lookupSRVRecords(String name);
} }

View file

@ -23,10 +23,10 @@ public class HostAddress {
/** /**
* Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222
* *
* @param fqdn * @param fqdn Fully qualified domain name.
* @throws IllegalArgumentException * @throws IllegalArgumentException If the fqdn is null.
*/ */
public HostAddress(String fqdn) throws IllegalArgumentException { public HostAddress(String fqdn) {
if (fqdn == null) if (fqdn == null)
throw new IllegalArgumentException("FQDN is null"); throw new IllegalArgumentException("FQDN is null");
if (fqdn.charAt(fqdn.length() - 1) == '.') { if (fqdn.charAt(fqdn.length() - 1) == '.') {
@ -39,7 +39,14 @@ public class HostAddress {
this.port = 5222; this.port = 5222;
} }
public HostAddress(String fqdn, int port) throws IllegalArgumentException { /**
* Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222
*
* @param fqdn Fully qualified domain name.
* @param port The port to connect on.
* @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535).
*/
public HostAddress(String fqdn, int port) {
this(fqdn); this(fqdn);
if (port < 0 || port > 65535) if (port < 0 || port > 65535)
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -60,10 +67,12 @@ public class HostAddress {
this.exception = e; this.exception = e;
} }
@Override
public String toString() { public String toString() {
return fqdn + ":" + port; return fqdn + ":" + port;
} }
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {
return true; return true;
@ -80,6 +89,13 @@ public class HostAddress {
return port == address.port; return port == address.port;
} }
@Override
public int hashCode() {
int result = 1;
result = 37 * result + fqdn.hashCode();
return result * 37 + port;
}
public String getErrorMessage() { public String getErrorMessage() {
String error; String error;
if (exception == null) { if (exception == null) {

View file

@ -28,12 +28,12 @@ import javax.naming.directory.InitialDirContext;
import org.jivesoftware.smack.util.DNSUtil; import org.jivesoftware.smack.util.DNSUtil;
/** /**
* A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namepsace. * A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namespace.
* *
* @author Florian Schmaus * @author Florian Schmaus
* *
*/ */
public class JavaxResolver extends DNSResolver { public class JavaxResolver implements DNSResolver {
private static JavaxResolver instance; private static JavaxResolver instance;
private static DirContext dirContext; private static DirContext dirContext;
@ -48,14 +48,14 @@ public class JavaxResolver extends DNSResolver {
} }
// Try to set this DNS resolver as primary one // Try to set this DNS resolver as primary one
DNSUtil.setDNSResolver(maybeGetInstance()); DNSUtil.setDNSResolver(getInstance());
} }
private JavaxResolver() { private JavaxResolver() {
} }
public static DNSResolver maybeGetInstance() { public static DNSResolver getInstance() {
if (instance == null && isSupported()) { if (instance == null && isSupported()) {
instance = new JavaxResolver(); instance = new JavaxResolver();
} }

View file

@ -29,13 +29,13 @@ public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
/** /**
* Create a new SRVRecord * Create a new SRVRecord
* *
* @param fqdn * @param fqdn Fully qualified domain name
* @param port * @param port The connection port
* @param priority * @param priority Priority of the target host
* @param weight * @param weight Relative weight for records with same priority
* @throws IllegalArgumentException * @throws IllegalArgumentException fqdn is null or any other field is not in valid range (0-65535).
*/ */
public SRVRecord(String fqdn, int port, int priority, int weight) throws IllegalArgumentException { public SRVRecord(String fqdn, int port, int priority, int weight) {
super(fqdn, port); super(fqdn, port);
if (weight < 0 || weight > 65535) if (weight < 0 || weight > 65535)
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -60,6 +60,7 @@ public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
return weight; return weight;
} }
@Override
public int compareTo(SRVRecord other) { public int compareTo(SRVRecord other) {
// According to RFC2782, // According to RFC2782,
// "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach". // "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach".
@ -71,6 +72,7 @@ public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
return res; return res;
} }
@Override
public String toString() { public String toString() {
return super.toString() + " prio:" + priority + ":w:" + weight; return super.toString() + " prio:" + priority + ":w:" + weight;
} }

View file

@ -24,7 +24,7 @@ public class DNSUtilTest {
@Test @Test
public void xmppClientDomainJavaXTest() { public void xmppClientDomainJavaXTest() {
DNSResolver resolver = JavaxResolver.maybeGetInstance(); DNSResolver resolver = JavaxResolver.getInstance();
assertNotNull(resolver); assertNotNull(resolver);
DNSUtil.setDNSResolver(resolver); DNSUtil.setDNSResolver(resolver);
xmppClientDomainTest(); xmppClientDomainTest();
@ -32,7 +32,7 @@ public class DNSUtilTest {
@Test @Test
public void xmppServerDomainJavaXTest() { public void xmppServerDomainJavaXTest() {
DNSResolver resolver = JavaxResolver.maybeGetInstance(); DNSResolver resolver = JavaxResolver.getInstance();
assertNotNull(resolver); assertNotNull(resolver);
DNSUtil.setDNSResolver(resolver); DNSUtil.setDNSResolver(resolver);
xmppServerDomainTest(); xmppServerDomainTest();