参考文档:
http://developer.novell.com/documentation/samplecode/jldap_sample/index.htm
package com.ebizserve.cp.jldap;
import com.novell.ldap.*;
public class TestJLDAP {
public static void main(String[] args) throws LDAPException {
LDAPConnection lc = new LDAPConnection();
try {
lc.connect( ldapHost, ldapPort );
lc.bind( ldapVersion, loginDN, password.getBytes("UTF8"));
// addOrg(lc);
add(lc);
search(lc);
delete(lc);
} catch (LDAPException e) {
System.err.print("连接异常! ");
e.printStackTrace();
throw e;
}
if (lc.isConnected()) {
lc.disconnect();
}
}
public static void add(LDAPConnection lc) throws LDAPException {
LDAPAttributeSet attributeSet = new LDAPAttributeSet();
attributeSet.add(new LDAPAttribute("objectclass", new String[] { "top",
"posixAccount", "person", "inetOrgPerson",
"organizationalPerson" }));
attributeSet.add(new LDAPAttribute("cn", "a"));
attributeSet.add(new LDAPAttribute("sn", "b"));
attributeSet.add(new LDAPAttribute("uidNumber", "1"));
attributeSet.add(new LDAPAttribute("gidNumber", "1"));
attributeSet.add(new LDAPAttribute("homeDirectory", "1"));
attributeSet.add(new LDAPAttribute("givenName", "测试用人员"));
LDAPEntry entry = new LDAPEntry("uid=ab,ou=test,dc=test,dc=com",
attributeSet);
lc.add(entry);
System.out.println("成功的添加了ab!");
}
public static void delete(LDAPConnection lc) throws LDAPException {
lc.delete("uid=ab,ou=test,dc=test,dc=com");
System.out.println("成功的删除了ab!");
}
public static void search(LDAPConnection lc) throws LDAPException {
LDAPSearchResults rs = lc.search("ou=test,dc=test,dc=com",
LDAPConnection.SCOPE_SUB, "objectClass=posixAccount", null,
false);
while (rs.hasMore()) {
LDAPEntry entry = rs.next();
System.out.println(entry.getAttribute("uid").getStringValue()
+ entry.getAttribute("givenName").getStringValue() + " "
+ entry.getAttribute("sn").getStringValue());
}
}
public static void addOrg(LDAPConnection lc) throws LDAPException {
LDAPAttributeSet attributeSet = new LDAPAttributeSet();
attributeSet
.add(new LDAPAttribute("objectclass", "organizationalUnit"));
attributeSet.add(new LDAPAttribute("ou", "test"));
LDAPEntry entry = new LDAPEntry("ou=test,dc=test,dc=com",
attributeSet);
lc.add(entry);
System.out.println("成功的添加了org!");
}
}