Source:
public LdapContext getContextWithCredentials(String userDN, String password) throws UserStoreException, NamingException, AuthenticationException { LdapContext context = null; Hashtable<String, String> tempEnv = new Hashtable<String, String>(); for (Object key : environment.keySet()) { tempEnv.put((String) key, (String) environment.get(key)); } tempEnv.put(Context.SECURITY_PRINCIPAL, userDN); tempEnv.put(Context.SECURITY_CREDENTIALS, password); if (dcMap == null) { context = new InitialLdapContext(tempEnv, null); } else if (dcMap != null && dcMap.size() != 0) { try { Integer firstKey = dcMap.firstKey(); SRVRecord firstRecord = dcMap.get(firstKey); tempEnv.put(Context.PROVIDER_URL, getLDAPURLFromSRVRecord(firstRecord)); context = new InitialLdapContext(tempEnv, null); } catch (AuthenticationException e) { throw e; } catch (NamingException e) { log.error("Error obtaining connection to first Domain Controller. Trying to connect with other Domain " + "Controllers...", e); for (Integer integer : dcMap.keySet()) { try { SRVRecord srv = dcMap.get(integer); environment.put(Context.PROVIDER_URL, getLDAPURLFromSRVRecord(srv)); context = new InitialLdapContext(environment, null); break; } catch (AuthenticationException e2) { throw e2; } catch (NamingException e1) { if (integer == (dcMap.lastKey())) { throw new UserStoreException("Error obtaining connection for all " + integer + " Domain " + "Controllers.", e1); } } } } } return (context); }

Target:
public LdapContext getContextWithCredentials(String userDN, String password) throws UserStoreException, NamingException { LdapContext context = null; Hashtable<String, String> tempEnv = new Hashtable<String, String>(); for (Object key : environment.keySet()) { tempEnv.put((String) key, (String) environment.get(key)); } tempEnv.put(Context.SECURITY_PRINCIPAL, userDN); tempEnv.put(Context.SECURITY_CREDENTIALS, password); if (dcMap == null) { context = new InitialLdapContext(tempEnv, null); } else if (dcMap.isEmpty()) { try { Integer firstKey = dcMap.firstKey(); SRVRecord firstRecord = dcMap.get(firstKey); tempEnv.put(Context.PROVIDER_URL, getLDAPURLFromSRVRecord(firstRecord)); context = new InitialLdapContext(tempEnv, null); } catch (AuthenticationException e) { if (log.isDebugEnabled()) { log.debug("Error occurred while authenticating for user DN : " + userDN + " with first domain controller"); } throw e; } catch (NamingException e) { log.error("Error obtaining connection to first Domain Controller. Trying to connect with other Domain " + "Controllers...", e); for (Integer integer : dcMap.keySet()) { try { SRVRecord srv = dcMap.get(integer); environment.put(Context.PROVIDER_URL, getLDAPURLFromSRVRecord(srv)); context = new InitialLdapContext(environment, null); break; } catch (AuthenticationException e2) { if (log.isDebugEnabled()) { log.debug("Error occurred while authenticating for user DN : " + userDN + " with other domain controllers"); } throw e2; } catch (NamingException e1) { log.info("Trying to connect with other Domain Controllers"); if (integer.equals(dcMap.lastKey())) { String errorMessage = "Error obtaining connection for all " + integer + " Domain " + "Controllers."; if (log.isDebugEnabled()) { log.debug(errorMessage, e); } throw new UserStoreException(errorMessage, e1); } } } } } return (context); }