Search in sources :

Example 1 with LDAPURL

use of com.unboundid.ldap.sdk.LDAPURL in project zm-mailbox by Zimbra.

the class LdapServerPool method createServerSet.

private ServerSet createServerSet(SocketFactory socketFactory) throws UnknownHostException {
    if (urls.size() == 1) {
        LDAPURL url = urls.get(0);
        if (LdapConnType.isLDAPI(url.getScheme())) {
            //dummy ldap host is used for LDAPI
            return new SingleServerSet(url.getHost(), url.getPort(), socketFactory, connOpts);
        }
        InetAddress[] addrs = InetAddress.getAllByName(url.getHost());
        if (addrs.length == 1) {
            if (socketFactory == null) {
                return new SingleServerSet(url.getHost(), url.getPort(), connOpts);
            } else {
                return new SingleServerSet(url.getHost(), url.getPort(), socketFactory, connOpts);
            }
        } else {
            Set<String> uniqAddr = new HashSet<>();
            for (int i = 0; i < addrs.length; i++) {
                uniqAddr.add(addrs[i].getHostAddress());
            }
            if (uniqAddr.size() == 1) {
                if (socketFactory == null) {
                    return new SingleServerSet(url.getHost(), url.getPort(), connOpts);
                } else {
                    return new SingleServerSet(url.getHost(), url.getPort(), socketFactory, connOpts);
                }
            } else {
                String[] hosts = new String[uniqAddr.size()];
                int[] ports = new int[uniqAddr.size()];
                int i = 0;
                for (String addr : uniqAddr) {
                    hosts[i] = addr;
                    ports[i] = url.getPort();
                    i++;
                }
                if (socketFactory == null) {
                    return new FailoverServerSet(hosts, ports, connOpts);
                } else {
                    return new FailoverServerSet(hosts, ports, socketFactory, connOpts);
                }
            }
        }
    } else {
        Set<Pair<String, Integer>> hostsAndPorts = new LinkedHashSet<>();
        for (LDAPURL url : urls) {
            InetAddress[] addrs = InetAddress.getAllByName(url.getHost());
            if (addrs.length == 1) {
                hostsAndPorts.add(new Pair<String, Integer>(url.getHost(), url.getPort()));
            } else {
                for (int i = 0; i < addrs.length; i++) {
                    hostsAndPorts.add(new Pair<String, Integer>(addrs[i].getHostAddress(), url.getPort()));
                }
            }
        }
        String[] hostsStrs = new String[hostsAndPorts.size()];
        int[] portsStrs = new int[hostsAndPorts.size()];
        int i = 0;
        for (Pair<String, Integer> pair : hostsAndPorts) {
            hostsStrs[i] = pair.getFirst();
            portsStrs[i] = pair.getSecond();
            i++;
        }
        if (socketFactory == null) {
            FailoverServerSet serverSet = new FailoverServerSet(hostsStrs, portsStrs, connOpts);
            serverSet.setReOrderOnFailover(true);
            return serverSet;
        } else {
            FailoverServerSet serverSet = new FailoverServerSet(hostsStrs, portsStrs, socketFactory, connOpts);
            serverSet.setReOrderOnFailover(true);
            return serverSet;
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LDAPURL(com.unboundid.ldap.sdk.LDAPURL) SingleServerSet(com.unboundid.ldap.sdk.SingleServerSet) FailoverServerSet(com.unboundid.ldap.sdk.FailoverServerSet) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Pair(com.zimbra.common.util.Pair)

Aggregations

FailoverServerSet (com.unboundid.ldap.sdk.FailoverServerSet)1 LDAPURL (com.unboundid.ldap.sdk.LDAPURL)1 SingleServerSet (com.unboundid.ldap.sdk.SingleServerSet)1 Pair (com.zimbra.common.util.Pair)1 InetAddress (java.net.InetAddress)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1