Search in sources :

Example 6 with ConnectionFactory

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();
    }
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) ServerGroup(com.iplanet.services.ldap.ServerGroup) DSConfigMgr(com.iplanet.services.ldap.DSConfigMgr) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) ServerInstance(com.iplanet.services.ldap.ServerInstance)

Example 7 with ConnectionFactory

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);
    }
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) PolicyException(com.sun.identity.policy.PolicyException) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager) SSLContextBuilder(org.forgerock.opendj.ldap.SSLContextBuilder) PolicyException(com.sun.identity.policy.PolicyException)

Example 8 with ConnectionFactory

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;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) Attribute(org.forgerock.opendj.ldap.Attribute) Connection(org.forgerock.opendj.ldap.Connection) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 9 with ConnectionFactory

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();
}
Also used : Options(org.forgerock.util.Options) ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) Duration(org.forgerock.util.time.Duration) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) SSLContextBuilder(org.forgerock.opendj.ldap.SSLContextBuilder)

Example 10 with ConnectionFactory

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);
    }
}
Also used : ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) ByteString(org.forgerock.opendj.ldap.ByteString)

Aggregations

ConnectionFactory (org.forgerock.opendj.ldap.ConnectionFactory)14 LDAPConnectionFactory (org.forgerock.opendj.ldap.LDAPConnectionFactory)12 Connection (org.forgerock.opendj.ldap.Connection)6 SSLContextBuilder (org.forgerock.opendj.ldap.SSLContextBuilder)5 Options (org.forgerock.util.Options)5 DSConfigMgr (com.iplanet.services.ldap.DSConfigMgr)4 LDAPServiceException (com.iplanet.services.ldap.LDAPServiceException)4 ByteString (org.forgerock.opendj.ldap.ByteString)4 ArrayList (java.util.ArrayList)3 LdapException (org.forgerock.opendj.ldap.LdapException)3 ShutdownListener (org.forgerock.util.thread.listener.ShutdownListener)3 Duration (org.forgerock.util.time.Duration)3 ServerGroup (com.iplanet.services.ldap.ServerGroup)2 ServerInstance (com.iplanet.services.ldap.ServerInstance)2 SMSException (com.sun.identity.sm.SMSException)2 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 StringTokenizer (java.util.StringTokenizer)2 ShutdownManager (org.forgerock.util.thread.listener.ShutdownManager)2 SystemProperties (com.iplanet.am.util.SystemProperties)1