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

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


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