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));
}
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;
}
}
Aggregations