国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

封裝JNDI操作LDAP服務器的工具類(3)

2019-11-18 12:24:19
字體:
來源:轉載
供稿:網友

  目標:使用者只需要會使用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),或向我要代碼.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屯昌县| 赫章县| 大埔区| 思南县| 长垣县| 长泰县| 天津市| 大关县| 定州市| 富平县| 南部县| 丁青县| 靖宇县| 广元市| 鄂托克前旗| 白河县| 从江县| 正定县| 搜索| 惠安县| 华池县| 娱乐| 大埔县| 恩施市| 沐川县| 固原市| 婺源县| 永川市| 砀山县| 黔江区| 普定县| 高陵县| 广南县| 金昌市| 金湖县| 塔城市| 玉林市| 治多县| 宕昌县| 平谷区| 栾川县|