Search in sources :

Example 1 with ShutdownManager

use of org.forgerock.util.thread.listener.ShutdownManager 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 2 with ShutdownManager

use of org.forgerock.util.thread.listener.ShutdownManager in project OpenAM by OpenRock.

the class AMSetupDSConfig method getLDAPConnection.

/**
     * Helper method to return Ldap connection 
     *
     * @param ssl <code>true</code> if directory server is running SSL.
     * @return Ldap connection 
     */
private synchronized Connection getLDAPConnection(boolean ssl) {
    try {
        if (ld == null) {
            ShutdownManager shutdownMan = com.sun.identity.common.ShutdownManager.getInstance();
            // All connections will use authentication
            SimpleBindRequest request = LDAPRequests.newSimpleBindRequest(dsManager, dsAdminPwd.toCharArray());
            Options options = Options.defaultOptions().set(REQUEST_TIMEOUT, new Duration((long) 3, TimeUnit.SECONDS)).set(AUTHN_BIND_REQUEST, request);
            if (ssl) {
                options = options.set(SSL_CONTEXT, new SSLContextBuilder().getSSLContext());
            }
            ld = new LDAPConnectionFactory(dsHostName, getPort(), options);
            shutdownMan.addShutdownListener(new ShutdownListener() {

                public void shutdown() {
                    disconnectDServer();
                }
            });
        }
        return ld.getConnection();
    } catch (LdapException e) {
        disconnectDServer();
        dsConfigInstance = null;
        ld = null;
    } catch (Exception e) {
        dsConfigInstance = null;
        ld = null;
    }
    return null;
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) Options(org.forgerock.util.Options) SimpleBindRequest(org.forgerock.opendj.ldap.requests.SimpleBindRequest) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager) Duration(org.forgerock.util.time.Duration) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) SSLContextBuilder(org.forgerock.opendj.ldap.SSLContextBuilder) LdapException(org.forgerock.opendj.ldap.LdapException) LdapException(org.forgerock.opendj.ldap.LdapException) IOException(java.io.IOException)

Example 3 with ShutdownManager

use of org.forgerock.util.thread.listener.ShutdownManager in project OpenAM by OpenRock.

the class ShutdownMonitorTest method setup.

@BeforeTest
public void setup() {
    ShutdownManager shutdownManagerWrapper = mock(ShutdownManager.class);
    ArgumentCaptor<ShutdownListener> captor = ArgumentCaptor.forClass(ShutdownListener.class);
    monitor = new ShutdownMonitor(shutdownManagerWrapper);
    verify(shutdownManagerWrapper).addShutdownListener(captor.capture());
    listener = captor.getValue();
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with ShutdownManager

use of org.forgerock.util.thread.listener.ShutdownManager in project OpenAM by OpenRock.

the class DataLayer method initLdapPool.

/**
     * Initialize the pool shared by all DataLayer object(s).
     */
private synchronized void initLdapPool() throws UMSException {
    // Don't 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 = null;
    String hostName = null;
    try {
        DSConfigMgr dsCfg = DSConfigMgr.getDSConfigMgr();
        hostName = dsCfg.getHostName("default");
        baseFactory = dsCfg.getNewProxyConnectionFactory();
        svrCfg = dsCfg.getServerInstance(LDAPUser.Type.AUTH_PROXY);
    } catch (LDAPServiceException ex) {
        debug.error("Error initializing connection pool " + ex.getMessage());
    }
    // Check if svrCfg was successfully obtained
    if (svrCfg == null) {
        debug.error("Error getting server config.");
        String[] args = new String[1];
        args[0] = hostName == null ? "default" : hostName;
        throw new UMSException(i18n.getString(IUMSConstants.NEW_INSTANCE_FAILED, args));
    }
    int poolMin = svrCfg.getMinConnections();
    int poolMax = svrCfg.getMaxConnections();
    m_releaseConnectionBeforeSearchCompletes = svrCfg.getBooleanValue(LDAP_RELEASECONNBEFORESEARCH, false);
    if (debug.messageEnabled()) {
        debug.message("Creating ldap connection pool with: poolMin {}, poolMax {}", poolMin, poolMax);
    }
    int idleTimeout = SystemProperties.getAsInt(Constants.LDAP_CONN_IDLE_TIME_IN_SECS, 0);
    if (idleTimeout == 0) {
        debug.warning("Idle timeout not set. Defaulting to 0.");
    }
    _ldapPool = Connections.newCachedConnectionPool(Connections.newNamedConnectionFactory(baseFactory, "DataLayer"), 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();
            }
        }
    });
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) DSConfigMgr(com.iplanet.services.ldap.DSConfigMgr) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) ByteString(org.forgerock.opendj.ldap.ByteString) ServerInstance(com.iplanet.services.ldap.ServerInstance)

Example 5 with ShutdownManager

use of org.forgerock.util.thread.listener.ShutdownManager in project OpenAM by OpenRock.

the class IdServicesImpl method getInstance.

protected static synchronized IdServices getInstance() {
    if (_instance == null) {
        DEBUG.message("IdServicesImpl.getInstance(): " + "Creating new Instance of IdServicesImpl()");
        ShutdownManager shutdownMan = com.sun.identity.common.ShutdownManager.getInstance();
        _instance = new IdServicesImpl();
        shutdownMan.addShutdownListener(new ShutdownListener() {

            public void shutdown() {
                synchronized (_instance) {
                    shutdownCalled = true;
                }
                _instance.clearIdRepoPlugins();
            }
        });
    }
    return _instance;
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager)

Aggregations

ShutdownManager (org.forgerock.util.thread.listener.ShutdownManager)11 ShutdownListener (org.forgerock.util.thread.listener.ShutdownListener)10 ThreadPool (com.iplanet.am.util.ThreadPool)2 DSConfigMgr (com.iplanet.services.ldap.DSConfigMgr)2 LDAPServiceException (com.iplanet.services.ldap.LDAPServiceException)2 ServerInstance (com.iplanet.services.ldap.ServerInstance)2 IOException (java.io.IOException)2 ConnectionFactory (org.forgerock.opendj.ldap.ConnectionFactory)2 LDAPConnectionFactory (org.forgerock.opendj.ldap.LDAPConnectionFactory)2 SSLContextBuilder (org.forgerock.opendj.ldap.SSLContextBuilder)2 ThreadPoolException (com.iplanet.am.util.ThreadPoolException)1 ServerGroup (com.iplanet.services.ldap.ServerGroup)1 XMLException (com.iplanet.services.util.XMLException)1 SSOException (com.iplanet.sso.SSOException)1 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)1 IdRepoException (com.sun.identity.idm.IdRepoException)1 PolicyException (com.sun.identity.policy.PolicyException)1 ConfiguratorException (com.sun.identity.setup.ConfiguratorException)1 FileNotFoundException (java.io.FileNotFoundException)1 LoginException (javax.security.auth.login.LoginException)1