Novell ldap开发组件实例: ModifyAttrs.java

http://developer.novell.com/documentation/jldap/jldapenu/api/com/novell/ldap/LDAPModification.html

java 代码
  1. //Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/f91a68eb-ad37-4526-92b1-b1938f37b871/ModifyAttrs.java  
  2. //Warning: This code has been marked up for HTML  
  3.   
  4. /******************************************************************************* 
  5.  * $Novell: ModifyAttrs.java,v 1.21 2003/08/21 11:35:10 $ 
  6.  * Copyright (C) 1999, 2000, 2001 Novell, Inc. All Rights Reserved. 
  7.  * 
  8.  * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND 
  9.  * TREATIES. USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE 
  10.  * AGREEMENT ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS 
  11.  * THIS WORK. PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO 
  12.  * DEVELOPER A ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE 
  13.  * CODE IN ITS PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS 
  14.  * TO MARKET, DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF 
  15.  * DEVELOPER'S PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR 
  16.  * DEVELOPER'S CUSTOMERS WITH RESPECT TO THIS CODE. 
  17.  * 
  18.  * $name:         ModifyAttrs.java 
  19.  * 
  20.  * $description:  The ModifyAttrs example modifies the attributes of an entry. 
  21.  *                It also demonstrates the use of an ArrayList to store 
  22.  *                and indeterminate number of of modifications. 
  23.  ******************************************************************************/  
  24.   
  25. import com.novell.ldap.*;  
  26.   
  27. import java.util.Date;  
  28.   
  29. import java.util.ArrayList;  
  30.   
  31. import java.io.UnsupportedEncodingException;  
  32.   
  33.   
  34.   
  35. public class ModifyAttrs   
  36.   
  37. {  
  38.   
  39.     public static void main( String[] args )   
  40.   
  41.     {          
  42.   
  43.         int returnCode = 0;  
  44.   
  45.         if (args.length != 4) {  
  46.   
  47.             System.err.println("Usage:   java ModifyAttrs   
  48.   
  49.                              + ");  
  50.   
  51.             System.err.println("Example: java ModifyAttrs Acme.com "  
  52.   
  53.                              + "\"cn=Admin,o=Acme\" secret\n"  
  54.   
  55.                              + "         \"cn=JSmith,ou=Sales,o=Acme\"");  
  56.   
  57.             System.exit(1);  
  58.   
  59.         }  
  60.   
  61.   
  62.   
  63.         int ldapPort  = LDAPConnection.DEFAULT_PORT;  
  64.   
  65.         int ldapVersion  = LDAPConnection.LDAP_V3;          
  66.   
  67.         Date currentDate = new Date();  
  68.   
  69.         String ldapHost = args[0];  
  70.   
  71.         String loginDN  = args[1];  
  72.   
  73.         String password = args[2];  
  74.   
  75.         String dn = args[3];  
  76.   
  77.         LDAPConnection lc = new LDAPConnection();  
  78.   
  79.         ArrayList modList = new ArrayList();  
  80.   
  81.   
  82.   
  83.         /* To add a new value to an attribute (creates attribute if necessary) 
  84.          *     -- Specify the dn of the entry to modify 
  85.          *     -- Specify the attribute and value to add 
  86.          *     -- Specify the add modification type 
  87.          *     -- Add modification to the modification array 
  88.          *     -- Call LDAPConnection modify() method 
  89.         /* To replace values of an attribute (creates attribute if necessary) 
  90.          *     -- Specify the dn of the entry to modify 
  91.          *     -- Specify the attribute and new values to replace the old ones 
  92.          *     -- Specify the replace modification type 
  93.          *     -- Add modification to the modification array 
  94.          *     -- Call LDAPConnection modify() method 
  95.         /* To delete or modify a single attribute value 
  96.          *     -- Specify the dn of the entry to modify 
  97.          *     -- Specify the attribute and value to be modified 
  98.          *     -- Specify the delete modification type 
  99.          *     -- Add modification to the modification array 
  100.          * --  To add a new value in place of the one we just deleted 
  101.          *     -- Specify the same attribute and new its value 
  102.          *     -- Specify the add modification type 
  103.          *     -- Add modification to the modification array 
  104.          *     -- Call LDAPConnection modify() method 
  105.          */  
  106.   
  107.         String desc =  
  108.   
  109.             "This object was modified at " + new Date( currentDate.getTime());  
  110.   
  111.        // Add a new value to the description attribute  
  112.   
  113.   
  114.         LDAPAttribute attribute = new LDAPAttribute( "description", desc);  
  115.   
  116.         modList.add( new LDAPModification(LDAPModification.ADD, attribute));  
  117.   
  118.   
  119.   
  120.        // Replace all values the E-mail address with a new value  
  121.   
  122.   
  123.         String email = "James_Smith@Acme.com";  
  124.   
  125.         attribute = new LDAPAttribute( "mail", email);  
  126.   
  127.         modList.add( new LDAPModification(LDAPModification.REPLACE, attribute));  
  128.   
  129.   
  130.   
  131.        // Change the phone number  
  132.   
  133.   
  134.        //  First we delete the old phone number  
  135.   
  136.   
  137.         String phone1= "1 801 555 1212";  
  138.   
  139.         attribute= new LDAPAttribute( "telephoneNumber", phone1);  
  140.   
  141.         modList.add( new LDAPModification(LDAPModification.DELETE, attribute));  
  142.   
  143.   
  144.   
  145.        //  Now we add the new phone number  
  146.   
  147.   
  148.         String phone2 = "1 423 555 1212";  
  149.   
  150.         attribute= new LDAPAttribute( "telephoneNumber", phone2);  
  151.   
  152.         modList.add( new LDAPModification(LDAPModification.ADD, attribute));  
  153.   
  154.   
  155.   
  156.         LDAPModification[] mods = new LDAPModification[modList.size()];   
  157.   
  158.         mods = (LDAPModification[])modList.toArray(mods);  
  159.   
  160.         
  161.   
  162.         try {  
  163.   
  164.            // connect to the server  
  165.   
  166.   
  167.             lc.connect( ldapHost, ldapPort);  
  168.   
  169.            // bind to the server  
  170.   
  171.   
  172.             lc.bind( ldapVersion, loginDN, password.getBytes("UTF8") );  
  173.   
  174.               
  175.   
  176.            // Add a known phone number value so we have something to change  
  177.   
  178.   
  179.             lc.modify( dn, new LDAPModification(  
  180.   
  181.                     LDAPModification.ADD,  
  182.   
  183.                     new LDAPAttribute( "telephoneNumber""1 801 555 1212")));  
  184.   
  185.         } catch( LDAPException e1) {  
  186.   
  187.            // If phonenumber value already exists, just go on,  
  188.   
  189.   
  190.            //      otherwise it's an error  
  191.   
  192.   
  193.             if( e1.getResultCode() != LDAPException.ATTRIBUTE_OR_VALUE_EXISTS) {  
  194.   
  195.                 System.out.println("Cannot create attribute: " + e1.toString());  
  196.   
  197.                 System.exit(1);  
  198.   
  199.             }  
  200.   
  201.         }  
  202.   
  203.         catch( UnsupportedEncodingException e ) {  
  204.   
  205.             System.out.println( "Error: " + e.toString() );  
  206.   
  207.         }  
  208.   
  209.               
  210.   
  211.        // Note: All the above modifications will be performed as an atomic  
  212.   
  213.   
  214.        // unit, if all do not succeed, the operation fails and the  
  215.   
  216.   
  217.        // directory is unchanged by this operation.  
  218.   
  219.   
  220.         try {  
  221.   
  222.             lc.modify( dn, mods);  
  223.   
  224.             System.out.println(  
  225.   
  226.                         "Successfully modified the attributes of the entry." );  
  227.   
  228.         } catch( LDAPException e2 ) {  
  229.   
  230.            // Tell what happened  
  231.   
  232.   
  233.             System.err.println( "Error: " + e2.toString() );  
  234.   
  235.               
  236.   
  237.            // Indicate the operation failed  
  238.   
  239.   
  240.             returnCode = 1;  
  241.   
  242.               
  243.   
  244.            // Didn't change the directory, delete initial phone number value  
  245.   
  246.   
  247.             try {  
  248.   
  249.                 lc.modify( dn, new LDAPModification( LDAPModification.DELETE,  
  250.   
  251.                                new LDAPAttribute( "telephoneNumber", phone1)));  
  252.   
  253.             } catch( Exception e) {  
  254.   
  255.                // ignore  
  256.   
  257.   
  258.             }  
  259.   
  260.         } finally {  
  261.   
  262.             if( returnCode == 0) {  
  263.   
  264.                 try {  
  265.   
  266.                    // Cleanup - get rid of the updated description, mail,  
  267.   
  268.   
  269.                    //           and telephone attribute values  
  270.   
  271.   
  272.                    // We delete only those values that we added.  If the  
  273.   
  274.   
  275.                    // value deleted is the only value of the respective  
  276.   
  277.   
  278.                    // attribute, then the attribute is deleted.  
  279.   
  280.   
  281.                     modList.clear();  
  282.   
  283.                     modList.add(new LDAPModification( LDAPModification.DELETE,  
  284.   
  285.                                 new LDAPAttribute( "description", desc)));  
  286.   
  287.                     modList.add(new LDAPModification( LDAPModification.DELETE,  
  288.   
  289.                                 new LDAPAttribute( "mail", email)));  
  290.   
  291.                     modList.add(new LDAPModification( LDAPModification.DELETE,  
  292.   
  293.                                 new LDAPAttribute( "telephoneNumber", phone2)));  
  294.   
  295.                     mods = new LDAPModification[modList.size()];   
  296.   
  297.                     mods = (LDAPModification[])modList.toArray(mods);  
  298.   
  299.                     lc.modify( dn, mods);  
  300.   
  301.                 } catch( Exception e) {  
  302.   
  303.                    // ignore  
  304.   
  305.   
  306.                 }  
  307.   
  308.             }  
  309.   
  310.            // Always disconnect  
  311.   
  312.   
  313.             try {  
  314.   
  315.                 lc.disconnect();  
  316.   
  317.             } catch( Exception e) {  
  318.   
  319.                // ignore  
  320.   
  321.   
  322.             }  
  323.   
  324.         }   
  325.   
  326.         System.exit(returnCode);  
  327.   
  328.     }  
  329.   
  330. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值