LDAP操作之java篇(2)

LDAP操作之java篇(2

       續接上一篇我們將舉例來講解對節點的操作(也就是entry),包括查詢、增加、修改、刪除節點。

         增加節點

/**

*entry :包含新增節點的所有信息

/*

public boolean addInformation(Entry entry)

       {

              try

              {

                     Attributes attrs = new BasicAttributes();

                     Enumeration attrEnum = entry.keys();

                     while (attrEnum.hasMoreElements())

                     {

                            String type = (String) attrEnum.nextElement();

                            Attribute oneAttr = new BasicAttribute(type);

                            Vector vals = (Vector) entry.get(type);

                            Enumeration valEnum = vals.elements();

                            while (valEnum.hasMoreElements())

                            {

                                   oneAttr.add((String) valEnum.nextElement());

                            }

                            attrs.put(oneAttr);

                     }

                     dc.createSubcontext(entry.getDN(), attrs);

                     return true;

              } catch (Exception ne) {

                     //System.err.println("Error: " + ne.getMessage());

                     ne.printStackTrace();

                     return false;

              }

       }

      

       舉一個構造entry的例子:

       Entry en    = new Entry();

       String enDN = "cn=dsafa,dc=gsmd,dc=com";

       Vector employeeName = new Vector();

       Vector objectClass = new Vector();

              Vector mailAddress = new Vector();

              Vector mobilePhone = new Vector();

              en.setDN(enDN);

              employeeName.add("liufu");

              en.put("employeeName",employeeName);

              objectClass.add("cssisMail");

              en.put("objectClass",objectClass);

              mailAddress.add("dsfsa");

              en.put("mailAddress",mailAddress);

              mobilePhone.add("1234");

              en.put("mobilePhone",mobilePhone);

      

         修改節點的信息

每個節點相當於一個實體,每個實體有很多屬性,當然我們要改的就是某些或某個屬性的值。

       /**

       *dn :節點的位置 如:ou = kk, dc = china ,dc=com

       /*

       public boolean modifyInformation(String dn)

       {

              try {

                     ModificationItem[] mods = new ModificationItem[2];

                     Attribute mobilePhone = new BasicAttribute("mobilePhone",

                     "12315");

                     Attribute employeeName = new BasicAttribute("employeeName","Smith");

                     mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,

                                   mobilePhone);

                     mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,

                                   employeeName);

                     dc.modifyAttributes(dn,mods);

                     return true;

              } catch (NamingException ne) {

                     System.err.println("Error: " + ne.getMessage());

                     return false;

              }

       }

      

         刪除指定的節點

這個簡單沒啥好說的把:)

public boolean deleteInformation(String dn)

       {

              try {

                     dc.destroySubcontext("cn=asdf,ou=mail,dc=gsmd,dc=com");

                     return true;

              } catch (NamingException ne) {

                     System.err.println("Error: " + ne.getMessage());

                     return false;

              }

       }

 

         修改節點的名字(dn

這個好像只能修改葉子節點的,有沒有人找到可以修改非葉子節點dn的方法,告訴[email protected]

              public boolean renameEntry(String oldDN,String newDN)

              {

                     try {

                            dc.rename(oldDN,newDN);

                            return true;

                     } catch (NamingException ne) {

                            System.err.println("Error: " + ne.getMessage());

                            return false;

                     }

              }

這兩篇文章就LDAP的一些操作進行了簡單的講解,ldap遠遠不止這些,偶還是一隻小小菜鳥,不斷在學習中,我會留下偶學習的痕跡的,希望能與各位大大交流,有啥問題也可以一起探討,[email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章