`

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

    博客分类:
  • LDAP
阅读更多

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

Java代码 复制代码
  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());    
  437.             <span
    分享到:
    评论

相关推荐

    封装JNDI的LDAP服务器的工具类

    本篇内容将深入探讨如何封装JNDI操作,以便更方便地与LDAP服务器进行交互。 首先,我们要理解封装的目标:简化 LDAP 操作,使得用户只需熟悉 List 和 Map 数据结构,而无需直接处理复杂的 JNDI API。为此,我们创建...

    封装jndi操作ldap服务器的工具类

    【封装JNDI操作LDAP】 在Java开发中,直接使用JNDI API操作LDAP可能会涉及很多底层细节,包括连接设置、安全配置等,这增加了代码的复杂性和出错的可能性。因此,为了简化这一过程,通常会创建一个工具类来封装这些...

    使用Java操作LDAP案例

    在IT行业中,LDAP(Lightweight Directory Access Protocol)是一种用于访问和管理分布式目录服务的标准协议,常用于企业身份验证...在实际项目中,为了提高代码的可维护性和安全性,通常会封装成专门的服务或工具类。

    LDAP以及JAVA方法操作详解.doc

    1. **连接LDAP服务器**:使用`InitialDirContext`类建立与LDAP服务器的连接。首先,创建一个环境属性对象,配置服务器地址、端口、用户名和密码,然后通过`new InitialDirContext(environment)`初始化上下文。 2. *...

    JAVA LDAP

    为了简化这些操作,你可以创建一个名为`LdapUtil`的工具类,封装连接、查询、添加、修改和删除的方法。这样可以使代码更易于理解和维护。 总结,Java LDAP操作涉及连接LDAP服务器、执行查询、添加、修改和删除条目...

    LDAP学习资料

    - "ldapUtil"可能是这个压缩包中包含的Java工具类,它封装了对LDAP的各种操作,便于开发人员调用。 - 这个工具类可能包含连接LDAP服务器的方法,执行查询、增加、删除和修改操作的函数,以及异常处理逻辑。 学习...

    java操作Ldap,支持建立开启状态的用户,支持修改密码,放入eclipse测试即用

    在Java中,我们通常使用JNDI(Java Naming and Directory Interface)来与LDAP服务器交互。JNDI是一个API,它为多种命名和目录服务提供了统一的接口,包括LDAP。在本项目中,可能包含了一个或多个Java类,这些类封装...

    JNDI-Injection-Exploit-1.0-SNAPSHOT-all.zip

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的API,它允许程序查找和操作各种命名和目录服务,如 Lightweight Directory Access Protocol (LDAP)、Java Database Connectivity ...

    spring ldap 1.3.0下载

    3. **强大的查询支持**:通过LdapTemplate类,开发者可以使用XPath或LDIF(LDAP Data Interchange Format)进行复杂查询,同时提供了安全性和性能优化。 4. **事务管理**:Spring LDAP 支持LDAP操作的事务管理,使得...

    com.sun.jndi.providerutil.jar

    `providerutil`组件则是这些提供者的辅助工具,它包含了一些通用的工具类和方法,帮助实现JNDI服务提供者的基本功能,比如上下文管理、属性处理等。 二、`com.sun.jndi.providerutil.jar`的功能 1. **提供者注册**...

    Ldap增删改查

    在实际项目中,这些通常会被封装到配置类或工具类中,以提高代码的可重用性和安全性。 在压缩包文件"LdapCRUDTest"中,可能包含了实现上述功能的测试代码。这些代码会展示如何在Java中使用JNDI进行具体的LDAP操作,...

    spring-ldap-1.3.0

    它封装了JNDI(Java Naming and Directory Interface)API,使得开发者能够以更简洁的方式进行目录操作。 2. **ContextSource**:用于配置与 LDAP 服务器的连接。它可以设置URL、基础DN(Distinguished Name)、...

    jndi 反射 耦合

    更重要的是,反射操作往往破坏了封装性,可能导致安全风险,因此在使用反射时需要特别小心,遵循最小权限原则。 最后,耦合性是衡量软件模块之间相互依赖程度的一个度量。低耦合意味着模块之间的关系简单,易于理解...

    Java-J2EE Job Interview

    - 类与对象:类的概念、对象的创建过程、封装、继承、多态的基本原理。 - 构造器:构造器的作用、构造器重载、构造器链调用。 - 继承:继承的概念、super关键字的使用、继承中的构造器调用规则。 - 多态:多态性...

    基于java网上书城

    1. **Java基础**:Java是该项目的基础语言,学习者需要掌握Java面向对象编程的基本概念,包括类、对象、封装、继承、多态等。此外,对异常处理、输入输出流、集合框架(如ArrayList、HashMap)的理解也是必要的。 2...

    Java/J2EE Job Interview Companion

    ### Java/J2EE核心概念与关键领域 ... - **并发工具类**:熟悉ConcurrentHashMap等并发容器的使用方法。 通过以上知识点的学习,读者可以全面掌握Java/J2EE的核心概念和技术细节,为即将到来的技术面试做好充分准备。

    JDirectory-开源

    总的来说,`JDirectory`是Java开发者处理LDAP目录服务的强大工具,它通过简化JNDI的使用,使得开发人员能更专注于应用逻辑,提高了开发效率和代码质量。了解并掌握`JDirectory`,对于从事相关领域开发的人员来说,...

    Core Java, Volume II--Advanced Features (9th Edition).pdf

    5. JNDI/LDAP目录集成:本书还会介绍如何利用Java命名和目录接口(JNDI)以及轻量级目录访问协议(LDAP)进行企业级服务的发现和集成。 6. 国际化:随着现代软件越来越注重全球市场,本节将说明如何利用Java的国际...

    JAVA J2EE JOB INTERVIEW COMPANION PREVIEW

    这部分内容涵盖了变量、数据类型、运算符、控制结构、类与对象、封装、继承、多态等基本概念。 #### Swing Swing是Java中用于构建图形用户界面(GUI)的一套组件库。了解Swing可以帮助开发者创建美观且功能丰富的...

Global site tag (gtag.js) - Google Analytics