Author: norman
Date: Fri Jan 7 09:25:12 2011
New Revision: 1056224
URL: http://svn.apache.org/viewvc?rev=1056224&view=rev
Log:
Remove some duplicated code by factor out to seperate util class
Added:
james/server/trunk/util/src/main/java/org/apache/james/util/MXHostAddressIterator.java
Modified:
james/server/trunk/dnsservice-dnsjava/pom.xml
james/server/trunk/dnsservice-dnsjava/src/main/java/org/apache/james/dnsservice/dnsjava/DNSJavaService.java
james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
Modified: james/server/trunk/dnsservice-dnsjava/pom.xml
URL:
http://svn.apache.org/viewvc/james/server/trunk/dnsservice-dnsjava/pom.xml?rev=1056224&r1=1056223&r2=1056224&view=diff
==============================================================================
--- james/server/trunk/dnsservice-dnsjava/pom.xml (original)
+++ james/server/trunk/dnsservice-dnsjava/pom.xml Fri Jan 7 09:25:12 2011
@@ -75,6 +75,10 @@
<artifactId>james-server-lifecycle-api</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.james</groupId>
+ <artifactId>james-server-util</artifactId>
+ </dependency>
+ <dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
</dependency>
Modified:
james/server/trunk/dnsservice-dnsjava/src/main/java/org/apache/james/dnsservice/dnsjava/DNSJavaService.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/dnsservice-dnsjava/src/main/java/org/apache/james/dnsservice/dnsjava/DNSJavaService.java?rev=1056224&r1=1056223&r2=1056224&view=diff
==============================================================================
---
james/server/trunk/dnsservice-dnsjava/src/main/java/org/apache/james/dnsservice/dnsjava/DNSJavaService.java
(original)
+++
james/server/trunk/dnsservice-dnsjava/src/main/java/org/apache/james/dnsservice/dnsjava/DNSJavaService.java
Fri Jan 7 09:25:12 2011
@@ -38,6 +38,7 @@ import org.apache.james.dnsservice.api.D
import org.apache.james.dnsservice.api.TemporaryResolutionException;
import org.apache.james.lifecycle.api.Configurable;
import org.apache.james.lifecycle.api.LogEnabled;
+import org.apache.james.util.MXHostAddressIterator;
import org.apache.mailet.HostAddress;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Cache;
@@ -444,67 +445,8 @@ public class DNSJavaService implements D
* @see
org.apache.james.dnsservice.api.DNSService#getSMTPHostAddresses(String)
*/
public Iterator<HostAddress> getSMTPHostAddresses(final String domainName)
throws TemporaryResolutionException {
- return new Iterator<HostAddress>() {
- private Iterator<String> mxHosts =
findMXRecords(domainName).iterator();
- private Iterator<HostAddress> addresses = null;
-
- 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()) &&
mxHosts.hasNext()) do {
- final String nextHostname = (String)mxHosts.next();
- InetAddress[] addrs = null;
- try {
- if (singleIPPerMX) {
- addrs = new InetAddress[]
{getByName(nextHostname)};
- } else {
- addrs = getAllByName(nextHostname);
- }
- } 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(nextHostname)
- .append(".");
- logger.error(logBuffer.toString());
- }
- 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(nextHostname, "smtp://" +
ipAddresses[i++].getHostAddress());
- }
-
- public void remove() {
- throw new UnsupportedOperationException ("remove
not supported by this iterator");
- }
- };
- } while (!addresses.hasNext() && mxHosts.hasNext());
-
- return addresses != null && addresses.hasNext();
- }
-
- public HostAddress next() {
- return addresses != null ? addresses.next() : null;
- }
-
- public void remove() {
- throw new UnsupportedOperationException ("remove not supported
by this iterator");
- }
- };
+ Iterator<String> mxHosts = findMXRecords(domainName).iterator();
+ return new MXHostAddressIterator(mxHosts, this, singleIPPerMX, logger);
}
/* java.net.InetAddress.get[All]ByName(String) allows an IP literal
Modified:
james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java?rev=1056224&r1=1056223&r2=1056224&view=diff
==============================================================================
---
james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
(original)
+++
james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java
Fri Jan 7 09:25:12 2011
@@ -21,6 +21,7 @@
package org.apache.james.transport.mailets;
+import org.apache.commons.logging.Log;
import org.apache.james.dnsservice.api.DNSService;
import org.apache.james.dnsservice.api.TemporaryResolutionException;
import org.apache.james.domainlist.api.DomainList;
@@ -30,6 +31,7 @@ import org.apache.james.queue.api.MailQu
import org.apache.james.queue.api.MailQueueFactory;
import org.apache.james.queue.api.MailQueue.MailQueueException;
import org.apache.james.queue.api.MailQueue.MailQueueItem;
+import org.apache.james.util.MXHostAddressIterator;
import org.apache.james.util.TimeConverter;
import org.apache.mailet.base.GenericMailet;
import org.apache.mailet.HostAddress;
@@ -60,7 +62,6 @@ import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ConnectException;
-import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
@@ -193,6 +194,8 @@ public class RemoteDelivery extends Gene
private String heloName;
+ private final Log logAdapter = new MailetLog();
+
@Resource(name="mailqueuefactory")
@@ -1556,72 +1559,9 @@ public class RemoteDelivery extends Gene
* @return an Iterator over HostAddress instances, sorted by priority
*/
private Iterator<HostAddress> getGatewaySMTPHostAddresses(final
Collection<String> gatewayServers) {
- return new Iterator<HostAddress>() {
- private Iterator<String> gateways = gatewayServers.iterator();
- private Iterator<HostAddress> addresses = null;
-
- 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 gateway didn't have any
- * valid addresses.
- */
- if (!hasNextAddress() && gateways.hasNext()) {
- do {
- String server = (String) gateways.next();
- String port = "25";
-
- int idx = server.indexOf(':');
- if ( idx > 0) {
- port = server.substring(idx+1);
- server = server.substring(0,idx);
- }
-
- final String nextGateway = server;
- final String nextGatewayPort = port;
- try {
- final InetAddress[] ips =
dnsServer.getAllByName(nextGateway);
- addresses = new Iterator<HostAddress>() {
- private InetAddress[] ipAddresses = ips;
- int i = 0;
-
- public boolean hasNext() {
- return i < ipAddresses.length;
- }
-
- public HostAddress next() {
- return new
org.apache.mailet.HostAddress(nextGateway, "smtp://" +
(ipAddresses[i++]).getHostAddress() + ":" + nextGatewayPort);
- }
+ Iterator<String> gateways = gatewayServers.iterator();
- public void remove() {
- throw new UnsupportedOperationException
("remove not supported by this iterator");
- }
- };
- }
- catch (java.net.UnknownHostException uhe) {
- log("Unknown gateway host: " +
uhe.getMessage().trim());
- log("This could be a DNS server error or
configuration error.");
- }
- } while (!hasNextAddress() && gateways.hasNext());
- }
-
- return hasNextAddress();
- }
-
- private boolean hasNextAddress() {
- return addresses != null && addresses.hasNext();
- }
-
- public HostAddress next() {
- return (addresses != null) ? addresses.next() : null;
- }
-
- public void remove() {
- throw new UnsupportedOperationException ("remove not supported
by this iterator");
- }
- };
+ return new MXHostAddressIterator(gateways, dnsServer, false,
logAdapter);
}
protected String getHeloName() {
@@ -1638,4 +1578,91 @@ public class RemoteDelivery extends Gene
}
}
+ private final class MailetLog implements Log {
+
+ public void debug(Object arg0) {
+ if (isDebug) {
+ log(arg0.toString());
+ }
+ }
+
+ public void debug(Object arg0, Throwable arg1) {
+ if (isDebug) {
+ log(arg0.toString(), arg1);
+ }
+ }
+
+ public void error(Object arg0) {
+ log(arg0.toString());
+
+ }
+
+ public void error(Object arg0, Throwable arg1) {
+ log(arg0.toString(), arg1);
+
+ }
+
+ public void fatal(Object arg0) {
+ log(arg0.toString());
+
+ }
+
+ public void fatal(Object arg0, Throwable arg1) {
+ log(arg0.toString(), arg1);
+
+ }
+
+ public void info(Object arg0) {
+ log(arg0.toString());
+
+ }
+
+ public void info(Object arg0, Throwable arg1) {
+ log(arg0.toString(), arg1);
+
+ }
+
+ public boolean isDebugEnabled() {
+ return isDebug;
+ }
+
+ public boolean isErrorEnabled() {
+ return true;
+ }
+
+ public boolean isFatalEnabled() {
+ return true;
+ }
+
+ public boolean isInfoEnabled() {
+ return true;
+
+ }
+
+ public boolean isTraceEnabled() {
+ return false;
+ }
+
+ public boolean isWarnEnabled() {
+ return true;
+ }
+
+ public void trace(Object arg0) {
+ }
+
+ public void trace(Object arg0, Throwable arg1) {
+
+ }
+
+ public void warn(Object arg0) {
+ log(arg0.toString());
+
+ }
+
+ public void warn(Object arg0, Throwable arg1) {
+ log(arg0.toString(), arg1);
+
+ }
+
+ }
}
Added:
james/server/trunk/util/src/main/java/org/apache/james/util/MXHostAddressIterator.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/util/src/main/java/org/apache/james/util/MXHostAddressIterator.java?rev=1056224&view=auto
==============================================================================
---
james/server/trunk/util/src/main/java/org/apache/james/util/MXHostAddressIterator.java
(added)
+++
james/server/trunk/util/src/main/java/org/apache/james/util/MXHostAddressIterator.java
Fri Jan 7 09:25:12 2011
@@ -0,0 +1,130 @@
+/****************************************************************
+ * 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.util;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Iterator;
+
+import org.apache.commons.logging.Log;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.mailet.HostAddress;
+
+/**
+ *
+ *
+ */
+public class MXHostAddressIterator implements Iterator<HostAddress>{
+
+
+ private Iterator<HostAddress> addresses = null;
+ private Iterator<String> hosts;
+ private DNSService dns;
+ private boolean useSingleIP;
+ private Log logger;
+
+ public MXHostAddressIterator(Iterator<String> hosts, DNSService dns,
boolean useSingleIP, Log logger) {
+ this.hosts = hosts;
+ this.dns = dns;
+ this.useSingleIP = useSingleIP;
+ this.logger = logger;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @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 = "25";
+ }
+
+ 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());
+ }
+ 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();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.util.Iterator#next()
+ */
+ public HostAddress next() {
+ return addresses != null ? addresses.next() : null;
+ }
+
+ /**
+ * Not supported.
+ */
+ public void remove() {
+ throw new UnsupportedOperationException ("remove not supported by this
iterator");
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]