package org.ovirt.engine.extensions.aaa.builtin.kerberosldap;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.naming.spi.DirectoryManager;
import javax.security.auth.login.Configuration;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.api.extensions.Base;
import org.ovirt.engine.core.ldap.LdapSRVLocator;
import org.ovirt.engine.core.utils.dns.DnsSRVLocator.DnsSRVResult;

public class Utils {

    private static boolean firstCallToInit = true;

    public static void setDefaults(Properties conf, String domain) {
        {
            Properties defaults = new Properties();
            try (Reader reader = new InputStreamReader(Utils.class.getResourceAsStream("defaults.properties"), Charset.forName("UTF-8"))) {
                defaults.load(reader);
                for (Map.Entry<Object, Object> entry : defaults.entrySet()) {
                    putIfAbsent(conf, (String) entry.getKey(), (String) entry.getValue());
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (StringUtils.isBlank(conf.getProperty("config.LdapServers"))) {
            try {

                LdapSRVLocator locator = new LdapSRVLocator();
                DnsSRVResult results = locator.getLdapServers(domain);
                if (results == null || results.getNumOfValidAddresses() == 0) {
                    throw new Exception(String.format("No ldap servers  were found for domain %1$s", domain));
                } else {
                    List<String> ldapServers = new ArrayList<>();
                    for (int counter = 0; counter < results.getNumOfValidAddresses(); counter++) {
                        String address = results.getAddresses()[counter];
                        String ldapURI = locator.constructURI("ldap", address, "389").toString();
                        ldapServers.add(ldapURI);
                    }
                    conf.setProperty("config.LdapServers", StringUtils.join(ldapServers, ";"));
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    private static void putIfAbsent(Properties props, String key, String value) {
        if (!props.containsKey(key)) {
            props.put(key, value);
        }
    }

    public static synchronized void handleApplicationInit(String applicationName) throws Exception {
        if (firstCallToInit) {
            if (applicationName.equals(Base.ApplicationNames.OVIRT_ENGINE_AAA_EXTENSION_TOOL)) {
                Configuration.getConfiguration().refresh();
                DirectoryManager.setObjectFactoryBuilder(new DirectoryContextFactoryBuilder());
            }
            firstCallToInit = false;
        }
        System.setProperty("sun.security.krb5.msinterop.kstring", "true");
    }


}

@@ -276,17 +276,16 @@
 erties;%0A
-%0A
 import j
@@ -1259,164 +1259,358 @@
   %7D%0A
-        if (StringUtils.isBlank(conf.getProperty(%22config.LdapServers%22))) %7B%0A            try %7B%0A%0A                LdapSRVLocator locator = new LdapSRVLocator();
+%0A        List%3CString%3E ldapServers = new ArrayList%3C%3E();%0A        LdapSRVLocator locator = new LdapSRVLocator();%0A        try %7B%0A            if (StringUtils.isBlank(conf.getProperty(%22config.LdapServers%22))) %7B%0A                // list of LDAP servers is empty, find LDAP servers using DNS SRV records and convert them%0A                // into the list of URIs
 %0A   
@@ -1897,74 +1897,8 @@
 e %7B%0A
-                    List%3CString%3E ldapServers = new ArrayList%3C%3E();%0A
     
@@ -2256,18 +2256,148 @@
     
+%7D%0A
    
+         %7D else %7B%0A                // list of LDAP servers was entered, convert them to URIs%0A                for (String server :
  conf.
-s
+g
 etPr
@@ -2427,43 +2427,113 @@
 ers%22
-, StringUtils.join(ldapS
+).split(%22;%22)) %7B%0A                    ldapServers.add(locator.constructURI(%22ldap%22, s
 erver
-s
 , %22
-;%22
+389%22).toString(
 ));%0A
@@ -2555,32 +2555,42 @@
  %7D%0A            %7D
+%0A        %7D
  catch (Exceptio
@@ -2601,36 +2601,32 @@
 ) %7B%0A            
-   
- 
 throw new Runtim
@@ -2641,36 +2641,32 @@
 on(ex);%0A        
-    
 %7D%0A        %7D%0A    
@@ -2651,33 +2651,107 @@
       %7D%0A        
-%7D
+conf.setProperty(%22config.LdapServers%22, StringUtils.join(ldapServers, %22;%22));
 %0A    %7D%0A%0A    priv

Convert list of ldap server names to URIs