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) |