JNDI是java访问名字和目录服务的接口,与JDBC一样,它仅仅只定义了一套服务接口,实现由具体厂商提供。作为j2ee核心组件之一,它为应用程序查找其他程序组件和资源提供了统一的接口,其中最常见的用途就是数据源的配置、EJB名字查找、JMS相关配置等。JNDI的架构如下图。在JDK1.3中,已经包含了JNDI,它分成五个包。每个包提供的功能: 
-          javax.naming,包含访问命名服务的类和接口定义。
-          javax.naming.directory,包含访问目录服务的类和接口定义。
-          javax.naming.ldap,为ldapv3提供的扩展操作提供支持。
-          javax.naming.event,为访问命名和目录服务时的事件通知提供支持。
-          javax.naming.spi,为服务提供商提供的接口,一般用户不会涉及。

基本概念 
       了解名字服务和目录服务的相关概念,有助于更好的使用JNDI。
Naming service
       名字服务定义了如何将名字与对象关联,并通过名字如何找到对象的方法。典型的例子如:DNS将域名与IP关联,文件系统将文件名与文件相关联。在名字服务中,主要的概念:
-          名字(Names),在名字系统中实际对象的代号,如文件名,域名等,它会被用来查找关联的对象。不同的系统中会有不同的命名规范,如文件系统采用“\”来表示层级,而DNS则使用“.”。
-          绑定(Bindings),名字和实际对象的关联。
-          引用和地址(References and Addresses),当对象不能直接被存储在名字系统时,就必须使用引用,通过引用找到实际的对象。在系统中,保存的引用的内容被称为地址。引用还有另一个用处:在名字系统中,缺少象关系数据库中外键的概念。通过使用引用,可以作为外键的一个取代办法。
-          上下文(Context),它是一个名字-对象集合,提供了与名字系统交互的主要操作,如查找、绑定、去绑定。子上下文(subcontext)与它的关系类似文件系统中目录和子目录的关系,子上下文被包含在一个上下文中,通过父上下文中的一个名字与子上下文关联。
-          名字系统和名字空间(Naming Systems and Namespaces),名字系统是相同类型的上下文的集合,它提供名字服务;名字空间,是名字系统中的名字集合,如文件系统的文件名和目录。 
Directory service
       目录服务是名字服务的扩展,它除了关联名字和对象,还允许对象包含属性。目录系统通常以层次结构组织数据。在目录服务中的主要概念:
-          属性(Attributes),它属于目录对象,它是(名字,值)对,属性可以有多个值。
-          目录和目录服务(Directories and Directory Services),目录是目录对象的集合;目录服务则提供与目录相关的服务,创建、删除和修改存放在目录中的对象的属性。
-          查找和查找过滤器(Searches and Search Filters),获取目录对象的操作就是查找;过滤器是类似查找条件的对象。
基本使用 
²        注册JNDI提供者
在使用JNDI之前,需要先获取JNDI的提供者,并在系统注册它。与JNDI相关的系统属性在javax.naming.Context中定义,常用的属性:
-          java.naming.factory.initial,服务提供者用来创建InitialContext的类名。
-          java.naming.provider.url,用来配置InitialContext的初始url
-          java.naming.factory.object,用来创建name-to-object映射的类,用于NameClassPair和References。
-          java.naming.factory.state,用来创建jndi state的类
对于目录服务,由于一般需要安全设置,还通常使用:
-          java.naming.security.authentication,安全类型,三个值:none,simple或strong。
-          java.naming.security.principal,认证信息。
-          java.naming.security.credentials,证书信息。
-          java.naming.security.protocol,安全协议名。
使用System.setProperty注册,如果程序不显示说明,那么java会在classpath内查找jdni.properties文件来完成注册。jdni.properties例子:
java.naming.factory.initial=com.codeline.db.MockInitialContextFactory 
²        连接服务
注册之后,就可以实施服务连接了。对于名字服务由InitialContext开始,目录服务则使用InitialDirContext。它们分别实现了Context和DirContext,这两个接口分别对应名字服务和目录服务的接口,也是JNDI中最重要的两个接口。 
-          连接名字服务:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY," 
com.sun.jndi.fscontext.FSContextFactory"); 
InitialContext ctx = new InitialContext(); 
-          连接目录服务:
   Hashtable env = new Hashtable(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
    env.put(Context.PROVIDER_URL, "ldap://myserver.com/"); 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    //登录ldap server需要的用户名 
    env.put(Context.SECURITY_PRINCIPAL, "ldapuser"); 
    //登录ldap server需要的密码 
    env.put(Context.SECURITY_CREDENTIALS, "mypassword"); 
InitialDirContext ctx = new InitialDirContext(env); 
-          多服务提供者:如果应用包含多个服务提供者,在连接时略有不同。以名字服务为例:
Hashtable env = new Hashtable(); 
env.put(Context.INITIAL_CONTEXT_FACTORY, 
"com.sun.jndi.rmi.registry.RegistryContextFactory"); 
env.put(Context.PROVIDER_URL, "rmi://myserver.com:1099"); 
//使用不同的构造函数 
InitialContext ctx = new InitialContext(env); 
²        查找对象
不论名字服务还是目录服务,都是使用lookup来查找对象的。除了可以使用String作为参数之外,lookup还可使用Name接口作为参数。
Greeter greeter = (Greeter)ctx.lookup("SayHello"); 
如果想要获得上下文中所有的对象名字,就使用lis返回NameClassPair列表。NameClassPair包含对象名字和对象类名。如果想要获得实际的对象实例列表,就使用listBindings,它返回Binding列表。Binding是NameClassPair的子类,它包含对象的实例。 
-          list 
NamingEnumeration list = ctx.list("awt"); 
while (list.hasMore()) { 
    NameClassPair nc = (NameClassPair)list.next(); 
    System.out.println(nc); 
} 
-          listBindings 
NamingEnumeration bindings = ctx.listBindings("awt"); 
while (bindings.hasMore()) { 
    Binding bd = (Binding)bindings.next(); 
    System.out.println(bd.getName() + ": " + bd.getObject()); 
} 
²        对象绑定
-          使用bind添加绑定
Fruit fruit = new Fruit("orange");
ctx.bind("favorite", fruit); 
-          使用rebind修改绑定
Fruit fruit = new Fruit("lemon");
ctx.rebind("favorite", fruit); 
-          使用unbind去除绑定。
ctx.unbind("favorite"); 
²        对象改名
使用rename可以给一个在上下文中的对象改名
ctx.rename("report.txt", "old_report.txt"); 
²        获取属性
属性相关的接口是Attribute和Attributes,它们都在javax.naming.directory包内。通过DirContext的getAttributes方法就可以获得对象的属性集合,然后使用Attributes的get方法获得对应的属性,最后通过Attribute的get方法就可以获得属性值。
String dn = "uid=me, dc=mycompany, dc=com, ou=customer, o=ExampleApp"; 
Context user = (Context)ctx.lookup(dn); 
//获得所有属性 
Attributes attrs = user.getAttributes(""); 
Attribute test= attrs .get("test"); 
Object testValue= test.get(); 
上例中获得的是user的所有属性,在实际使用过程中,考虑网络带宽的影响,可以设置获取要获取的属性列表: 
String reqd_attrs = new String[] { "surname", "initials","title", "rfc822mailalias"}; 
Attributes attrs = user.getAttributes("", reqd_attrs); 
²        查找和过滤
使用search方法完成。
   public DirContext[] findUser(String initials,String surname,String country,String phone) { 
        //构造条件 
        BasicAttributes search_attrs = new BasicAttributes(); 
        search_attrs.put("initials", initials); 
        search_attrs.put("sn", surname); 
        search_attrs.put("c", country); 
        if(phone != null) 
          search_attrs.put("phonenumber", phone); 
 
        NamingEnumeration results = initial_ctx.search("ou=Customer,o=ExampleApp", search_attrs); 
        LinkedList found = new LinkedList(); 
        while(results.hasMore()) { 
            SearchResults sr = (SearchResults)results.next(); 
            String name = sr.getName(); 
            Object ctx = sr.getObject(); 
            if((ctx == null) || !(ctx instanceof DirContext)) 
                found.add(initial_ctx.lookup(name)); 
            else 
                found.add(ctx); 
        } 
 
        DirContext[] ret_val = new DirContext[found.size()]; 
        found.toArray(ret_val); 
        return ret_val; 
   } 
DirContext接口主要过滤方式: 
1.使用过滤字符串 
String reqd_attrs = new String[] { "cn", "uid","rfc822mailalias" }; 
NamingEnumeration results = initial_ctx.search("ou=Customer, o=ExampleApp",search_attrs,reqd_attrs); 
2.使用SearchControls,获得更多的控制 
SearchControls ctrls = new SearchControls(); 
ctrls.setCountLimit(20); 
ctrls.setTimeLimit(5000); 
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
NamingEnumeration results = initial_ctx.search("cat=books,ou=Products, 
o=ExampleApp","title=*Java*",ctrls); 
²        修改属性
使用DirContext和InitialDirContext的modifyAttributes方法完成。所谓的修改过程,实际就是先构造要修改的属性列表,然后使用上述方法提交。对于属性包含多个值时,需要把属性的不修改的值也要包含,否则服务器会认为那些值不再需要而删除它们。
public void updateAddress(String dn,String address, String country, String phone) { 
        BasicAttributes mod_attrs = new BasicAttributes(); 
        if(address != null) 
            mod_attrs.put("address", address); 
        if(country != null) 
            mod_attrs.put("c", country); 
        if(phone != null) 
            mod_attrs.put("phonenumber", phone); 
        if(mod_attrs.size() != 0) 
            initial_ctx.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, mod_attrs); 
} 
使用ModificationItem,也可一次进行多个不同的修改操作: 
ModificationItem[] mod_items = new ModificationItems[2]; 
Attribute email = new BasicAttribute("rfc822mailalias", new_email); 
ModificationItem email_mod = new ModificationItem(DirContext.ADD_ATTRIBUTE, email); 
Attribute addr = new BasicAttribute("address", address); 
ModificationItem addr_mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, addr); 
mod_items[0] = email_mod; 
mod_items[1] = addr_mod; 
initial_ctx.modifyAttributes(dn, mod_items); 
²        创建上下文
使用createSubcontext方法完成。
BasicAttributes attrs = new BasicAttributes(); 
attrs.put("initials", initials); 
attrs.put("sn", surname); 
attrs.put("rfc822mailalias", email); 
if(address != null) 
    attrs.put("address", address); 
if(country != null) 
    attrs.put("c", country); 
if(phone != null) 
    attrs.put("phonenumber", phone); 
initial_ctx.createSubcontext(dn, attrs); 
²        删除上下文 
使用destroySubcontext方法完成。
initial_ctx.destroySubcontext(dn); 
	posted on 2005-10-25 17:23 
my java 阅读(675) 
评论(0)  编辑  收藏  所属分类: 
JNDI