public class DirContextSource extends Object
DirContext objects. The basic
idea is borrowed from DataSource where you get a database connection. Same does
this class with directory contexts. This directory context source has built-in support for
anonymous and GSS-API with Kerberos 5 authentication. If you intend to use the latter, make sure
that your environment is properly configured.
Here is a minimal example how to create a DirContextSource with the supplied builder:
DirContextSource.Builder builder = new DirContextSource.Builder("ldap://hostname");
DirContextSource contextSource = builder.build();
// try and catch block omitted for the sake of brevity, handle NamingException appropriately
DirContext context = contextSource.getDirContext();
// Perform operations
context.close();
Before returning a DirContext the source will loop several times until a connection has
been established or the number of retries are exhausted, which ever comes first.
A DirContextSource object will be initially preconfigured by its builder for you:
com.sun.jndi.ldap.LdapCtxFactory.DirContextSource.
A complete overview of all DirContext properties can be found
here.
Make sure that you pass reasonable/valid values only otherwise the behavior is undefined.
| Modifier and Type | Class and Description |
|---|---|
static class |
DirContextSource.Auth
Enum containing all supported authentication mechanisms.
|
static class |
DirContextSource.Builder
A builder to construct a
DirContextSource with a fluent interface. |
| Modifier and Type | Field and Description |
|---|---|
protected static Oid |
KRB5_MECHANISM |
| Modifier and Type | Method and Description |
|---|---|
protected DirContext |
getAnonymousDirContext() |
DirContext |
getDirContext()
Returns a ready-to-use
DirContext. |
protected DirContext |
getGssApiDirContext() |
protected static final Oid KRB5_MECHANISM
protected DirContext getGssApiDirContext() throws NamingException
NamingExceptionprotected DirContext getAnonymousDirContext() throws NamingException
NamingExceptionpublic DirContext getDirContext() throws NamingException
DirContext. Do not forget to close the context after all
operations.DirContextNamingException - thrown if a problem with the creation arisesCopyright © 2012–2019 Michael Osipov. All rights reserved.