class AmbariLdapDataPopulator {

  protected LdapTemplate loadLdapTemplate() {
    final LdapServerProperties properties = getConfiguration().getLdapServerProperties();
    if (ldapTemplate == null || !properties.equals(getLdapProperties())) {
      LOG.info("Reloading properties");
      ldapServerProperties = properties;

      final LdapContextSource ldapContextSource = createLdapContextSource();

      // The LdapTemplate by design will close the connection after each call to the LDAP Server
      // In order to have the interaction work with large/paged results, said connection must be pooled and reused
      ldapContextSource.setPooled(true);

      final List<String> ldapUrls = ldapServerProperties.getLdapUrls();
      ldapContextSource.setUrls(ldapUrls.toArray(new String[ldapUrls.size()]));

      if (!ldapServerProperties.isAnonymousBind()) {
        ldapContextSource.setUserDn(ldapServerProperties.getManagerDn());
        ldapContextSource.setPassword(ldapServerProperties.getManagerPassword());
      }

      if (ldapServerProperties.isUseSsl() && ldapServerProperties.isDisableEndpointIdentification()) {
        System.setProperty(SYSTEM_PROPERTY_DISABLE_ENDPOINT_IDENTIFICATION, "true");
        LOG.info("Disabled endpoint identification");
      } else {
        System.clearProperty(SYSTEM_PROPERTY_DISABLE_ENDPOINT_IDENTIFICATION);
        LOG.info("Removed endpoint identification disabling");
      }

      ldapContextSource.setReferral(ldapServerProperties.getReferralMethod());

      try {
        ldapContextSource.afterPropertiesSet();
      } catch (Exception e) {
        LOG.error("LDAP Context Source not loaded ", e);
        throw new UsernameNotFoundException("LDAP Context Source not loaded", e);
      }

      ldapTemplate = createLdapTemplate(ldapContextSource);

      ldapTemplate.setIgnorePartialResultException(true);
    }
    return ldapTemplate;
  }

  @Inject public  AmbariLdapDataPopulator(Provider<AmbariLdapConfiguration> configurationProvider, Users users);

}