岁月联盟 · 中国技术网 本站主页 | 安全认证 | 用户服务 | 技术论坛
新闻快报 | 新手学堂 | 黑客特区 | 程序语言 | 数 据 库 | 防 火 墙 | 路由交换 | 系统集成 | 服 务 器 | 存储备份 | 考试认证
Windows | Linux | Java | 协议分析 | 问题解答 | 进程大全 | 网页设计 | 多 媒 体 | 图库资料 | 软件下载 | 站内下载
  您现在的位置: 岁月联盟 >> Java >> J2EE >> J2ee核心 >> Java正文
封装JNDI操作LDAP服务器的工具类(4)
作者:未知 文章来源:本站整理 点击数: 更新时间:2007-7-22 18:49:13

  目标:使用者只需要会使用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] 下一页


  • 上一个Java:
  • 下一个Java:
  •  
    热门文章
    推荐文章
    关于我们 | 发展历程 | 网站地图 | 广告服务 | 招贤纳士 | 战略合作 | 友情链接 | 著作声明 | 联系我们
    Copyright © 2002-2007 SYUE All rights reserved.
    E_mail:Admin@Syue.Com 皖ICP备05004589号
    未经授权禁止转载、摘编、复制或建立镜像.如有违反,追究法律责任.
    传世私服 传奇世界私服 天龙八部私服 bet365 传世私服 天龙八部私服 热血江湖私服 英雄合击传奇私服 热血江湖私服 bet365 bet365