Search in sources :

Example 36 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project karaf by apache.

the class LdapPoolingTest method testSSLConnectionPool.

/**
     * @see <a href="http://docs.oracle.com/javase/jndi/tutorial/ldap/connect/config.html">LDAP connection pool</a>
     * @throws Exception
     */
@Test
public void testSSLConnectionPool() throws Exception {
    System.setProperty("com.sun.jndi.ldap.connect.pool.maxsize", "2");
    System.setProperty("com.sun.jndi.ldap.connect.pool.protocol", "ssl");
    System.setProperty("com.sun.jndi.ldap.connect.pool.debug", "all");
    Hashtable<String, String> env = new Hashtable<>();
    env.put("com.sun.jndi.ldap.connect.pool", "true");
    env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("java.naming.provider.url", "ldaps://localhost:" + getLdapServer().getPortSSL() + "/ou=system");
    env.put("java.naming.ldap.factory.socket", ManagedSSLSocketFactory.class.getName());
    env.put("java.naming.security.protocol", "ssl");
    env.put("java.naming.security.principal", "uid=admin,ou=system");
    env.put("java.naming.security.credentials", "secret");
    env.put("java.naming.security.authentication", "simple");
    final int[] socketsCreated = new int[] { 0 };
    ManagedSSLSocketFactory.setSocketFactory(new ManagedSSLSocketFactory(sslContext.getSocketFactory()) {

        @Override
        public Socket createSocket(String host, int port) throws IOException {
            socketsCreated[0]++;
            return super.createSocket(host, port);
        }
    });
    InitialDirContext context = new InitialDirContext(env);
    context.close();
    new InitialDirContext(env);
    context.close();
    ManagedSSLSocketFactory.setSocketFactory(null);
    assertThat(socketsCreated[0], equalTo(1));
}
Also used : Hashtable(java.util.Hashtable) IOException(java.io.IOException) InitialDirContext(javax.naming.directory.InitialDirContext) Socket(java.net.Socket) Test(org.junit.Test)

Example 37 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project fess by codelibs.

the class LdapManager method getDirContext.

protected DirContextHolder getDirContext(final Supplier<Hashtable<String, String>> envSupplier) {
    DirContextHolder holder = contextLocal.get();
    if (holder == null) {
        final Hashtable<String, String> env = envSupplier.get();
        try {
            holder = new DirContextHolder(new InitialDirContext(env));
            contextLocal.set(holder);
            return holder;
        } catch (final NamingException e) {
            throw new LdapOperationException("Failed to create DirContext.", e);
        }
    } else {
        holder.inc();
        return holder;
    }
}
Also used : LdapOperationException(org.codelibs.fess.exception.LdapOperationException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)37 NamingException (javax.naming.NamingException)18 DirContext (javax.naming.directory.DirContext)18 Hashtable (java.util.Hashtable)17 Attributes (javax.naming.directory.Attributes)9 IOException (java.io.IOException)8 Attribute (javax.naming.directory.Attribute)7 Properties (java.util.Properties)5 Socket (java.net.Socket)4 SearchResult (javax.naming.directory.SearchResult)4 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 NamingEnumeration (javax.naming.NamingEnumeration)2 SearchControls (javax.naming.directory.SearchControls)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 LdapContext (javax.naming.ldap.LdapContext)2 JndiUtils.getInitialDirContext (com.facebook.presto.server.security.util.jndi.JndiUtils.getInitialDirContext)1 AuthenticationFailedException (com.google.gerrit.server.account.AuthenticationFailedException)1 LdapCtx (com.sun.jndi.ldap.LdapCtx)1