Skip to content

Commit

Permalink
JAMES-2251 Avoid creating mutableDomains in AbstractDomainList.getDom…
Browse files Browse the repository at this point in the history
…ains()
  • Loading branch information
dtrebbien authored and aduprat committed Dec 12, 2017
1 parent f4b6276 commit 9c871fe
Showing 1 changed file with 18 additions and 17 deletions.
Expand Up @@ -21,7 +21,6 @@


import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
Expand All @@ -40,6 +39,7 @@
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;


/** /**
* All implementations of the DomainList interface should extends this abstract * All implementations of the DomainList interface should extends this abstract
Expand Down Expand Up @@ -151,32 +151,33 @@ public boolean containsDomain(String domain) throws DomainListException {
} }


@Override @Override
public List<String> getDomains() throws DomainListException { public ImmutableList<String> getDomains() throws DomainListException {
List<String> domains = getDomainListInternal(); List<String> domains = getDomainListInternal();

ImmutableList<String> detectedDomains = detectDomains();
// create mutable copy, some subclasses return ImmutableList // Guava does not support concatenating ImmutableLists at this time:
ArrayList<String> mutableDomains = new ArrayList<>(domains); // https://stackoverflow.com/questions/37919648/concatenating-immutablelists
List<String> detectedDomains = detectDomains(); // A work-around is to use Iterables.concat() until something like
mutableDomains.addAll(detectedDomains); // https://github.com/google/guava/issues/1029 is implemented.
mutableDomains.addAll(detectIps(mutableDomains)); ImmutableList<String> detectedIps = detectIps(Iterables.concat(domains, detectedDomains));
Iterable<String> allDomains = Iterables.concat(domains, detectedDomains, detectedIps);


if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
for (String domain : mutableDomains) { for (String domain : allDomains) {
LOGGER.debug("Handling mail for: " + domain); LOGGER.debug("Handling mail for: " + domain);
} }
} }


return ImmutableList.copyOf(mutableDomains); return ImmutableList.copyOf(allDomains);
} }


private List<String> detectIps(ArrayList<String> mutableDomains) { private ImmutableList<String> detectIps(Iterable<String> domains) {
if (autoDetectIP) { if (autoDetectIP) {
return getDomainsIP(mutableDomains, dns, LOGGER); return getDomainsIP(domains, dns, LOGGER);
} }
return ImmutableList.of(); return ImmutableList.of();
} }


private List<String> detectDomains() { private ImmutableList<String> detectDomains() {
if (autoDetect) { if (autoDetect) {
String hostName; String hostName;
try { try {
Expand All @@ -197,11 +198,11 @@ private List<String> detectDomains() {
* Return a List which holds all ipAddress of the domains in the given List * Return a List which holds all ipAddress of the domains in the given List
* *
* @param domains * @param domains
* List of domains * Iterable of domains
* @return domainIP List of ipaddress for domains * @return domainIP List of ipaddress for domains
*/ */
private static List<String> getDomainsIP(List<String> domains, DNSService dns, Logger log) { private static ImmutableList<String> getDomainsIP(Iterable<String> domains, DNSService dns, Logger log) {
return domains.stream() return Guavate.stream(domains)
.flatMap(domain -> getDomainIP(domain, dns, log).stream()) .flatMap(domain -> getDomainIP(domain, dns, log).stream())
.distinct() .distinct()
.collect(Guavate.toImmutableList()); .collect(Guavate.toImmutableList());
Expand All @@ -210,7 +211,7 @@ private static List<String> getDomainsIP(List<String> domains, DNSService dns, L
/** /**
* @see #getDomainsIP(List, DNSService, Logger) * @see #getDomainsIP(List, DNSService, Logger)
*/ */
private static List<String> getDomainIP(String domain, DNSService dns, Logger log) { private static ImmutableList<String> getDomainIP(String domain, DNSService dns, Logger log) {
try { try {
return dns.getAllByName(domain).stream() return dns.getAllByName(domain).stream()
.map(InetAddress::getHostAddress) .map(InetAddress::getHostAddress)
Expand Down

0 comments on commit 9c871fe

Please sign in to comment.