You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/05/18 13:25:38 UTC
svn commit: r1124194 - in /james/server/trunk/dnsservice-library/src:
main/java/org/apache/james/dnsservice/library/MXHostAddressIterator.java
test/java/org/apache/james/dnsservice/library/MXHostAddressIteratorTest.java
Author: norman
Date: Wed May 18 11:25:37 2011
New Revision: 1124194
URL: http://svn.apache.org/viewvc?rev=1124194&view=rev
Log:
Make sure the MXHostAddressIterator offer entries for every given MX. See JAMES-1251
Added:
james/server/trunk/dnsservice-library/src/test/java/org/apache/james/dnsservice/library/MXHostAddressIteratorTest.java
Modified:
james/server/trunk/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/MXHostAddressIterator.java
Modified: james/server/trunk/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/MXHostAddressIterator.java
URL: http://svn.apache.org/viewvc/james/server/trunk/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/MXHostAddressIterator.java?rev=1124194&r1=1124193&r2=1124194&view=diff
==============================================================================
--- james/server/trunk/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/MXHostAddressIterator.java (original)
+++ james/server/trunk/dnsservice-library/src/main/java/org/apache/james/dnsservice/library/MXHostAddressIterator.java Wed May 18 11:25:37 2011
@@ -20,7 +20,9 @@ package org.apache.james.dnsservice.libr
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import org.apache.james.dnsservice.api.DNSService;
import org.apache.mailet.HostAddress;
@@ -49,6 +51,8 @@ public class MXHostAddressIterator imple
this.useSingleIP = useSingleIP;
this.logger = logger;
this.defaultPort = defaultPort;
+
+ init();
}
/*
@@ -57,70 +61,53 @@ public class MXHostAddressIterator imple
* @see java.util.Iterator#hasNext()
*/
public boolean hasNext() {
- /*
- * Make sure that when next() is called, that we can provide a
- * HostAddress. This means that we need to have an inner iterator, and
- * verify that it has addresses. We could, for example, run into a
- * situation where the next mxHost didn't have any valid addresses.
- */
- if ((addresses == null || !addresses.hasNext()) && hosts.hasNext())
- do {
- String nextHostname = (String) hosts.next();
- final String hostname;
- final String port;
-
- int idx = nextHostname.indexOf(':');
- if (idx > 0) {
- port = nextHostname.substring(idx + 1);
- hostname = nextHostname.substring(0, idx);
- } else {
- hostname = nextHostname;
- port = defaultPort + "";
- }
+
+ return addresses.hasNext();
+ }
- InetAddress[] addrs = null;
- try {
- if (useSingleIP) {
- addrs = new InetAddress[] { dns.getByName(hostname) };
- } else {
- addrs = dns.getAllByName(hostname);
- }
- } catch (UnknownHostException uhe) {
- // this should never happen, since we just got
- // this host from mxHosts, which should have
- // already done this check.
- StringBuffer logBuffer = new StringBuffer(128).append("Couldn't resolve IP address for discovered host ").append(hostname).append(".");
- logger.error(logBuffer.toString());
+ private void init() {
+ final List<HostAddress> hAddresses = new ArrayList<HostAddress>();
+ while (hosts.hasNext()) {
+ String nextHostname = (String) hosts.next();
+ final String hostname;
+ final String port;
+
+ int idx = nextHostname.indexOf(':');
+ if (idx > 0) {
+ port = nextHostname.substring(idx + 1);
+ hostname = nextHostname.substring(0, idx);
+ } else {
+ hostname = nextHostname;
+ port = defaultPort + "";
+ }
+
+ InetAddress[] addrs = null;
+ try {
+ if (useSingleIP) {
+ addrs = new InetAddress[] { dns.getByName(hostname) };
+ } else {
+ addrs = dns.getAllByName(hostname);
}
- final InetAddress[] ipAddresses = addrs;
-
- addresses = new Iterator<HostAddress>() {
- int i = 0;
-
- public boolean hasNext() {
- return ipAddresses != null && i < ipAddresses.length;
- }
-
- public HostAddress next() {
- return new org.apache.mailet.HostAddress(hostname, "smtp://" + ipAddresses[i++].getHostAddress() + ":" + port);
- }
-
- public void remove() {
- throw new UnsupportedOperationException("remove not supported by this iterator");
- }
- };
- } while (!addresses.hasNext() && hosts.hasNext());
-
- return addresses != null && addresses.hasNext();
+ } catch (UnknownHostException uhe) {
+ // this should never happen, since we just got
+ // this host from mxHosts, which should have
+ // already done this check.
+ StringBuffer logBuffer = new StringBuffer(128).append("Couldn't resolve IP address for discovered host ").append(hostname).append(".");
+ logger.error(logBuffer.toString());
+ }
+ for (int i = 0; i < addrs.length; i++) {
+ hAddresses.add(new org.apache.mailet.HostAddress(hostname, "smtp://" + addrs[i].getHostAddress() + ":" + port));
+ }
+ }
+ addresses = hAddresses.iterator();
}
-
/*
* (non-Javadoc)
*
* @see java.util.Iterator#next()
*/
public HostAddress next() {
- return addresses != null ? addresses.next() : null;
+ return addresses.next();
}
/**
Added: james/server/trunk/dnsservice-library/src/test/java/org/apache/james/dnsservice/library/MXHostAddressIteratorTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/dnsservice-library/src/test/java/org/apache/james/dnsservice/library/MXHostAddressIteratorTest.java?rev=1124194&view=auto
==============================================================================
--- james/server/trunk/dnsservice-library/src/test/java/org/apache/james/dnsservice/library/MXHostAddressIteratorTest.java (added)
+++ james/server/trunk/dnsservice-library/src/test/java/org/apache/james/dnsservice/library/MXHostAddressIteratorTest.java Wed May 18 11:25:37 2011
@@ -0,0 +1,82 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you 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.apache.james.dnsservice.library;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.dnsservice.api.TemporaryResolutionException;
+import org.slf4j.LoggerFactory;
+
+import junit.framework.TestCase;
+
+public class MXHostAddressIteratorTest extends TestCase{
+
+ /**
+ * Test case for JAMES-1251
+ */
+ public void testIteratorContainMultipleMX() {
+ DNSService dns = new DNSService() {
+
+ public InetAddress getLocalHost() throws UnknownHostException {
+ throw new UnsupportedOperationException();
+ }
+
+ public String getHostName(InetAddress addr) {
+ throw new UnsupportedOperationException();
+ }
+
+ public InetAddress getByName(String host) throws UnknownHostException {
+ return InetAddress.getLocalHost();
+ }
+
+ /**
+ * Every time this method is called it will return two InetAddress instances
+ */
+ public InetAddress[] getAllByName(String host) throws UnknownHostException {
+ InetAddress addr = InetAddress.getLocalHost();
+ return new InetAddress[] {addr, addr};
+ }
+
+ public Collection<String> findTXTRecords(String hostname) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Collection<String> findMXRecords(String hostname) throws TemporaryResolutionException {
+ throw new UnsupportedOperationException();
+ }
+ };
+ MXHostAddressIterator it = new MXHostAddressIterator(Arrays.asList("localhost", "localhost2").iterator(), dns, false, LoggerFactory.getLogger(this.getClass()));
+ for (int i = 0; i < 4; i++) {
+ assertTrue(it.hasNext());
+ assertNotNull(it.next());
+ }
+ assertFalse(it.hasNext());
+
+ it = new MXHostAddressIterator(Arrays.asList("localhost", "localhost2").iterator(), dns, true, LoggerFactory.getLogger(this.getClass()));
+ for (int i = 0; i < 2; i++) {
+ assertTrue(it.hasNext());
+ assertNotNull(it.next());
+ }
+ assertFalse(it.hasNext());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org