以前经常在诸如 weblogic, jboss, tomcat 服务器上配置 JNDI 服务,代码也很简单。但还没有用过 LDAP 服务器. LDAP 用层次(树) 结构存储数据。 要引用树中的项目,就需要列出树中的节点名称。从所要项目开始,一直向树顶跟踪。树中每个节点都有 nodetype=value 形式的唯一 COM 域。 域组件的节点类型为 DC ,叶子节点类型为 O, LDAP 有属性的概念,同时也有类的概念。因此,可以依据属性或类检索。
Java 很是方便只要通过 JNDI 透过 LDAP 就可操作 LDAP, NIS, NDS, NT 等目录服务器.
例子1.
通过属性检索
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;
/**
* @author Jack.Wang
*
*/
public class NameSearch {
public static void main(String args[]) {
try {
InitialDirContext ctx = new InitialDirContext();
BasicAttributes searchAttrs = new BasicAttributes();
searchAttrs.put("sn", "Tippin");
NamingEnumeration objs = ctx.search(
"ldap://ldap.jack.com/o=test search, dc=test,dc=com",
searchAttrs);
while (objs.hasMoreElements()) {
SearchResult match = (SearchResult) objs.nextElement();
System.out.println(match.getName());
Attributes attrs = match.getAttributes();
NamingEnumeration e = attrs.getAll();
while (e.hasMoreElements()) {
Attribute attr = (Attribute) e.nextElement();
System.out.println(attr.getID());
for (int i = 0; i < attr.size(); i++) {
if (i > 0) {
System.out.println(",");
} else {
System.out.println(attr.get(i));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
例子2.
通过类名字检索
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
/**
* @author Jack.Wang
*
*/
public class AllSearch {
public static void main(String[] args) {
try {
InitialDirContext ctx = new InitialDirContext();
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration objs = ctx
.search(
"ldap://ldap.jack.com/o=test search, dc=test,dc=com",
"(objectClass=*)", searchControls);
while (objs.hasMoreElements()) {
SearchResult match = (SearchResult) objs.nextElement();
System.out.println(match.getName());
Attributes attrs = match.getAttributes();
NamingEnumeration e = attrs.getAll();
while (e.hasMoreElements()) {
Attribute attr = (Attribute) e.nextElement();
System.out.println(attr.getID());
for (int i = 0; i < attr.size(); i++) {
if (i > 0) {
System.out.println(",");
} else {
System.out.println(attr.get(i));
}
}
}
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BasicAttributes searchAttrs = new BasicAttributes();
}
}
例子3.
基于文件系统的 binding
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TestBinding {
private String serverName = "jackhost";
private int portNumber = 1433;
private String login = "jack";
private String password = "19810622";
private String databaseName = "jack";
private String filePath = "jdbc/jack";
public TestBinding() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(javax.naming.Context.PROVIDER_URL, "file:d:\\");
try {
Context ctx = new InitialContext(env);
AnalogyDB ds = new AnalogyDB();
ds.setServerName(serverName);
ds.setPortNumber(portNumber);
ds.setUser(login);
ds.setPassword(password);
ds.setDatabaseName(databaseName);
ds.setDescription("JDBC Datasource connection.");
ctx.bind(filePath, ds);
ctx.close();
} catch (Exception e) {
System.err.println("ERROR " + e.getMessage());
}
}
public static void main(String[] args) {
TestBinding test = new TestBinding();
}
}
例子3.
基于文件系统的 lookup
import java.sql.Connection;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
*
* @author Jack.Wang
*
*/
public class TestLookup {
public TestLookup() {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(javax.naming.Context.PROVIDER_URL, "file:d:\\");
Context ctx = new InitialContext(env);
AnalogyDB ds = (AnalogyDB) ctx
.lookup("jdbc/jack");
Connection connection = ds.getConnection();
System.out.println("Connection Established.");
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestLookup test = new TestLookup();
}
}
本博客为学习交流用,凡未注明引用的均为本人作品,转载请注明出处,如有版权问题请及时通知。由于博客时间仓促,错误之处敬请谅解,有任何意见可给我留言,愿共同学习进步。