Search in sources :

Example 6 with FailoverServerSet

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

the class LdapServerPoolTest method testLDAPCreatePool.

@Test
public void testLDAPCreatePool() throws Exception {
    LdapServerPool pool = new LdapServerPool(new MockLdapServerConfig(LdapServerType.MASTER));
    FailoverServerSet set = (FailoverServerSet) pool.serverSet;
    Assert.assertTrue(set.reOrderOnFailover());
}
Also used : FailoverServerSet(com.unboundid.ldap.sdk.FailoverServerSet) Test(org.junit.Test)

Example 7 with FailoverServerSet

use of com.unboundid.ldap.sdk.FailoverServerSet 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)7 BindRequest (com.unboundid.ldap.sdk.BindRequest)3 LDAPConnectionOptions (com.unboundid.ldap.sdk.LDAPConnectionOptions)3 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)3 SSLUtil (com.unboundid.util.ssl.SSLUtil)3 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 TrustAllTrustManager (com.unboundid.util.ssl.TrustAllTrustManager)2 GeneralSecurityException (java.security.GeneralSecurityException)2 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)1 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1 LDAPURL (com.unboundid.ldap.sdk.LDAPURL)1 SingleServerSet (com.unboundid.ldap.sdk.SingleServerSet)1 StartTLSExtendedRequest (com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest)1 StaticUtils.toUTF8String (com.unboundid.util.StaticUtils.toUTF8String)1 TrustStoreTrustManager (com.unboundid.util.ssl.TrustStoreTrustManager)1 Pair (com.zimbra.common.util.Pair)1 InetAddress (java.net.InetAddress)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1