jndi详解
来源:互联网 发布:游戏宣传片制作软件 编辑:程序博客网 时间:2024/05/29 18:45
摘要:
本文详细介绍了JNDI的架构与实现,JNDI的工作原理,并给出了具体代码,帮助读者更理解J2EE主要常用技术---JNDI.本文为系列文章的第一篇,其它相关文章会在近期推出。
JNDI的架构与JDBC的架构非常类似.JNDI架构提供了一组标准命名系统的API,这些API在JDK1.3之前是作为一个单独的扩展包jndi.jar(通过这个地址下载),这个基础API构建在与SPI之上。这个API提供如下五个包
- javax.naming
- javax.naming.directory
- javax.naming.event
- javax.naming.ldap
- javax.naming.spi
图中所列的一些SPI可从http://java.sun.com/products/jndi/downloads/index.html下载.
它包括了几个增强和下面的命名/目录服务提供者:
- LDAP(Lightweight Directory Access Protocol)服务提供者
- CORBA COS(Common Object Request Broker Architecture Common Object Services)命名服务提供者
- RMI(Java Remote Method Invocation)注册服务提供者
- DNS(Domain Name System)服务提供者.
- FSSP(File System Service Provider)文件系统服务提供者
- 其它服务提供者
下面通过一个示例程序来说明JNDI工作原理(代码为自解释).
/* * Created on 2005-3-4 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package com.sily.jndi; import java.io.FileInputStream; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; /** * @author shizy * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class TestJbossJNDI { /** * */ public TestJbossJNDI() { super(); // TODO Auto-generated constructor stub } public static void main(String[] args) { try { Properties env = new Properties(); //载入jboss的SPI相关参数,包括初始上下文工厂,服务URL,等等 env.load(new FileInputStream("jbossJndi.properties")); env.list(System.out); //通过JNDI api 初始化上下文 InitialContext ctx = new javax.naming.InitialContext(env); System.out.println("Got context"); //create a subContext ctx.createSubcontext("/sylilzy"); ctx.createSubcontext("sylilzy/sily"); //rebind a object ctx.rebind("sylilzy/sily/a", "I am sily a!"); ctx.rebind("sylilzy/sily/b", "I am sily b!"); //lookup context Context ctx1=(Context)ctx.lookup("sylilzy"); Context ctx2=(Context)ctx1.lookup("/sylilzy/sily"); ctx2.bind("/sylilzy/g", "this is g"); //lookup binded object Object o; o=ctx1.lookup("sily/a"); System.out.println("get object from jndi:"+o); //rename the object ctx2.rename("/sylilzy/g", "g1"); o=ctx2.lookup("g1"); System.out.println("get object from jndi:"+o); } catch (Exception e) { e.printStackTrace(); } } }
结果输出如下:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Got context
get object from jndi:I am sily a!
get object from jndi:this is g
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
/* * Created on 2005-3-1 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package com.sily.jndi; import java.io.FileInputStream; import java.util.Properties; import javax.naming.*; import javax.naming.Context; import javax.naming.InitialContext; /** * @author shizy * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class JndiTest1 { /** * */ public JndiTest1() { super(); // TODO Auto-generated constructor stub } public static void main(String[] args) { try { Properties env = new Properties(); env.load(new FileInputStream("fileSystemService.properties")); env.put(Context.PROVIDER_URL, "file:///c:/"); Context ctx = new InitialContext(env); ctx.createSubcontext("sylilzy"); NamingEnumeration list = ctx.list("/"); while (list.hasMore()) { NameClassPair nc = (NameClassPair) list.next(); System.out.println(nc); } } catch (Exception e) { e.printStackTrace(); } } }
总结:
/* * Created on 2005-3-4 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package com.sily.jndi; import java.io.FileInputStream; import java.util.Properties; import javax.naming.*; import javax.naming.Context; import javax.naming.InitialContext; /** * @author shizy * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class ListJbossJndi { /** * */ public ListJbossJndi() { super(); // TODO Auto-generated constructor stub } public static void main(String[] args) { try { Properties env = new Properties(); env.load(new FileInputStream("jbossJndi.properties")); //env.list(System.out); Context ctx = new InitialContext(env); listCtx(ctx.lookup("sylilzy")); } catch (Exception e) { e.printStackTrace(); } } static void listCtx(Object o){ if(!(o instanceof Context))log(":"+o); else { log("\n-----------------------------"); try { Context ctx=(Context)o; //log(ctx.getNameInNamespace()+"/:"); NamingEnumeration list=ctx.listBindings(""); while(list.hasMore()){ Binding bind=(Binding)list.next(); log("\n/"+ctx.getNameInNamespace()+"/"+bind.getName()); listCtx(bind.getObject()); } log("\n-----------------------------"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static void log(Object o){ System.out.print(o); } }
目录(Directory)可看作是对命名(Naming)的一个扩充,一个目录对象不仅像命名一样,而且还提供的对属性(Attributes)的操作.由API文档可知,javax.naming.directory.DirContext 类扩展自Context接口,同样,javax.naming.directory.InitialDirContext也扩展自 javax.naming.InitialContext,由此也可看出目录操作完全支持命名操作。下面给出一个DNS Service Provider例子以演示有关目录的一些操作:
<!-- Code creation by HtmlSave Eclipse Plug-in (C) 2005 Morten Moeller / eclipse.moelleryoung.com --> * Created on 2005-11-17 package com.sily.jndi; import java.util.Properties; /** * Description: * * @author shizy * @version 1.0 date:2005-11-17 */ public class TestDNSJndi { public static void main(String[] args) throws Exception { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); //此IP一定要为要访问的DNS服务器的IP,可通过网络设置查看 env.put(Context.PROVIDER_URL, "dns://10.17.45.239"); DirContext ctx = new InitialDirContext(env); System.out.println("a:" + ctx); DirContext ctx1 = (DirContext) ctx.lookup("www.sina.com"); System.out.println("b:" + ctx1); printAttributes("c:", ctx1.getAttributes("")); //从ctx.getAttributes("www.sina.com")与ctx1.getAttributes("")结果一样 printAttributes("d:", ctx.getAttributes("www.sina.com")); Attributes attrs1 = ctx.getAttributes("www.sina.com", new String[] { "a" }); Attributes attrs2 = ctx.getAttributes("www.163.com", new String[] { "a" }); Attributes attrs3 = ctx1.getAttributes("", new String[] { "a" }); Attributes attrs4 = ctx.getAttributes("www.baidu.com", new String[] { "a" }); printAttributes("e:", attrs1); printAttributes("f:", attrs2); printAttributes("g:", attrs3); printAttributes("attrs4:", attrs4); System.out.println("nameParse:"+ctx1.getNameInNamespace()); //list,此方法会导致程序lock //listEnumation("list:",ctx.list("")); //----------------------search Attributes matchAttrs = new BasicAttributes(true); matchAttrs.put(new BasicAttribute("a", "61.172.201.13")); NamingEnumeration answer = ctx1.search("www.sina.com", matchAttrs); printNamingEnumeration("search :", answer); } public static void printAttributes(String tag, Attributes attres) throws Exception { for (NamingEnumeration ae = attres.getAll(); ae.hasMore();) { Attribute attr = (Attribute) ae.next(); System.out .println(tag + "-----------------------------------------------\nattribute: " + attr.getID()); /* Print each value */ for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out .println("value: " + e.next())) ; } } public static void listEnumation(String tag, NamingEnumeration name) throws Exception { for (; name.hasMore();) { NameClassPair nameClass = (NameClassPair) name.next(); System.out .println(tag + "-----------------------------------------------\nattribute: " + nameClass.getName() + ":" + nameClass.getClassName()); } } public static void printNamingEnumeration(String tag, NamingEnumeration e) throws Exception { for (; e.hasMore();) { Attribute attr = (Attribute) e.next(); System.out .println(tag + "-----------------------------------------------\nattribute: " + attr.getID()); /* Print each value */ for (NamingEnumeration ve = attr.getAll(); ve.hasMore(); System.out .println("value: " + ve.next())) ; } } }
上例中,在jdk1.4中可运行通过。对于DNS Service Provider更详细的文档,大家可通过此URL下载:http://java.sun.com/products/jndi/downloads/index.html
上例一个可能运行结果如下:
a:javax.naming.directory.InitialDirContext@1bf216ab:com.sun.jndi.dns.DnsContext@3a6727c:-----------------------------------------------attribute: CNAMEvalue: us.sina.com.cn.d:-----------------------------------------------attribute: CNAMEvalue: us.sina.com.cn.e:-----------------------------------------------attribute: Avalue: 218.30.66.67value: 218.30.66.68value: 218.30.66.69value: 218.30.66.70value: 218.30.66.71value: 218.30.66.56value: 218.30.66.57value: 218.30.66.58value: 218.30.66.59value: 218.30.66.60value: 218.30.66.61value: 218.30.66.62value: 218.30.66.63value: 218.30.66.64value: 218.30.66.65value: 218.30.66.66f:-----------------------------------------------attribute: Avalue: 220.181.28.42g:-----------------------------------------------attribute: Avalue: 218.30.66.68value: 218.30.66.69value: 218.30.66.70value: 218.30.66.71value: 218.30.66.56value: 218.30.66.57value: 218.30.66.58value: 218.30.66.59value: 218.30.66.60value: 218.30.66.61value: 218.30.66.62value: 218.30.66.63value: 218.30.66.64value: 218.30.66.65value: 218.30.66.66value: 218.30.66.67attrs4:-----------------------------------------------attribute: Avalue: 220.181.27.5nameParse:www.sina.com.Exception in thread "main" javax.naming.OperationNotSupportedExceptionat com.sun.jndi.dns.DnsContext.c_search(Unknown Source)at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(Unknown Source)at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)at com.sily.jndi.TestDNSJndi.main(TestDNSJndi.java:57)示例分析:通过分析代码,我们可以看出我们从DNS服务器获取了指定域名的IP地址,而且可以看出www.sina.com有多个IP.另外,可以看出从ctx.getAttributes("www.sina.com")得到的结果与ctx1.getAttributes("")结果一样,这便是目录操作的两种模式,这两种模式取得的结果是一样的,这点可以参考API文档(http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/directory/DirContext.html):There are two basic models of what attributes should be associated with. First, attributes may be directly associated with a DirContext object. In this model, an attribute operation on the named object is roughly...
另外,还有一点需要注意,从ctx.getAttributes()方法返回的Attributes中包含多个Attribute,每个Attribute包含多个values,其它详细内容请参考API文档最后,代码NamingEnumeration answer = ctx1.search("www.sina.com", matchAttrs);试图对ctx1进行属性查找,但是抛出了异常,查看 DNS Service Provider 的文档可知,DNS Service Provider 没有提供对search方法的支持,大家可用其它的SP来测试此方法,如LDAP SP
总结:
此例只是简单地演示的JNDI的目录操作,对于目录操作的其它高级主题如Search,Search Scope,Count Limit,Composite Names 等没有详细介绍,请参考其它相关文档.
- jndi 详解
- JNDI详解
- jndi详解
- JNDI详解
- JNDI详解
- JNDI详解
- JNDI详解
- JNDI详解
- JNDI介绍(详解,什么是JNDI)
- JNDI介绍(详解,什么是JNDI)
- JNDI介绍(详解,什么是JNDI)
- JNDI介绍(详解,什么是JNDI)
- JNDI介绍(详解,什么是JNDI)
- JNDI配置原理详解
- JNDI配置原理详解
- JNDI配置原理详解
- JNDI,JTA,JMS 详解
- JNDI配置原理详解
- 在iOS4中使用代码块
- C++中 string 的使用
- [bfs] poj 1753 Flip Game# bfs + 位存储
- 计算机网络层(3)--------《计算机网络-自顶向下方法》
- ACM水题-数据选取(AC解题报告)
- jndi详解
- Android面试之---谈谈你对Android NDK的理解
- JSP内置对象之--response登录验证
- Android 下载安装APK
- mfc绘图 结构与函数大全
- 返回局部对象的引用和返回局部对象的指针
- Spring Security学习笔记
- Android面试之----谈谈你对Java中Native关键字的认识!
- 初识Socket之构造接受包的简易程序(C语言实习)