use of org.forgerock.opendj.ldap.ConnectionFactory in project OpenAM by OpenRock.
the class SMDataLayer method initLdapPool.
/**
* Initialize the pool shared by all SMDataLayer object(s).
*/
private synchronized void initLdapPool() {
// Dont' do anything if pool is already initialized
if (_ldapPool != null)
return;
// Initialize the pool with minimum and maximum connections settings
// retrieved from configuration
ServerInstance svrCfg;
try {
DSConfigMgr dsCfg = DSConfigMgr.getDSConfigMgr();
// Get "sms" ServerGroup if present
ServerGroup sg = dsCfg.getServerGroup("sms");
final ConnectionFactory baseFactory;
if (sg != null) {
baseFactory = dsCfg.getNewConnectionFactory("sms", LDAPUser.Type.AUTH_ADMIN);
svrCfg = sg.getServerInstance(LDAPUser.Type.AUTH_ADMIN);
} else {
baseFactory = dsCfg.getNewAdminConnectionFactory();
svrCfg = dsCfg.getServerInstance(LDAPUser.Type.AUTH_ADMIN);
}
if (svrCfg == null) {
debug.error("SMDataLayer:initLdapPool()-" + "Error getting server config.");
}
int poolMin = 1;
int poolMax = 2;
// Initialize the Connection Pool size only for the server
if (SystemProperties.isServerMode()) {
poolMin = svrCfg.getMinConnections();
poolMax = svrCfg.getMaxConnections();
}
debug.message("SMDataLayer:initLdapPool(): Creating ldap connection pool with: poolMin {} poolMax {}", poolMin, poolMax);
int idleTimeout = SystemProperties.getAsInt(LDAP_CONN_IDLE_TIME_IN_SECS, 0);
if (idleTimeout == 0 && StringUtils.isNotBlank(SystemProperties.get(LDAP_CONN_IDLE_TIME_IN_SECS))) {
debug.error("SMDataLayer: Idle timeout could not be parsed, connection reaping is disabled");
} else if (idleTimeout == 0) {
debug.message("SMDataLayer: Idle timeout is set to 0 - connection reaping is disabled");
}
_ldapPool = Connections.newCachedConnectionPool(baseFactory, poolMin, poolMax, idleTimeout, TimeUnit.SECONDS);
ShutdownManager shutdownMan = com.sun.identity.common.ShutdownManager.getInstance();
shutdownMan.addShutdownListener(new ShutdownListener() {
public void shutdown() {
if (_ldapPool != null) {
_ldapPool.close();
}
}
});
} catch (LDAPServiceException ex) {
debug.error("SMDataLayer:initLdapPool()-" + "Error initializing connection pool " + ex.getMessage());
ex.printStackTrace();
}
}
use of org.forgerock.opendj.ldap.ConnectionFactory in project OpenAM by OpenRock.
the class LDAPConnectionPools method initConnectionPool.
/**
* Create a Ldap Connection Pool for a ldap server
* @param host the name of the LDAP server host and its port number.
* For example, dsame.sun.com:389
* Alternatively, this can be a space-delimited list of
* host names.
* @param ssl if the connection is in ssl
* @param minPoolSize minimal pool size
* @param maxPoolSize maximum pool size
*/
static void initConnectionPool(String host, String authDN, String authPasswd, boolean ssl, int minPoolSize, int maxPoolSize, Options options) throws PolicyException {
if (host.length() < 1) {
debug.message("Invalid host name");
throw new PolicyException(ResBundleUtils.rbName, "invalid_ldap_server_host", null, null);
}
try {
synchronized (connectionPools) {
if (connectionPools.get(host) == null) {
if (debug.messageEnabled()) {
debug.message("Create LDAPConnectionPool: " + host);
}
if (ssl) {
options.set(LDAPConnectionFactory.SSL_CONTEXT, new SSLContextBuilder().getSSLContext());
}
ConnectionFactory ldc = LDAPUtils.createFailoverConnectionFactory(host, DEFAULT_PORT, authDN, authPasswd, options);
if (minPoolSize < 1) {
minPoolSize = MIN_CONNECTION_POOL_SIZE;
}
if (maxPoolSize < 1) {
maxPoolSize = MAX_CONNECTION_POOL_SIZE;
}
debug.message("LDAPConnectionPools.initConnectionPool(): minPoolSize={}, maxPoolSize={}", minPoolSize, maxPoolSize);
ShutdownManager shutdownMan = com.sun.identity.common.ShutdownManager.getInstance();
int idleTimeout = SystemProperties.getAsInt(Constants.LDAP_CONN_IDLE_TIME_IN_SECS, 0);
if (idleTimeout == 0) {
debug.error("LDAPConnectionPools: Idle timeout could not be parsed, connection reaping is disabled");
}
final ConnectionFactory cPool = Connections.newCachedConnectionPool(ldc, minPoolSize, maxPoolSize, idleTimeout, TimeUnit.SECONDS);
debug.message("LDAPConnectionPools.initConnectionPool(): host: {}", host);
shutdownMan.addShutdownListener(new ShutdownListener() {
public void shutdown() {
cPool.close();
}
});
connectionPools.put(host, cPool);
}
}
} catch (Exception e) {
debug.message("Unable to create LDAPConnectionPool", e);
throw new PolicyException(e.getMessage(), e);
}
}
use of org.forgerock.opendj.ldap.ConnectionFactory in project OpenAM by OpenRock.
the class IdRepoUtils method getADAMInstanceGUID.
private static String getADAMInstanceGUID(Map attrValues) throws Exception {
try (ConnectionFactory factory = getLDAPConnection(attrValues);
Connection ld = factory.getConnection()) {
String attrName = "schemaNamingContext";
String[] attrs = { attrName };
ConnectionEntryReader res = ld.search(LDAPRequests.newSearchRequest("", SearchScope.BASE_OBJECT, "(objectclass=*)"));
if (res.hasNext()) {
SearchResultEntry entry = res.readEntry();
Attribute ldapAttr = entry.getAttribute(attrName);
if (ldapAttr != null) {
String value = ldapAttr.firstValueAsString();
int index = value.lastIndexOf("=");
if (index != -1) {
return value.substring(index + 1).trim();
}
}
}
}
return null;
}
use of org.forgerock.opendj.ldap.ConnectionFactory in project OpenAM by OpenRock.
the class AjaxPage method getConnection.
protected Connection getConnection(String host, int port, String bindDN, char[] bindPwd, int timeout, boolean isSSl) throws GeneralSecurityException, LdapException {
Options ldapOptions = Options.defaultOptions().set(CONNECT_TIMEOUT, new Duration((long) timeout, TimeUnit.SECONDS)).set(AUTHN_BIND_REQUEST, LDAPRequests.newSimpleBindRequest(bindDN, bindPwd));
if (isSSl) {
ldapOptions = ldapOptions.set(SSL_CONTEXT, new SSLContextBuilder().getSSLContext());
}
ConnectionFactory factory = new LDAPConnectionFactory(host, port, ldapOptions);
return factory.getConnection();
}
use of org.forgerock.opendj.ldap.ConnectionFactory in project OpenAM by OpenRock.
the class SMSRepositoryMig method main.
public static void main(String[] args) throws Exception {
String host, binddn, pw, basedn, flatfiledir;
int port;
if (args.length < 6) {
usage();
System.exit(0);
}
host = args[0];
port = Integer.parseInt(args[1]);
binddn = args[2];
pw = args[3];
basedn = args[4];
flatfiledir = args[5];
try (ConnectionFactory factory = getConnectionFactory(host, port, basedn, pw.toCharArray())) {
// do the migration
migrate(factory, host, port, binddn, pw, basedn, flatfiledir);
}
}
Aggregations