if (ldapServer == null) {
throw new LdapDAOException("Ldap connection is not configured.");
}
CConnectionInfo connInfo = ldapServer.getConnectionInfo();
String url;
try {
if (useBackupUrl) {
url = new LdapURL(connInfo.getBackupMirrorProtocol(), connInfo.getBackupMirrorHost(), connInfo
.getBackupMirrorPort(), connInfo.getSearchBase()).toString();
}
else {
url = new LdapURL(connInfo.getProtocol(), connInfo.getHost(), connInfo.getPort(), connInfo
.getSearchBase()).toString();
}
}
catch (MalformedURLException e) {
// log an error, because the user could still log in and fix the config.
logger.error("LDAP Configuration is Invalid.");
throw new LdapDAOException("Invalid LDAP URL: " + e.getMessage());
}
defaultLdapContextFactory.setUsePooling(true);
defaultLdapContextFactory.setUrl(url);
defaultLdapContextFactory.setSystemUsername(connInfo.getSystemUsername());
defaultLdapContextFactory.setSystemPassword(connInfo.getSystemPassword());
defaultLdapContextFactory.setSearchBase(connInfo.getSearchBase());
defaultLdapContextFactory.setAuthentication(connInfo.getAuthScheme());
// get the timeout
Map<String, String> connectionProperties = new HashMap<String, String>();
connectionProperties.put("com.sun.jndi.ldap.connect.timeout",
Integer.toString(ldapServer.getConnectionInfo().getConnectionTimeout() * 1000));
// and the realm
if (connInfo.getRealm() != null) {
connectionProperties.put("java.naming.security.sasl.realm", connInfo.getRealm());
}
defaultLdapContextFactory.setAdditionalEnvironment(connectionProperties);
return defaultLdapContextFactory;
}