目标:使用者只需要会使用List,Map 数据结构,将对LDAP的操作进行封装 类:主要有三个类 1 Env类 包含LDAP的连接信息 2 LdapConnectionFactory类 ldap连接工厂,提供初始化及获取ldap连接的方法 3 LdapOperUtils ldap的处理工具类,提供了各种操作ldap的方法。 ldap的处理工具类,提供了各种操作ldap的方法。 package com.common.ldapconnection; import java.util.List; import java.util.Vector; /** * * <p>功能描述:ldap的处理类,提供了各种操作ldap的方法。</p> * @author liaowufeng * @version 1.0 */ public class LdapOperUtils { // 调用log4j的日志,用于输出 private static Logger log = Logger.getLogger(LdapOperUtils.class.getName()); /** * 根据连接Env信息,取得Ldap DirContext * @param env 连接Env的连接信息 * @return Ldap连接的DirContext * @throws BaseException */ private static DirContext getLdapDirContext(Env env) throws BaseException { // 参数为空 if (env == null) { String[] args = { "env"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter env NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 定义DirContext DirContext dirContext = null; // 从Ldap连接工厂中,取得Ldap连接 dirContext = LdapConnectionFactory.getDirContext(env); return dirContext; } /** * 根据在ldappool.properties文件定义的Ldap DirContext池名,取得Ldap DirContext * @param dirContextName Ldap DirContext池名 * @return 返回操作Ldap 的 DirContext * @throws BaseException */ private static DirContext getLdapDirContext(String dirContextName,Env env) throws BaseException { // 参数为空 if (StringUtils.isEmpty(dirContextName)) { String[] args = { "dirContextName"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter dirContextName NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 定义DirContext DirContext dirContext = null; // 从Ldap连接工厂中,取得Ldap连接 dirContext = LdapConnectionFactory.getDirContext(dirContextName,env); return dirContext; } /** * 关闭LDAP连接 * @param dirContext DirContext * @throws BaseException */ public static void closeEnvLdapDirContext(DirContext dirContext) throws BaseException { // 关闭LDAP连接 closeLdapDirContext(dirContext); } /** * 关闭Ldap 的DirContext * @param dirContext 连接Ldap的DirContext * @throws BaseException */ private static void closeLdapDirContext(DirContext dirContext) throws BaseException { // 如果参数为NULL if (dirContext == null) { String[] args = { "dirContext"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter conn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } try { // 关闭 dirContext.close(); } catch (NamingException ex) { // 关闭不成功,再次关闭 if (log.isDebugEnabled()) { log.debug("Not close DirContext " + ex); } // 记录日志 log.error("Not close DirContext " + ex); ex.printStackTrace(); try { //再次关闭 dirContext.close(); } catch (NamingException ex1) { // 再次关闭失败 if (log.isDebugEnabled()) { log.debug("Not again close DirContext " + ex); } // 记录日志 log.error("Not again close DirCo[1] [2] [3] [4] 下一页
|
|