服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

封装jndi操作ldap服务器的工具类(3)


  目标:使用者只需要会使用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 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();
  }
  }
  
  由于类 ldapoperutils 太长,jr限制,其它方法见我的下一篇文章
  
  封装jndi操作ldap服务器的工具类(4),或向我要代码.

扫描关注微信公众号