关键字:JNDI,J2EE,Java,命名和目录接口,Java Naming and Directory Interface
摘要:
本文详细介绍了JNDI的架构与实现,JNDI的工作原理,并给出了具体代码,帮助读者更理解J2EE主要常用技术---JNDI.本文为系列文章的第一篇,其它相关文章会在近期推出。
图中所列的一些SPI可从http://java.sun.com/products/jndi/downloads/index.html下载.
下面通过一个示例程序来说明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(); } } }
结果输出如下:
/* * 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); } }