KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > simya > ldap > beans > LdapAddEntryBean


1 package net.simya.ldap.beans;
2
3 import javax.naming.*;
4 import javax.naming.directory.*;
5 import java.util.Enumeration JavaDoc;
6 import java.util.Hashtable JavaDoc;
7
8 /**
9  * This code adds the entry into the ldap
10  *
11  * next version will provide : binary attributes
12  */

13 public class LdapAddEntryBean {
14     
15     private DirContext ctx = null;
16     
17     private Attributes attrList = new BasicAttributes(true); // other attributes
18

19     private Hashtable JavaDoc attrTable = new Hashtable JavaDoc();
20     
21     private String JavaDoc DN;
22             
23     
24     public void setContext (DirContext ctx)
25     {
26         this.ctx = ctx;
27     }
28     
29         
30     public void setDN ( String JavaDoc dn)
31     {
32         this.DN = dn ;
33     }
34         
35     
36     public void setAddAttribute ( String JavaDoc name, Object JavaDoc value)
37     {
38         // get attribute from hashtable
39
Attribute tmp = (Attribute) attrTable.get(name);
40         // if attribute doesn't exist, add it
41
if (tmp == null)
42         {
43             attrTable.put ( name, new BasicAttribute(name));
44         
45             tmp = (Attribute) attrTable.get(name);
46             tmp.add ( value);
47         
48             attrTable.put (name, tmp);
49         }
50         // if exists, add value
51
else
52         {
53             tmp = (Attribute) attrTable.get(name);
54             tmp.add ( value);
55         }
56     }
57     
58     public void addEntry ()
59     throws NamingException
60     {
61         // create attribute list
62
Enumeration JavaDoc en= attrTable.keys();
63         // get attributes from hash table
64
while (en.hasMoreElements())
65         {
66             String JavaDoc name = (String JavaDoc) en.nextElement();
67             // get attribute and put into Attributes array
68
attrList.put((Attribute) attrTable.get(name));
69         }
70         
71         // add entry into ldap
72
ctx.createSubcontext(DN, attrList);
73     }
74     
75 }// end of class
76

77
Popular Tags