封装LDAP 增删改查 方法


忙了好久,有时间来整理下最近的知识了,首先封装LDAP的方法,让他可以已实体的方式来进行操作,思路类似于JPA 传入传出最好是实体,在实体里 使用map当作注解映射。

推荐阅读:

RedHat6下构建LDAP服务

Linux 使用pam_LDAP通过ad域认证

Zimbra故障——Unable to determine enabled services from LDAP

下面 先上2个代码

第一个封装一个baseDao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.Name;
import javax.naming.directory.SearchControls;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.control.PagedResultsCookie;
import org.springframework.ldap.control.PagedResultsDirContextProcessor;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.simple.ParameterizedContextMapper;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;
/**
 * Dao基层
 * @创建人 PengBo
 * @创建时间 2013-7-7  下午7:03:49
 */
@SuppressWarnings("unchecked")
public class BaseDAO< T extends BaseModel> {
    @Autowired
    private LdapTemplate ldapTemplate;
                                                                                                                                       
    /**
    * 创建实体
    * @param t
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:53:40
    */
    public void create(T t) {
          DirContextAdapter context = new DirContextAdapter(buildDn(t));
          mapToContext(t.getMap(), context);
          ldapTemplate.bind(context);
      }
    /**
    * 更新实体
    * @param t
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:53:56
    */
    public void update(T t) {
          DirContextOperations context = ldapTemplate.lookupContext(buildDn(t));
          mapToContext(t.getMap(), context);
          ldapTemplate.modifyAttributes(context);
      }
    /**
    * 创建更新时使用的属性存储构造器
    * @param map
    * @param context
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:54:16
    */
    private void mapToContext(Map<String, String> map, DirContextOperations context) {
            context.setAttributeValue("objectclass", "top");
        for(String mkey:map.keySet()){          //获得实体的属性和对应的值 加入属性构造器中
            if(map.get(mkey) != null)
            context.setAttributeValue(mkey, map.get(mkey));
        }
    }
    /**
    * 删除属性
    * @param t
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:54:46
    */
    public void delete(T t) {
          ldapTemplate.unbind(buildDn(t));
    }
    /**
    * 根据唯一DN进行查找
    * @param t
    * @return
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:55:02
    */
    public T findByPrimaryKey(final T t) {
        return (T) ldapTemplate.lookup(buildDn(t),  getContextMapper(t));
    }
                                                                                                                                       
    /**
    * 根据dn直接获取实体信息
    * @param t
    * @param dn
    * @return
    * @创建人 PengBo
    * @创建时间 2013-7-29  下午4:39:59
    */
    public T findByDN(final T t,String dn) {
        return (T) ldapTemplate.lookup(dn,  getContextMapper(t));
    }
    /**
    * 根据实体条件精确查找进行查找
    * @param t
    * @return  返回查找的集合
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:55:38
    */
    public List<T> findByEqualFilter( final T t) {
        return  ldapTemplate.search(buildDn(t), getEqualFilter(t).encode(), getContextMapper(t));
                                                                                                                                               
    }
                                                                                                                                       
    /**
    * 根据实体条件进行查找
    * @param t
    * @return  返回查找的集合
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:55:38
    */
    public List<T> findByFilter( final T t) {
        return  ldapTemplate.search(buildDn(t), getFilter(t).encode(), getContextMapper(t));
                                                                                                                                               
    }
                                                                                                                                             
    /**
    * 根据实体类型查找所有实体
    * @param t
    * @return  返回实体集合
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:56:13
    */
    public List<T> findAll(final T t) {
        return  ldapTemplate.search(buildDn(t),  getObjectclass(t).encode(), getContextMapper(t));
                                                                                                                                         
    }
    /**
    * 根据实体的分页属性进行查获早分页信息 
    * @param basePage
    * @return
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午6:57:00
    */
    public Page<T> getPages(Page<T> basePage,T t){
        int totalRow = findByFilter(t).size();
        basePage.setContent(getAllPageMap(null,t,(basePage.getPageSize()*basePage.getPage())));
        basePage.setTotalRow(totalRow);
        basePage.setTotalPage((totalRow+basePage.getPageSize()-1)/basePage.getPageSize());
        return basePage;
    }
    /**
    * 根据传入记录数查处所需要的信息
    * @param cookie
    * @param t
    * @param pageSize
    * @return
    * @创建人 PengBo
    * @创建时间 2013-7-10  上午9:23:46
    */
    private List<T> getAllPageMap(PagedResultsCookie cookie,T t,Integer pageSize) {
        PagedResultsDirContextProcessor  control = new PagedResultsDirContextProcessor (pageSize, cookie);
        SearchControls searchControls = new SearchControls();
          searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
          List<T> mList = ldapTemplate.search(buildDn(t),  getFilter(t).encode(),searchControls, getContextMapper(t), control);
          return mList;
        }
                                                                                                                                       
        /**
        * 查找全部信息所需要的filter检索方法
        * @param map
        * @return
        * @创建人 PengBo
        * @创建时间 2013-7-7  下午6:59:38
        */
        private AndFilter getObjectclass(T t){
            Map<String, String> map=t.getMap();
            AndFilter filter = new AndFilter();
            for(String mkey:map.keySet()){      //根据实体只获得他对应的objectclass的值
                if ("objectclass".equals(mkey)) {
                    filter.and(new EqualsFilter(mkey, (String) map.get(mkey)));
                }
            }
        return filter;
        }
                                                                                                                                       
    /**
    * 根据条件模糊查找的条件方法
    * @param t
    * @return
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午7:00:10
    */
      private AndFilter getFilter(T t) {
            AndFilter filter = new AndFilter();
            Map<String, String> map=t.getMap();
            for(String mkey:map.keySet()){
                if ("objectclass".equals(mkey)) {
                    filter.and(new EqualsFilter(mkey, (String) map.get(mkey)));
                }
                if(StringUtils.isNotBlank(map.get(mkey)) && !"objectclass".equals(mkey))
                filter.and(new WhitespaceWildcardsFilter(mkey, (String) map.get(mkey)));
            }
        return filter;
        }
      /**
        * 根据条件精确查找的条件方法
        * @param t
        * @return
        * @创建人 PengBo
        * @创建时间 2013-7-8  下午3:10:43
        */
      private AndFilter getEqualFilter(T t) {
            AndFilter filter = new AndFilter();
            Map<String, String> map=t.getMap();
            for(String mkey:map.keySet()){
                if ("objectclass".equals(mkey)) {
                    filter.and(new EqualsFilter(mkey, (String) map.get(mkey)));
                }
                if(StringUtils.isNotBlank(map.get(mkey)) && !"objectclass".equals(mkey))
                filter.and(new EqualsFilter(mkey, (String) map.get(mkey)));
            }
        return filter;
        }
        /**
        * 构造查询实体UUID方法
        * @param t
        * @return
        * @创建人 PengBo
        * @创建时间 2013-7-7  下午7:00:49
        */
      private Name buildDn(T t) {
          String a = t.getDN();
              DistinguishedName dn = new DistinguishedName(a);
                  if(StringUtils.isNotBlank(t.getUuid())){
                      dn.add("uid", t.getUuid());
                  }
              return dn;
          }
                                                                                                                                         
      /**
        * 构造查找组织的dn
        * @param t
        * @return
        * @创建人 PengBo
        * @创建时间 2013-7-16  上午9:45:57
        */
      public String findDn(T t) {
            Name dn= buildDn( t);
              return dn.toString();
          }
    /**
    * 查询获得实体属性构造器
    * @param t
    * @return
    * @创建人 PengBo
    * @创建时间 2013-7-7  下午7:01:12
    */
      private ContextMapper getContextMapper(final T t) {
              return new ParameterizedContextMapper<T>() {
                    @Override
                    public T mapFromContext(Object ctx) {
                        DirContextAdapter adapter = (DirContextAdapter) ctx;
                        T newT=null;
                        try {
                            newT = (T) t.getClass().newInstance();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        Map<String, String> map= t.getMap();
                        Map<String, String> smap=new HashMap<String, String>();
                        for(String mkey:map.keySet()){
                            if (!"objectclass".equals(mkey)) {
                                if(!"userPassword".equals(mkey)){
                                    if (StringUtils.isNotBlank(adapter.getStringAttribute(mkey))) {
                                        smap.put(mkey, adapter.getStringAttribute(mkey));
                                    }else {
                                        smap.put(mkey, null);
                                    }
                                }
                            }
                        }
                        newT.setMap(smap);
                        return newT;
                    }
              };
          }
}

接下来请看 

  • 1
  • 2
  • 下一页

相关内容