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 DirContext " + ex); ex.printStackTrace(); // 抛出异常 throw new BaseException("error.common.dao.ldap.closedircontext", null); } } } /** * 构造函数私有,防止实例化 */ private LdapOperUtils() { } /** * 在当前的Context 添加一个子Context * @param context 连接DirContext * @param cn 创建的子Context * @param attMap Context 的属性,Map 包含 List ,key = 属性名, * 当属性值为多值时,为list,为单值时,为String * @throws NamingException * @throws BaseException */ public static void addContext(DirContext context, String cn, Map attMap) throws NamingException, BaseException { // 参数为空 if (context == null) { String[] args = { "context"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter context NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (StringUtils.isEmpty(cn)) { String[] args = { "cn"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter cn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (attMap == null) { String[] args = { "attMap"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter attMap NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 为空,则退出 if (attMap.isEmpty()) { return; } // 取所有的属性key Set keySet = attMap.keySet(); Iterator keyIterator = keySet.iterator(); Attributes attrs = new BasicAttributes(); // 迭代所有的属性key while (keyIterator.hasNext()) { // 取下一个属性 String key = (String) keyIterator.next(); Attribute att = null; Object valueObj = attMap.get(key); // 判断属性类型 if (valueObj instanceof String) { // 为字符串,为单值属性 att = new BasicAttribute(key, valueObj); } else if (valueObj instanceof List) { // 为List ,为多值属性 att = new BasicAttribute(key); List valueList = (List) valueObj; // 加入多值属性 for (int i = 0; i < valueList.size(); i++) { att.add(valueList.get(i)); } } else { // 其它类型,都加入,如字节类型 (密码) att = new BasicAttribute(key,valueObj); } // 加入 attrs.put(att); } // 创建子Context context.createSubcontext(cn, attrs); // context.close(); } /** * 在当前的Context 删除一个子Context * @param context 连接的DirContext * @param cn 要删除的Context的名称 * @throws NamingException * @throws BaseException */ public static void deleteContext(DirContext context, String cn) throws NamingException, BaseException { // 参数为空 if (context == null) { String[] args = { "context"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter context NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (StringUtils.isEmpty(cn)) { String[] args = { "cn"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter cn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 删除一个子Context context.destroySubcontext(cn); // context.close(); } /** * 根据当前的连接DirContext 重命名Context * @param context 连接后的DirContext * @param cn 原Context的名称 * @param newCn 新的Context名称 * @throws NamingException * @throws BaseException */ public static void reNameContext(DirContext context, String cn, String newCn) throws NamingException, BaseException { // 参数为空 if (context == null) { String[] args = { "context"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter context NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (StringUtils.isEmpty(cn)) { String[] args = { "cn"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter cn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (context == null) { String[] args = { "context"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter context NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (StringUtils.isEmpty(newCn)) { String[] args = { "newCn"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter newCn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } context.rename(cn, newCn); // context.close(); } /** * 在当前连接的DirContext 指定的Context 添加一个 / 多个属性 * @param context 连接的DirContext * @param cn 指定的Context * @param attMap Map 包含 List ,key为属性名称, * value 属性值, 当为多值时,存为List,当为单值时,为String类型 * @throws BaseException * @throws NamingException */ public static void addAttributes(DirContext context, String cn, Map attMap) throws BaseException, NamingException { // 参数为空 if (context == null) { String[] args = { "context"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter context NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (attMap == null) { String[] args = { "attMap"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter attMap NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (StringUtils.isEmpty(cn)) { String[] args = { "cn"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter cn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 为空,退出 if (attMap.isEmpty()) { return; } // 取所有的属性key Set keySet = attMap.keySet(); Iterator keyIterator = keySet.iterator(); Attributes attrs = new BasicAttributes(); // 迭代所有的属性key while (keyIterator.hasNext()) { // 取下一个属性 String key = (String) keyIterator.next(); Attribute att = null; Object valueObj = attMap.get(key); // 判断属性类型 if (valueObj instanceof String) { // 为字符串,为单值属性 att = new BasicAttribute(key, valueObj); } else if (valueObj instanceof List) { // 为List ,为多值属性 att = new BasicAttribute(key); List valueList = (List) valueObj; // 加入多值属性 for (int i = 0; i < valueList.size(); i++) { att.add(valueList.get(i)); } } // 加入 attrs.put(att); } context.modifyAttributes(cn, DirContext.ADD_ATTRIBUTE, attrs); // context.close(); } /** * 在当前的连接DirContext 删除 指定Context 下的 一个 / 多个属性 * @param context 连接后的DirContext * @param cn 指定Context的名称 * @param attList 包含要删除的属性的名称,为List类型 * @throws BaseException * @throws NamingException */ public static void deleteAttributes(DirContext context, String cn, List attList) throws BaseException, NamingException { // 参数为空 if (context == null) { String[] args = { "context"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter context NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (attList == null) { String[] args = { "attList"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter attList NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 参数为空 if (StringUtils.isEmpty(cn)) { String[] args = { "cn"}; // 打印错误日志 StringBuffer msglog = new StringBuffer( "empty invoke parameter cn NULL "); log.error(msglog.toString()); throw new BaseException("error.common.parameter.empty", args); } // 为空,退出 if (attList.isEmpty()) { return; } Attributes attrs = new BasicAttributes(); for (int i = 0; i < attList.size(); i++) { Attribute att = null; att = new BasicAttribute((String) attList.get(i)); // 加入 attrs.put(att); } context.modifyAttributes(cn, DirContext.REMOVE_ATTRIBUTE, attrs); // context.close(); } }