JNDI
(Java Naming and Directory Interface,Java命名和目录接口),为各种现有的命名和目录结构提供通用接口。
它由两部分组成:一是客户API,二是服务提供商接口(Service Provider Intergace)。应用程序通过客户API访问命名和目录服务,服务提供商接口用于供厂商创建命名和目录服务的JNDI实现。
API包结构
1 javax.naming 2 javax.naming.directory 3 javax.naming.event 4 javax.naming.ldap 5 javax.naming.spi
结构图
几个命名和目录服务的提供者:
- 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)文件系统服务提供者
- 其它服务提供者
DNS Service Provider为例进行示例代码演示
1 package com.joyen.learning.javase; 2 3 import java.util.Properties; 4 5 import javax.naming.Context; 6 import javax.naming.NameClassPair; 7 import javax.naming.NamingEnumeration; 8 import javax.naming.directory.Attribute; 9 import javax.naming.directory.Attributes; 10 import javax.naming.directory.DirContext; 11 import javax.naming.directory.InitialDirContext; 12 13 public class TestDNSJndi { 14 15 public static void main(String[] args) throws Exception { 16 Properties env = new Properties(); 17 env.put(Context.INITIAL_CONTEXT_FACTORY, 18 "com.sun.jndi.dns.DnsContextFactory"); 19 //此IP一定要为要访问的DNS服务器的IP,可通过网络设置查看 20 env.put(Context.PROVIDER_URL, "dns://109.52.64.210"); 21 DirContext ctx = new InitialDirContext(env); 22 System.out.println("a:" + ctx); 23 DirContext ctx1 = (DirContext) ctx.lookup("www.baidu.com"); 24 System.out.println("b:" + ctx1); 25 26 //从ctx.getAttributes("www.baidu.com")与ctx1.getAttributes("")结果一样 27 printAttributes("c:", ctx1.getAttributes("")); 28 printAttributes("d:", ctx.getAttributes("www.baidu.com")); 29 30 Attributes attrs1 = ctx.getAttributes("www.baidu.com", new String[] { "a" }); 31 Attributes attrs2 = ctx1.getAttributes("", new String[] { "a" }); 32 printAttributes("e:", attrs1); 33 printAttributes("f:", attrs2); 34 35 System.out.println("nameParse:"+ctx1.getNameInNamespace()); 36 //list,此方法会导致程序lock 37 //listEnumation("list:",ctx.list("")); 38 } 39 40 public static void printAttributes(String tag, Attributes attres) throws Exception { 41 for (NamingEnumeration<?> ae = attres.getAll(); ae.hasMore();) { 42 Attribute attr = (Attribute) ae.next(); 43 System.out.println(tag + "----------------------------------------------- attribute: " + attr.getID()); 44 /* Print each value */ 45 for (NamingEnumeration<?> e = attr.getAll(); e.hasMore();System.out.println("value: " + e.next())); 46 } 47 } 48 49 public static void listEnumation(String tag, NamingEnumeration<?> name) throws Exception { 50 for (; name.hasMore();) { 51 NameClassPair nameClass = (NameClassPair) name.next(); 52 System.out .println(tag + "----------------------------------------------- attribute: " 53 + nameClass.getName() + ":" + nameClass.getClassName()); 54 } 55 } 56 57 }
运行结果:
a:javax.naming.directory.InitialDirContext@4a13ccea b:com.sun.jndi.dns.DnsContext@51d77a8e c:----------------------------------------------- attribute: CNAME value: www.a.shifen.com. d:----------------------------------------------- attribute: CNAME value: www.a.shifen.com. e:----------------------------------------------- attribute: A value: 180.97.33.108 value: 180.97.33.107 f:----------------------------------------------- attribute: A value: 180.97.33.107 value: 180.97.33.108 nameParse:www.baidu.com.
参考:
http://www.cnblogs.com/chinafine/archive/2010/06/16/1759246.html
http://www.cnblogs.com/chinafine/archive/2010/06/16/1759250.html