封装JNDI操作LDAP服务器的工具类3

  1. package com.common.ldapconnection;    
  2.   
  3. import java.util.List;    
  4. import java.util.Vector;    
  5.   
  6. /**   
  7.  *   
  8.  * <p>功能描述:ldap的处理类,提供了各种操作ldap的方法。</p>   
  9.  * @author liaowufeng   
  10.  * @version 1.0   
  11.  */    
  12. public class LdapOperUtils {    
  13.   
  14.     // 调用log4j的日志,用于输出    
  15.     private static Logger log = Logger.getLogger(LdapOperUtils.class.getName());    
  16.   
  17.     /**   
  18.      * 根据连接Env信息,取得Ldap DirContext   
  19.      * @param env 连接Env的连接信息   
  20.      * @return Ldap连接的DirContext   
  21.      * @throws BaseException   
  22.      */    
  23.     private static DirContext getLdapDirContext(Env env) throws BaseException {    
  24.         // 参数为空    
  25.         if (env == null) {    
  26.             String[] args = {    
  27.                             "env"};    
  28.             // 打印错误日志    
  29.             StringBuffer msglog = new StringBuffer(    
  30.                     "empty invoke parameter env NULL ");    
  31.             log.error(msglog.toString());    
  32.             throw new BaseException("error.common.parameter.empty", args);    
  33.   
  34.         }    
  35.         // 定义DirContext    
  36.         DirContext dirContext = null;    
  37.         // 从Ldap连接工厂中,取得Ldap连接    
  38.         dirContext = LdapConnectionFactory.getDirContext(env);    
  39.         return dirContext;    
  40.     }    
  41.   
  42.     /**   
  43.      * 根据在ldappool.properties文件定义的Ldap DirContext池名,取得Ldap DirContext   
  44.      * @param dirContextName Ldap DirContext池名   
  45.      * @return 返回操作Ldap 的 DirContext   
  46.      * @throws BaseException   
  47.      */    
  48.      private static DirContext getLdapDirContext(String dirContextName,Env env)    
  49.       throws BaseException {    
  50.             // 参数为空    
  51.             if (StringUtils.isEmpty(dirContextName)) {    
  52.                 String[] args = {    
  53.                                 "dirContextName"};    
  54.                 // 打印错误日志    
  55.                 StringBuffer msglog = new StringBuffer(    
  56.                         "empty invoke parameter dirContextName NULL ");    
  57.                 log.error(msglog.toString());    
  58.                 throw new BaseException("error.common.parameter.empty", args);    
  59.   
  60.             }    
  61.             // 定义DirContext    
  62.             DirContext dirContext = null;    
  63.             // 从Ldap连接工厂中,取得Ldap连接    
  64.             dirContext = LdapConnectionFactory.getDirContext(dirContextName,env);    
  65.   
  66.             return dirContext;    
  67.   
  68.         }    
  69.   
  70.         /**   
  71.          * 关闭LDAP连接   
  72.          * @param dirContext DirContext   
  73.          * @throws BaseException   
  74.          */    
  75.         public static void closeEnvLdapDirContext(DirContext dirContext)    
  76.         throws BaseException {    
  77.             // 关闭LDAP连接    
  78.             closeLdapDirContext(dirContext);    
  79.     }    
  80.   
  81.     /**   
  82.     * 关闭Ldap 的DirContext   
  83.     * @param dirContext 连接Ldap的DirContext   
  84.     * @throws BaseException   
  85.     */    
  86.    private static void closeLdapDirContext(DirContext dirContext) throws    
  87.            BaseException {    
  88.        // 如果参数为NULL    
  89.        if (dirContext == null) {    
  90.            String[] args = {    
  91.                            "dirContext"};    
  92.            // 打印错误日志    
  93.            StringBuffer msglog = new StringBuffer(    
  94.                    "empty invoke parameter conn NULL ");    
  95.            log.error(msglog.toString());    
  96.            throw new BaseException("error.common.parameter.empty", args);    
  97.        }    
  98.   
  99.        try {    
  100.            // 关闭    
  101.            dirContext.close();    
  102.        } catch (NamingException ex) {    
  103.            // 关闭不成功,再次关闭    
  104.            if (log.isDebugEnabled()) {    
  105.                log.debug("Not close DirContext " + ex);    
  106.            }    
  107.            // 记录日志    
  108.            log.error("Not close DirContext " + ex);    
  109.            ex.printStackTrace();    
  110.            try {    
  111.                //再次关闭    
  112.                dirContext.close();    
  113.            } catch (NamingException ex1) {    
  114.                // 再次关闭失败    
  115.                if (log.isDebugEnabled()) {    
  116.                    log.debug("Not again close DirContext " + ex);    
  117.                }    
  118.                // 记录日志    
  119.                log.error("Not again close DirContext " + ex);    
  120.                ex.printStackTrace();    
  121.                // 抛出异常    
  122.                throw new BaseException("error.common.dao.ldap.closedircontext"null);    
  123.            }    
  124.        }    
  125.    }    
  126.   
  127.   
  128.     /**   
  129.      * 构造函数私有,防止实例化   
  130.      */    
  131.     private LdapOperUtils() {    
  132.     }    
  133.   
  134.     /**   
  135.      * 在当前的Context 添加一个子Context   
  136.      * @param context 连接DirContext   
  137.      * @param cn 创建的子Context   
  138.      * @param attMap Context 的属性,Map 包含 List ,key = 属性名,   
  139.      * 当属性值为多值时,为list,为单值时,为String   
  140.      * @throws NamingException   
  141.      * @throws BaseException   
  142.      */    
  143.     public static void addContext(DirContext context, String cn, Map attMap) throws    
  144.             NamingException, BaseException {    
  145.   
  146.         // 参数为空    
  147.         if (context == null) {    
  148.             String[] args = {    
  149.                             "context"};    
  150.             // 打印错误日志    
  151.             StringBuffer msglog = new StringBuffer(    
  152.                     "empty invoke parameter context NULL ");    
  153.             log.error(msglog.toString());    
  154.             throw new BaseException("error.common.parameter.empty", args);    
  155.         }    
  156.   
  157.         // 参数为空    
  158.         if (StringUtils.isEmpty(cn)) {    
  159.             String[] args = {    
  160.                             "cn"};    
  161.             // 打印错误日志    
  162.             StringBuffer msglog = new StringBuffer(    
  163.                     "empty invoke parameter cn NULL ");    
  164.             log.error(msglog.toString());    
  165.             throw new BaseException("error.common.parameter.empty", args);    
  166.         }    
  167.   
  168.         // 参数为空    
  169.         if (attMap == null) {    
  170.             String[] args = {    
  171.                             "attMap"};    
  172.             // 打印错误日志    
  173.             StringBuffer msglog = new StringBuffer(    
  174.                     "empty invoke parameter attMap NULL ");    
  175.             log.error(msglog.toString());    
  176.             throw new BaseException("error.common.parameter.empty", args);    
  177.         }    
  178.   
  179.         // 为空,则退出    
  180.         if (attMap.isEmpty()) {    
  181.             return;    
  182.         }    
  183.   
  184.         // 取所有的属性key    
  185.         Set keySet = attMap.keySet();    
  186.         Iterator keyIterator = keySet.iterator();    
  187.         Attributes attrs = new BasicAttributes();    
  188.         // 迭代所有的属性key    
  189.         while (keyIterator.hasNext()) {    
  190.   
  191.             // 取下一个属性    
  192.             String key = (String) keyIterator.next();    
  193.             Attribute att = null;    
  194.             Object valueObj = attMap.get(key);    
  195.             // 判断属性类型    
  196.             if (valueObj instanceof String) {    
  197.                 // 为字符串,为单值属性    
  198.                 att = new BasicAttribute(key, valueObj);    
  199.             } else if (valueObj instanceof List) {    
  200.                 // 为List ,为多值属性    
  201.                 att = new BasicAttribute(key);    
  202.                 List valueList = (List) valueObj;    
  203.                 // 加入多值属性    
  204.                 for (int i = 0; i < valueList.size(); i++) {    
  205.                     att.add(valueList.get(i));    
  206.                 }    
  207.             } else {    
  208.                 // 其它类型,都加入,如字节类型 (密码)    
  209.                 att = new BasicAttribute(key,valueObj);    
  210.             }    
  211.             // 加入    
  212.             attrs.put(att);    
  213.         }    
  214.         // 创建子Context    
  215.         context.createSubcontext(cn, attrs);    
  216.         // context.close();    
  217.     }    
  218.   
  219.     /**   
  220.      * 在当前的Context 删除一个子Context   
  221.      * @param context 连接的DirContext   
  222.      * @param cn   要删除的Context的名称   
  223.      * @throws NamingException   
  224.      * @throws BaseException   
  225.      */    
  226.     public static void deleteContext(DirContext context, String cn) throws    
  227.             NamingException, BaseException {    
  228.   
  229.         // 参数为空    
  230.         if (context == null) {    
  231.             String[] args = {    
  232.                             "context"};    
  233.             // 打印错误日志    
  234.             StringBuffer msglog = new StringBuffer(    
  235.                     "empty invoke parameter context NULL ");    
  236.             log.error(msglog.toString());    
  237.             throw new BaseException("error.common.parameter.empty", args);    
  238.         }    
  239.   
  240.         // 参数为空    
  241.         if (StringUtils.isEmpty(cn)) {    
  242.             String[] args = {    
  243.                             "cn"};    
  244.             // 打印错误日志    
  245.             StringBuffer msglog = new StringBuffer(    
  246.                     "empty invoke parameter cn NULL ");    
  247.             log.error(msglog.toString());    
  248.             throw new BaseException("error.common.parameter.empty", args);    
  249.         }    
  250.   
  251.         // 删除一个子Context    
  252.         context.destroySubcontext(cn);    
  253.         //   context.close();    
  254.   
  255.     }    
  256.   
  257.     /**   
  258.      * 根据当前的连接DirContext 重命名Context   
  259.      * @param context 连接后的DirContext   
  260.      * @param cn    原Context的名称   
  261.      * @param newCn 新的Context名称   
  262.      * @throws NamingException   
  263.      * @throws BaseException   
  264.      */    
  265.     public static void reNameContext(DirContext context, String cn,    
  266.                                      String newCn) throws NamingException,    
  267.             BaseException {    
  268.   
  269.         // 参数为空    
  270.         if (context == null) {    
  271.             String[] args = {    
  272.                             "context"};    
  273.             // 打印错误日志    
  274.             StringBuffer msglog = new StringBuffer(    
  275.                     "empty invoke parameter context NULL ");    
  276.             log.error(msglog.toString());    
  277.             throw new BaseException("error.common.parameter.empty", args);    
  278.         }    
  279.   
  280.         // 参数为空    
  281.         if (StringUtils.isEmpty(cn)) {    
  282.             String[] args = {    
  283.                             "cn"};    
  284.             // 打印错误日志    
  285.             StringBuffer msglog = new StringBuffer(    
  286.                     "empty invoke parameter cn NULL ");    
  287.             log.error(msglog.toString());    
  288.             throw new BaseException("error.common.parameter.empty", args);    
  289.         }    
  290.   
  291.         // 参数为空    
  292.         if (context == null) {    
  293.             String[] args = {    
  294.                             "context"};    
  295.             // 打印错误日志    
  296.             StringBuffer msglog = new StringBuffer(    
  297.                     "empty invoke parameter context NULL ");    
  298.             log.error(msglog.toString());    
  299.             throw new BaseException("error.common.parameter.empty", args);    
  300.         }    
  301.   
  302.         // 参数为空    
  303.         if (StringUtils.isEmpty(newCn)) {    
  304.             String[] args = {    
  305.                             "newCn"};    
  306.             // 打印错误日志    
  307.             StringBuffer msglog = new StringBuffer(    
  308.                     "empty invoke parameter newCn NULL ");    
  309.             log.error(msglog.toString());    
  310.             throw new BaseException("error.common.parameter.empty", args);    
  311.         }    
  312.         context.rename(cn, newCn);    
  313.         // context.close();    
  314.     }    
  315.   
  316.     /**   
  317.      * 在当前连接的DirContext 指定的Context 添加一个 / 多个属性   
  318.      * @param context 连接的DirContext   
  319.      * @param cn     指定的Context   
  320.      * @param attMap Map 包含 List ,key为属性名称,   
  321.      * value 属性值, 当为多值时,存为List,当为单值时,为String类型   
  322.      * @throws BaseException   
  323.      * @throws NamingException   
  324.      */    
  325.     public static void addAttributes(DirContext context, String cn, Map attMap) throws    
  326.             BaseException, NamingException {    
  327.   
  328.         // 参数为空    
  329.         if (context == null) {    
  330.             String[] args = {    
  331.                             "context"};    
  332.             // 打印错误日志    
  333.             StringBuffer msglog = new StringBuffer(    
  334.                     "empty invoke parameter context NULL ");    
  335.             log.error(msglog.toString());    
  336.             throw new BaseException("error.common.parameter.empty", args);    
  337.         }    
  338.   
  339.         // 参数为空    
  340.         if (attMap == null) {    
  341.             String[] args = {    
  342.                             "attMap"};    
  343.             // 打印错误日志    
  344.             StringBuffer msglog = new StringBuffer(    
  345.                     "empty invoke parameter attMap NULL ");    
  346.             log.error(msglog.toString());    
  347.             throw new BaseException("error.common.parameter.empty", args);    
  348.         }    
  349.         // 参数为空    
  350.         if (StringUtils.isEmpty(cn)) {    
  351.             String[] args = {    
  352.                             "cn"};    
  353.             // 打印错误日志    
  354.             StringBuffer msglog = new StringBuffer(    
  355.                     "empty invoke parameter cn NULL ");    
  356.             log.error(msglog.toString());    
  357.             throw new BaseException("error.common.parameter.empty", args);    
  358.         }    
  359.   
  360.         // 为空,退出    
  361.         if (attMap.isEmpty()) {    
  362.             return;    
  363.         }    
  364.   
  365.         // 取所有的属性key    
  366.         Set keySet = attMap.keySet();    
  367.         Iterator keyIterator = keySet.iterator();    
  368.         Attributes attrs = new BasicAttributes();    
  369.         // 迭代所有的属性key    
  370.         while (keyIterator.hasNext()) {    
  371.   
  372.             // 取下一个属性    
  373.             String key = (String) keyIterator.next();    
  374.             Attribute att = null;    
  375.             Object valueObj = attMap.get(key);    
  376.             // 判断属性类型    
  377.             if (valueObj instanceof String) {    
  378.                 // 为字符串,为单值属性    
  379.                 att = new BasicAttribute(key, valueObj);    
  380.             } else if (valueObj instanceof List) {    
  381.                 // 为List ,为多值属性    
  382.                 att = new BasicAttribute(key);    
  383.                 List valueList = (List) valueObj;    
  384.                 // 加入多值属性    
  385.                 for (int i = 0; i < valueList.size(); i++) {    
  386.                     att.add(valueList.get(i));    
  387.                 }    
  388.             }    
  389.             // 加入    
  390.             attrs.put(att);    
  391.         }    
  392.   
  393.         context.modifyAttributes(cn, DirContext.ADD_ATTRIBUTE, attrs);    
  394.         // context.close();    
  395.     }    
  396.   
  397.     /**   
  398.      * 在当前的连接DirContext 删除 指定Context 下的 一个 / 多个属性   
  399.      * @param context 连接后的DirContext   
  400.      * @param cn 指定Context的名称   
  401.      * @param attList 包含要删除的属性的名称,为List类型   
  402.      * @throws BaseException   
  403.      * @throws NamingException   
  404.      */    
  405.     public static void deleteAttributes(DirContext context, String cn,    
  406.                                         List attList) throws BaseException,    
  407.             NamingException {    
  408.         // 参数为空    
  409.         if (context == null) {    
  410.             String[] args = {    
  411.                             "context"};    
  412.             // 打印错误日志    
  413.             StringBuffer msglog = new StringBuffer(    
  414.                     "empty invoke parameter context NULL ");    
  415.             log.error(msglog.toString());    
  416.             throw new BaseException("error.common.parameter.empty", args);    
  417.         }    
  418.   
  419.         // 参数为空    
  420.         if (attList == null) {    
  421.             String[] args = {    
  422.                             "attList"};    
  423.             // 打印错误日志    
  424.             StringBuffer msglog = new StringBuffer(    
  425.                     "empty invoke parameter attList NULL ");    
  426.             log.error(msglog.toString());    
  427.             throw new BaseException("error.common.parameter.empty", args);    
  428.         }    
  429.         // 参数为空    
  430.         if (StringUtils.isEmpty(cn)) {    
  431.             String[] args = {    
  432.                             "cn"};    
  433.             // 打印错误日志    
  434.             StringBuffer msglog = new StringBuffer(    
  435.                     "empty invoke parameter cn NULL ");    
  436.             log.error(msglog.toString());    

转载于:https://www.cnblogs.com/kungfupanda/archive/2011/02/21/1959931.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值