使用JNDI操做LDAP(2)(轉載)

好了,如今介紹一些簡單的操做。數組

 

l         修改屬性ui

修改屬性包括增長,修改,刪除屬性。spa

修改一個對象的屬性的方法之一就是提供一個修改請求列表(ModificationItem)。每一個ModificationItem由必定數量的指定修改類型的常量(ADD_ATTRIBUTE,REPLACE_ATTRIBUTE,REMOVE_ATTRIBUTE)以及修改的屬性Attribute組成。orm

 

下面是我寫的簡單的操做代碼:對象

    /**事務

     * updates the specified extension information,including add,replace,remove attributeip

     * @param search_base The search distinguishedName.ci

     * @param mod_op The modificational operation,can use ADD_ATTRIBUTE,REPLACE_ATTRIBUTE,REMOVE_ATTRIBUTE.開發

     * @param ctx The ldapcontext.rem

     */

    public static void updateAttribute(String search_base,int mod_op,LdapContext ctx){

        try {

            Attributes attrs = null;

            Attribute attr = null;           

            //define ModificationItem

            ModificationItem[] mods =new ModificationItem[1];

           

            //operate the ModificationItem

            switch(mod_op){

                //operation add_attribute

                case LdapContext.ADD_ATTRIBUTE:{

                    //instance an attribute to be added

                    attr = new BasicAttribute("description");

                    //value of attribute description to add

                    attr.add("I add an attribute.");

                    //instance ModificationItem

                    mods[0] = new ModificationItem(LdapContext.ADD_ATTRIBUTE,attr);

                    //add description attribute

                  ctx.modifyAttributes(search_base,mods);

                    attrs = ctx.getAttributes(search_base,new String[]{"description"});

                    //print the value of new attribute

                    System.out.println("****new attribute****");

                    System.out.println("attribute: description ");

                    System.out.println("value:"+attrs.get("description"));                   

                    break;

                }

               

                //operation replace_attribute

                case LdapContext.REPLACE_ATTRIBUTE:{

                    //print the value of original attribute

                    attrs = ctx.getAttributes(search_base,new String[]{"description"});                   

                    System.out.println("****original attribute****");

                    System.out.println("attribute:description");

                    System.out.println("value:"+attrs.get("description"));

                    //instance an attribute to be replaced

                    attr = new BasicAttribute("description");

                    //add value of attribute description to replace

                    attr.add("I replace an attribute.");

                    //instance ModificationItem

                   mods[0] = new ModificationItem(LdapContext.REPLACE_ATTRIBUTE,attr);

                    //replace description attribute                   

                    ctx.modifyAttributes(search_base,mods);

                    attrs = ctx.getAttributes(search_base,new String[]{"description"});

                    //print the value of replaced attribute

                    System.out.println("****replaced attribute****");

                    System.out.println("attribute:description");

                    System.out.println("value:"+attrs.get("description"));

                    break;

                }

               

                //operation remove_attribute

                case LdapContext.REMOVE_ATTRIBUTE:{

                    //print the value of original attribute

                    attrs = ctx.getAttributes(search_base,new String[]{"description"});                   

                    System.out.println("****original attribute****");

                    System.out.println("attribute:description");

                    System.out.println("value:"+attrs.get("description"));

                    //instance an attribute to be removed

                    attr = new BasicAttribute("description");

                    //instance ModificationItem

                    mods[0] = new ModificationItem(LdapContext.REMOVE_ATTRIBUTE,attr);

                    //remove description attribute                   

                    ctx.modifyAttributes(search_base,mods);

                    attrs = ctx.getAttributes(search_base,new String[]{"description"});

                    //print the value of removed attribute

                    System.out.println("****removed attribute****");

                    System.out.println("attribute:description");

                    System.out.println("value:"+attrs.get("description"));

                    break;

                }

            }           

        } catch (Exception e){

            System.out.println(e.toString());

        }       

    }

 

這個例子裏面有註釋,並且很好懂,我就不一一詳解了。

要注意的是,ModificationItem[]數組能夠指定一系列的操做,修改的順序和它們出如今數組中的順序一致,要麼全部的修改都完成,要麼一個都不完成,即一個遨豪一直專一於Liferay門戶本地化服務,致力於爲中國用戶提供最佳的Liferay門戶解決方案,其主要服務包括:

1. Liferay門戶二次開發服務(基於客戶需求)

2. Liferay門戶技術支持服務 (現場+遠程)

3. Liferay門戶定製培訓服務 (現場+遠程)

4. Liferay門戶企業版服務  (提供liferay企業版+專業技術支持)

------------------------------------------------------------

遨豪(大連)科技有限公司

Liferay 中國合做夥伴

-------------------------------------

市場部: Mr.Luo

-------------------------------------

電話:411-8405-2127

傳真:411-8489-8263

MSN: liferayjw@hotmail.com

QQ:  1209462980

email:jiajia6f@163.com

------------------------------------

地址:大連高新技術產業園區

-------------------------------------

網址: www.aukcell.com

-------------------------------------修改操做是一個事務。

相關文章
相關標籤/搜索