Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251955
  • 博文数量: 65
  • 博客积分: 2026
  • 博客等级: 大尉
  • 技术积分: 695
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-12 14:34
文章分类

全部博文(65)

文章存档

2012年(1)

2011年(1)

2010年(1)

2009年(2)

2008年(7)

2007年(6)

2006年(47)

我的朋友

分类: Java

2006-04-24 09:19:17

public class DomainManager {
 //LDAP全局变量信息初始化
 //定义服务的提供者
 private String INITCTX = "";
 //LDAP服务器主机
 private String MY_HOST = "";
 //LDAP根目录
 private String MY_SEARCHBASE = "";
 //验证方式
 private String MY_AUTH = "";
 //验证用户
 private String MY_DN = "";
 //验证用户
 private String MY_PW = "";
 //验证密码
 private String MY_FILTER = "";
 public static ThreadLocal myLdapConnection = new ThreadLocal();
 /**
  * 默认构造器
  *
  * @param
  * @return
  */
 public DomainManager() {
  Connection ldapConn = Connection.getInstance();
  INITCTX = ldapConn.getLdapInitctx();
  MY_HOST = ldapConn.getLdapHost();
  MY_SEARCHBASE = ldapConn.getLdapSearchbase();
  MY_DN = ldapConn.getLdapDn();
  MY_PW = ldapConn.getLdapPw();
  MY_AUTH = ldapConn.getLdapAuth();
 }
 /**
  * 创建机构
  *
  * @param String
  *            path 机构路径
  * @param String
  *            machineryID 机构名称ID
  * @param String
  *            machineryOrder 机构名称排序号
  * @param String
  *            machineryName 机构名称
  * @param String
  *            MachineryPath 所属路径
  * @return boolean 创建是否成功
  * @return 此类是遇到已经有的类就跳过
  */
 public boolean createMachinery(String path, String machineryID,
   String machineryOrder, String machineryName, String mdomainName,
   String MachineryPath) {
  DirContext dctx = null;
  try {
   dctx = (DirContext) myLdapConnection.get();
   if (dctx == null) {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
    env.put(Context.PROVIDER_URL, MY_HOST);
    env.put(Context.SECURITY_AUTHENTICATION, MY_AUTH);
    env.put(Context.SECURITY_PRINCIPAL, MY_DN);
    env.put(Context.SECURITY_CREDENTIALS, MY_PW);
    dctx = new InitialDirContext(env);
    myLdapConnection.set(dctx);
   }
   //检查机构是否已注册
   Attributes SearchAttrs = new BasicAttributes(true);
   SearchAttrs.put(new BasicAttribute("ou", machineryID));
   NamingEnumeration result = dctx.search(path, SearchAttrs);
   //判断机构信息是否全,如果为空转为空值
   if (machineryName == null) {
    machineryName = "";
   } else if (MachineryPath == null) {
    MachineryPath = "";
   }
   if (result.hasMore()) {
    //机构已注册
    return false;
   } else {
    //机构没有注册
    Attributes attrs = new BasicAttributes(true);
    //写机构ID
    Attribute id = new BasicAttribute("ou");
    id.add(machineryID);
    //写机构ID
    Attribute order = new BasicAttribute("l");
    order.add(machineryOrder);
    //写机构名
    Attribute name = new BasicAttribute("description");
    name.add(machineryName);
    //写上级机构ID
    Attribute uppath = new BasicAttribute("registeredAddress");
    uppath.add(MachineryPath);
    Attribute domain = new BasicAttribute("st");
    domain.add(mdomainName);
    Attribute objectclass = new BasicAttribute("objectclass");
    objectclass.add("organizationalunit");
    attrs.put(id);
    attrs.put(order);
    attrs.put(name);
    attrs.put(uppath);
    attrs.put(domain);
    attrs.put(objectclass);
    String AddDn = "ou=" + machineryID + "," + path;
    dctx.createSubcontext(AddDn, attrs);
    return true;
   }
  } catch (NamingException e) {
   e.printStackTrace();
   System.out.println("DomainManager.createMachinery() fail...,"
     + e.toString());
   return false;
  }
 }
}
阅读(1434) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~