摘自:http://linliangyi2007.iteye.com/blog/167128
从JDK5.0开始,对LDAP协议的数据访问操作就被集成在javax的扩展API包中,并随同JDK一并发布,这一章节,我们主要介绍API包中的类信息。
javax.naming.directory 包的结构
常用API解析
javax.naming.directory.InitialDirContext,初始化目录服务上下文类
该类是LDAP数据内容的操作工具类,通过该类可以执行绑定LDAP服务器、新增LDAP条目、获取条目实例、修改条目属性、删除条目和根据条件搜索条目等操作。常用方法说明如下:
初始化LDAP 目录服务上下文(相当于使用JDBC打开一个数据库链接)
InitialDirContext(Hashtable,?> environment)
绑定/创建LDAP条目对象(相当于新增一个LDAP条目数据bind(Name
name, Object obj, Attributes attrs)
bind(String name, Object obj, Attributes attrs)
createSubcontext(Name name, Attributes attrs)
createSubcontext(String name, Attributes attrs)
获取条目实例(属性集)
getAttributes(Name name)
getAttributes(Name name, String[] attrIds)
getAttributes(String name)
getAttributes(String name, String[] attrIds)
修改条目属性
modifyAttributes(Name name, int mod_op, Attributes attrs)
modifyAttributes(Name name, ModificationItem[] mods)
modifyAttributes(String name, int mod_op, Attributes attrs)
modifyAttributes(String name, ModificationItem[] mods)
删除条目
destroySubcontext(Name name)
destroySubcontext(String name)
根据属性集搜索条目
search(Name name, Attributes matchingAttributes)
search(Name name, Attributes matchingAttributes, String[] attributesToReturn)
search(String name, Attributes matchingAttributes)
search(String name, Attributes matchingAttributes, String[] attributesToReturn)
根据过滤器搜索条目
search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons)
search(Name name, String filter, SearchControls cons)
search(String name, String filterExpr, Object[] filterArgs, SearchControls cons)
search(String name, String filter, SearchControls cons)
javax.naming.directory.BasicAttribute,LDAP基本属性对象
该类用来表示LDAP条目中的单个属性对象。在目录服务中,每个属性名称是可以对应多个的属性值的。
构建属性对象
BasicAttribute(String id)
BasicAttribute(String id, boolean ordered)
BasicAttribute(String id, Object value)
BasicAttribute(String id, Object value, boolean ordered)
添加属性值
add(int ix, Object attrVal),添加属性值到多值属性的指定位置
add(Object attrVal) , 追加属性值到多值属性尾部
判断属性值是否包含
contains(Object attrVal) , 多值属性中有一个值是匹配的,返回true
获取属性值
get(),取得属性值中的一个
get(int ix),从多值属性中的指定位置取值
获取属性ID
getID(),属性的ID就是属性名
删除属性值
remove(int ix),删除指定位置的属性值
remove(Object attrval),删除指定的属性值
javax.naming.directory.BasicAttributes,LDAP实体的属性集
该类表示一个LDAP条目绑定的属性集合,在绝大多数情况下,一个LDAP条目存在多个属性。
构造属性集
BasicAttributes()
BasicAttributes(boolean ignoreCase),属性ID是否大小写敏感,建议不要使用敏感
BasicAttributes(String attrID, Object val)
BasicAttributes(String attrID, Object val, boolean ignoreCase)
获取属性集中的单个属性对象
get(String attrID)
获取全部属性的枚举
getAll()
获取全部属性的ID枚举
getIDs()
添加新属性
put(Attribute attr)
put(String attrID, Ob