访问LDAP数据的API--JLDAP 晴
作者: Leo 分类:技术文档 出处:本站原创 大 | 中 | 小
JLDAP是由novel开发的,原是针对Novel的NDS目录设计的JAVA访问工具。NOVEL的NDS和网景(NETSCAPE)的目录是工具界最早的目录产品。JLDAP并非JNDI的服务供应者,而是同一抽象层次下的访问工具集。与JNDI-LDAP相比,JLDAP更接近于类关系数据库的访问方式。
NDS是遵守LDAP协议的并进行了扩展的类MAD产品。而NOVEL也已把JLDAP捐献给了OPENLDAP开源项目,可以世界范围内自由使用。与 JNDI相比,JLDAP无须继承DirContext才能实现添加,也无需预先生成添加的类,可以象普通数据访问那样,生成连接,然后使用::add方法添加。这样,添加的灵活性要强于JNDI。
但由于JLDAP目前是访问NDS,因此,它不具备JNDI完全面向对象存储的能力,对于高级的LDAP应用,JLDAP不是合适的选择。
例:
======================================================================
1. package com.yydt.test;
2.
3. import com.novell.ldap.LDAPAttribute;
4. import com.novell.ldap.LDAPAttributeSet;
5. import com.novell.ldap.LDAPConnection;
6. import com.novell.ldap.LDAPEntry;
7. import com.novell.ldap.LDAPException;
8.
9. public class AddEntry {
10. int ldapVersion = LDAPConnection.LDAP_V3;
11.
12. public void testAddEntry(){
13. // int ldapPort = LDAPConnection.DEFAULT_PORT;
14. int ldapPort = 10389;
15. String ldapHost = "localhost";
16. String loginDN = "uid=admin,ou=system";
17. String password = "secret";
18. String dn = "cn=Huang zhenwei,ou=system";
19.
20. LDAPAttribute attribute = null;
21. LDAPAttributeSet attributeSet = new LDAPAttributeSet();
22.
23. String objectclass_values[] = {"inetOrgPerson"};
24. attribute = new LDAPAttribute("objectclass",objectclass_values);
25. attributeSet.add(attribute);
26. String cn_values[] = {"James Smith","Jim Smith","Jimmy Smith"};
27. attribute = new LDAPAttribute("cn",cn_values);
28. attributeSet.add(attribute);
29. String givenname_values[] = {"James","Jim","Jimmy"};
30. attribute = new LDAPAttribute("givenname",givenname_values);
31. attributeSet.add(attribute);
32. attributeSet.add(new LDAPAttribute("sn","Smith"));
33. attributeSet.add(new LDAPAttribute("telephonenumber","1 801 555 1212"));
34. attributeSet.add(new LDAPAttribute("mail","[email protected]"));
35. this.addEntry(ldapHost,ldapPort,loginDN,password,dn,attributeSet);
36. }
37.
38.
39. /*To Add an entry to the directory,
40. * --Create the attributes of the entry and add them to an attribute set
41. * --Specify the DN of the entry to be created
42. * --Create an LDAPEntry object with the DN and the attribute set
43. * --Call the LDAPConnection add method to add it to the directory
44. */
45. public void addEntry(String ldapHost,int ldapPort,String loginDN,String password,String dn,LDAPAttributeSet attributeSet){
46. LDAPConnection lc = new LDAPConnection();
47.
48. LDAPEntry newEntry = new LDAPEntry(dn,attributeSet);
49. try{
50. //connect to the server
51. lc.connect(ldapHost,ldapPort);
52. //authenticate to the server
53. lc.bind(ldapVersion,loginDN,password.getBytes("UTF8));
54. lc.add(newEntry);
55. // lc.delete(dn);
56. System.out.println("\nAdded object: " + dn + " successfully.");
57. //disconnect with the server
58. lc.disconnect();
59. }catch(LDAPException e){
60. System.out.println("Error: " + e.toString());
61. } catch (UnsupportedEncodingException e) {
62. // TODO Auto-generated catch block
63. e.printStackTrace();
64. }
65. System.exit(0);
66. }
67.
68. /**
69. * @param args
70. */
71. public static void main(String[] args) {
72. new AddEntry().testAddEntry();
73. }
74.
75. }
======================================================================