目标:使用者只需要会使用List,Map 数据结构,将对LDAP的操作进行封装 类:主要有三个类 1 Env类 包含LDAP的连接信息 2 LdapConnectionFactory类 ldap连接工厂,提供初始化及获取ldap连接的方法 3 LdapOperUtils ldap的处理工具类,提供了各种操作ldap的方法。 接 封装JNDI操作LDAP服务器的工具类(3) LdapOperUtils类的其余方法 /** * 在当前连接的DirContext 修改指定Context下的一个 或 多个属性 * @param context 连接的DirContext * @param cn 指定Context下的名字 * @param attMap 包含List key为属性名称,当属性为多值时 * value 为包含多值的List,为单值时,为包含单值的String类型 * @throws BaseException * @throws NamingException */ public static void modifyAttributes(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 List) { // 为List ,为多值属性 att = new BasicAttribute(key); List valueList = (List) valueObj; // 加入多值属性 for (int i = 0; i < valueList.size(); i++) { att.add(valueList.get(i)); } } else if (valueObj instanceof String) { att = new BasicAttribute(key, valueObj); } // 加入 attrs.put(att); } context.modifyAttributes(cn, DirContext.REPLACE_ATTRIBUTE, attrs); // context.close(); } // /** * 获取连接的DirContext中指定Context下的指定属性 * @param context 连接的DirContext * @param cn 指定Context的名称 * @param attNameList 要取的属性的名称List * @return Map包含List ,key 为属性的名称,当属性值为多值时,Value为List类型, * 否则,value 为String 类型 * @throws NamingException */ public static Map getAttributes(DirContext context, String cn, List attNameList) throws NamingException { Map attsMap = new HashMap(); Attributes results = null; List attValList = null; String attrId = null; if (attNameList == null) { results = context.getAttributes(cn); } else { if (!attNameList.isEmpty()) { // results = context.getAttributes(cn); String[] stTemp = new String[attNameList.size()]; /////////////////////////////////////////// 以下方法性能太低 //////////////////////////////// // for (int i = 0; i < attNameList.size(); i++) { // stTemp[i] = (String) attNameList.get(i); // } // results [1] [2] [3] [4] [5] 下一页
|
|