Package edu.vt.middleware.ldap
Class AbstractLdap<T extends LdapConfig>
- java.lang.Object
-
- edu.vt.middleware.ldap.AbstractLdap<T>
-
- Type Parameters:
T
- type of LdapConfig
- All Implemented Interfaces:
BaseLdap
- Direct Known Subclasses:
Ldap
,SearchDnResolver
public abstract class AbstractLdap<T extends LdapConfig> extends java.lang.Object implements BaseLdap
AbstractLdap
contains the functions for basic interaction with a LDAP. Methods are provided for connecting, binding, querying and updating.- Version:
- $Revision: 1440 $ $Date: 2010-06-27 18:41:34 +0200 (Sun, 27 Jun 2010) $
- Author:
- Middleware Services
-
-
Field Summary
Fields Modifier and Type Field Description protected static CopyResultHandler<javax.naming.Binding>
BINDING_COPY_RESULT_HANDLER
Default copy binding handler.protected T
config
LDAP configuration environment.protected ConnectionHandler
connectionHandler
LDAP connection handler.protected static CopyResultHandler<java.lang.Object>
COPY_RESULT_HANDLER
Default copy result handler.protected org.apache.commons.logging.Log
logger
Log for this class.protected static CopyResultHandler<javax.naming.NameClassPair>
NCP_COPY_RESULT_HANDLER
Default copy name class pair handler.protected static CopyResultHandler<javax.naming.directory.SearchResult>
SR_COPY_RESULT_HANDLER
Default copy search result handler, used if none supplied.
-
Constructor Summary
Constructors Constructor Description AbstractLdap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
This will close the connection to the LDAP.protected boolean
compare(java.lang.String dn, java.lang.String filter, java.lang.Object[] filterArgs)
This will perform an LDAP compare operation with the supplied filter and dn.boolean
connect()
This will establish a connection if one does not already exist by binding to the LDAP using parameters given byLdapConfig.getBindDn()
andLdapConfig.getBindCredential()
.protected void
create(java.lang.String dn, javax.naming.directory.Attributes attrs)
This will create the supplied dn in the LDAP namespace with the supplied attributes.protected void
delete(java.lang.String dn)
This will delete the supplied dn from the LDAP namespace.protected void
finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.protected javax.naming.directory.Attributes
getAttributes(java.lang.String dn, java.lang.String[] retAttrs, AttributeHandler... handler)
This will return the matching attributes associated with the supplied dn.protected javax.naming.ldap.LdapContext
getContext()
This will return an initialized connection to the LDAP.protected java.util.Iterator<javax.naming.directory.SearchResult>
getSchema(java.lang.String dn)
This will return the LDAP schema associated with the supplied dn.protected java.util.Iterator<javax.naming.NameClassPair>
list(java.lang.String dn)
This will enumerate the names bounds to the specified context, along with the class names of objects bound to them.protected java.util.Iterator<javax.naming.Binding>
listBindings(java.lang.String dn)
This will enumerate the names bounds to the specified context, along with the objects bound to them.protected void
modifyAttributes(java.lang.String dn, int modOp, javax.naming.directory.Attributes attrs)
This will modify the supplied attributes for the supplied value given by the modification operation.protected void
modifyAttributes(java.lang.String dn, javax.naming.directory.ModificationItem[] mods)
This will modify the supplied dn using the supplied modifications.protected void
operationRetry(javax.naming.ldap.LdapContext ctx, javax.naming.NamingException e, int count)
Confirms whether the supplied exception matches an exception fromLdapConfig.getOperationRetryExceptions()
and the supplied count is less thanLdapConfig.getOperationRetry()
.protected java.util.Iterator<javax.naming.directory.SearchResult>
pagedSearch(java.lang.String dn, java.lang.String filter, java.lang.Object[] filterArgs, javax.naming.directory.SearchControls searchControls, SearchResultHandler... handler)
This will query the LDAP with the supplied dn, filter, filter arguments, and search controls.boolean
reconnect()
This will close the current connection to the LDAP and establish a new connection to the LDAP usingconnect()
.protected void
rename(java.lang.String oldDn, java.lang.String newDn)
This will rename the supplied dn in the LDAP namespace.protected java.util.Iterator<javax.naming.directory.SearchResult>
search(java.lang.String dn, java.lang.String filter, java.lang.Object[] filterArgs, javax.naming.directory.SearchControls searchControls, SearchResultHandler... handler)
This will query the LDAP with the supplied dn, filter, filter arguments, and search controls.protected java.util.Iterator<javax.naming.directory.SearchResult>
searchAttributes(java.lang.String dn, javax.naming.directory.Attributes matchAttrs, java.lang.String[] retAttrs, SearchResultHandler... handler)
This will query the LDAP for the supplied dn, matching attributes and return attributes.protected void
setLdapConfig(T ldapConfig)
This will set the config parameters of thisLdap
.java.lang.String
toString()
Provides a descriptive string representation of this instance.
-
-
-
Field Detail
-
SR_COPY_RESULT_HANDLER
protected static final CopyResultHandler<javax.naming.directory.SearchResult> SR_COPY_RESULT_HANDLER
Default copy search result handler, used if none supplied.
-
NCP_COPY_RESULT_HANDLER
protected static final CopyResultHandler<javax.naming.NameClassPair> NCP_COPY_RESULT_HANDLER
Default copy name class pair handler.
-
BINDING_COPY_RESULT_HANDLER
protected static final CopyResultHandler<javax.naming.Binding> BINDING_COPY_RESULT_HANDLER
Default copy binding handler.
-
COPY_RESULT_HANDLER
protected static final CopyResultHandler<java.lang.Object> COPY_RESULT_HANDLER
Default copy result handler.
-
logger
protected final org.apache.commons.logging.Log logger
Log for this class.
-
connectionHandler
protected ConnectionHandler connectionHandler
LDAP connection handler.
-
config
protected T extends LdapConfig config
LDAP configuration environment.
-
-
Method Detail
-
setLdapConfig
protected void setLdapConfig(T ldapConfig)
This will set the config parameters of thisLdap
.- Parameters:
ldapConfig
-LdapConfig
-
compare
protected boolean compare(java.lang.String dn, java.lang.String filter, java.lang.Object[] filterArgs) throws javax.naming.NamingException
This will perform an LDAP compare operation with the supplied filter and dn. Note that to perform a real LDAP compare operation, your filter must be of the form '(name=value)'. Any other filter expression will result in a regular object level search operation. In either case the desired result is achieved, but the underlying LDAP invocation is different.- Parameters:
dn
-String
name to comparefilter
-String
expression to use for comparefilterArgs
-Object[]
to substitute for variables in the filter- Returns:
boolean
- result of compare operation- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
search
protected java.util.Iterator<javax.naming.directory.SearchResult> search(java.lang.String dn, java.lang.String filter, java.lang.Object[] filterArgs, javax.naming.directory.SearchControls searchControls, SearchResultHandler... handler) throws javax.naming.NamingException
This will query the LDAP with the supplied dn, filter, filter arguments, and search controls. This method will perform a search whose scope is defined in the search controls. The resultingIterator
is a deep copy of the original search results. If filterArgs is null, then no variable substitution will occur. Seejavax.naming.DirContext#search( String, String, Object[], SearchControls)
.- Parameters:
dn
-String
name to begin search atfilter
-String
expression to use for the searchfilterArgs
-Object[]
to substitute for variables in the filtersearchControls
-SearchControls
to perform search withhandler
-SearchResultHandler[]
to post process results- Returns:
Iterator
- of LDAP search results- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
pagedSearch
protected java.util.Iterator<javax.naming.directory.SearchResult> pagedSearch(java.lang.String dn, java.lang.String filter, java.lang.Object[] filterArgs, javax.naming.directory.SearchControls searchControls, SearchResultHandler... handler) throws javax.naming.NamingException
This will query the LDAP with the supplied dn, filter, filter arguments, and search controls. Seesearch(String, String, Object[], SearchControls, SearchResultHandler...)
. The PagedResultsControl is used in conjunction withLdapConfig.getPagedResultsSize()
to produce the results.- Parameters:
dn
-String
name to begin search atfilter
-String
expression to use for the searchfilterArgs
-Object[]
to substitute for variables in the filtersearchControls
-SearchControls
to perform search withhandler
-SearchResultHandler[]
to post process results- Returns:
Iterator
- of LDAP search results- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
searchAttributes
protected java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes(java.lang.String dn, javax.naming.directory.Attributes matchAttrs, java.lang.String[] retAttrs, SearchResultHandler... handler) throws javax.naming.NamingException
This will query the LDAP for the supplied dn, matching attributes and return attributes. This method will always perform a one level search. The resultingIterator
is a deep copy of the original search results. If matchAttrs is empty or null then all objects in the target context are returned. If retAttrs is null then all attributes will be returned. If retAttrs is an empty array then no attributes will be returned. Seejavax.naming.DirContext#search(String, Attributes, String[])
.- Parameters:
dn
-String
name to search inmatchAttrs
-Attributes
attributes to matchretAttrs
-String[]
attributes to returnhandler
-SearchResultHandler[]
to post process results- Returns:
Iterator
- of LDAP search results- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
list
protected java.util.Iterator<javax.naming.NameClassPair> list(java.lang.String dn) throws javax.naming.NamingException
This will enumerate the names bounds to the specified context, along with the class names of objects bound to them. The resultingIterator
is a deep copy of the original search results. SeeContext.list(String)
.- Parameters:
dn
-String
LDAP context to list- Returns:
Iterator
- LDAP search result- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
listBindings
protected java.util.Iterator<javax.naming.Binding> listBindings(java.lang.String dn) throws javax.naming.NamingException
This will enumerate the names bounds to the specified context, along with the objects bound to them. The resultingIterator
is a deep copy of the original search results. SeeContext.listBindings(String)
.- Parameters:
dn
-String
LDAP context to list- Returns:
Iterator
- LDAP search result- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
getAttributes
protected javax.naming.directory.Attributes getAttributes(java.lang.String dn, java.lang.String[] retAttrs, AttributeHandler... handler) throws javax.naming.NamingException
This will return the matching attributes associated with the supplied dn. If retAttrs is null then all attributes will be returned. If retAttrs is an empty array then no attributes will be returned. Seejavax.naming.DirContext#getAttributes(String, String[])
.- Parameters:
dn
-String
named object in the LDAPretAttrs
-String[]
attributes to returnhandler
-AttributeHandler[]
to post process results- Returns:
Attributes
- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
getSchema
protected java.util.Iterator<javax.naming.directory.SearchResult> getSchema(java.lang.String dn) throws javax.naming.NamingException
This will return the LDAP schema associated with the supplied dn. The resultingIterator
is a deep copy of the original search results. Seejavax.naming.DirContext#getSchema(String)
.- Parameters:
dn
-String
named object in the LDAP- Returns:
Iterator
- LDAP search result- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
modifyAttributes
protected void modifyAttributes(java.lang.String dn, int modOp, javax.naming.directory.Attributes attrs) throws javax.naming.NamingException
This will modify the supplied attributes for the supplied value given by the modification operation. modOp must be one of: ADD_ATTRIBUTE, REPLACE_ATTRIBUTE, REMOVE_ATTRIBUTE. The order of the modifications is not specified. Where possible, the modifications are performed atomically. Seejavax.naming.DirContext#modifyAttributes( String, int, Attributes)
.- Parameters:
dn
-String
named object in the LDAPmodOp
-int
modification operationattrs
-Attributes
attributes to be used for the operation, may be null- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
modifyAttributes
protected void modifyAttributes(java.lang.String dn, javax.naming.directory.ModificationItem[] mods) throws javax.naming.NamingException
This will modify the supplied dn using the supplied modifications. The modifications are performed in the order specified. Each modification specifies a modification operation code and an attribute on which to operate. Where possible, the modifications are performed atomically. Seejavax.naming.DirContext#modifyAttributes(String, ModificationItem[])
.- Parameters:
dn
-String
named object in the LDAPmods
-ModificationItem[]
modifications- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
create
protected void create(java.lang.String dn, javax.naming.directory.Attributes attrs) throws javax.naming.NamingException
This will create the supplied dn in the LDAP namespace with the supplied attributes. Seejavax.naming.DirContext#createSubcontext(String, Attributes)
. Note that the context created by this operation is immediately closed.- Parameters:
dn
-String
named object in the LDAPattrs
-Attributes
attributes to be added to this entry- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
rename
protected void rename(java.lang.String oldDn, java.lang.String newDn) throws javax.naming.NamingException
This will rename the supplied dn in the LDAP namespace. SeeContext.rename(String, String)
.- Parameters:
oldDn
-String
object to renamenewDn
-String
new name- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
delete
protected void delete(java.lang.String dn) throws javax.naming.NamingException
This will delete the supplied dn from the LDAP namespace. Note that this method does not throw NameNotFoundException if the supplied dn does not exist. SeeContext.destroySubcontext(String)
.- Parameters:
dn
-String
named object in the LDAP- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
connect
public boolean connect() throws javax.naming.NamingException
This will establish a connection if one does not already exist by binding to the LDAP using parameters given byLdapConfig.getBindDn()
andLdapConfig.getBindCredential()
. If these parameters have not been set then an anonymous bind will be attempted. This connection must be closed usingclose()
. Any method which requires an LDAP connection will call this method independently. This method should only be used if you need to verify that you can connect to the LDAP.
-
reconnect
public boolean reconnect() throws javax.naming.NamingException
This will close the current connection to the LDAP and establish a new connection to the LDAP usingconnect()
.
-
close
public void close()
This will close the connection to the LDAP.
-
getContext
protected javax.naming.ldap.LdapContext getContext() throws javax.naming.NamingException
This will return an initialized connection to the LDAP.- Returns:
LdapContext
- Throws:
javax.naming.NamingException
- if the LDAP returns an error
-
operationRetry
protected void operationRetry(javax.naming.ldap.LdapContext ctx, javax.naming.NamingException e, int count) throws javax.naming.NamingException
Confirms whether the supplied exception matches an exception fromLdapConfig.getOperationRetryExceptions()
and the supplied count is less thanLdapConfig.getOperationRetry()
.LdapConfig.getOperationRetryWait()
is used in conjunction withLdapConfig.getOperationRetryBackoff()
to delay retries. Callsclose()
if no exception is thrown, which allows the client to reconnect when the operation is performed again.- Parameters:
ctx
-LdapContext
that performed the operatione
-NamingException
that was throwncount
-int
operation attempts- Throws:
javax.naming.NamingException
- if the operation won't be retried
-
toString
public java.lang.String toString()
Provides a descriptive string representation of this instance.- Overrides:
toString
in classjava.lang.Object
- Returns:
- String of the form $Classname@hashCode::config=$config.
-
finalize
protected void finalize() throws java.lang.Throwable
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
- if an exception is thrown by this method
-
-