JNDI全攻略之(一)
關(guān)鍵字:JNDI,J2EE,Java,命名和目錄接口,Java Naming and Directory Interface
摘要:
本文詳細(xì)介紹了JNDI的架構(gòu)與實(shí)現(xiàn),JNDI的工作原理,并給出了具體代碼,幫助讀者更理解J2EE主要常用技術(shù)---JNDI.本文為系列文章的第一篇,其它相關(guān)文章會(huì)在近期推出。
名詞解釋
jndi是Java 命名和目錄接口(Java Naming and Directory Interface,JNDI)的簡稱.從一開始就一直是 Java 2 平臺(tái)企業(yè)版(JEE)的核心技術(shù)之一。在JMS,JMail,JDBC,EJB等技術(shù)中,就大量應(yīng)用的這種技術(shù)。
為什么會(huì)有jndi
jndi誕生的理由似乎很簡單。隨著分布式應(yīng)用的發(fā)展,遠(yuǎn)程訪問對(duì)象訪問成為常用的方法。雖然說通過Socket等編程手段仍然可實(shí)現(xiàn)遠(yuǎn)程通信,但按照模式的理論來說,仍是有其局限性的。RMI技術(shù),RMI-IIOP技術(shù)的產(chǎn)生,使遠(yuǎn)程對(duì)象的查找成為了技術(shù)焦點(diǎn)。JNDI技術(shù)就應(yīng)運(yùn)而生。JNDI技術(shù)產(chǎn)生后,就可方便的查找遠(yuǎn)程或是本地對(duì)象。
JNDI的架構(gòu)與實(shí)現(xiàn)

JNDI的架構(gòu)與JDBC的架構(gòu)非常類似.JNDI架構(gòu)提供了一組標(biāo)準(zhǔn)命名系統(tǒng)的API,這些API在JDK1.3之前是作為一個(gè)單獨(dú)的擴(kuò)展包jndi.jar(通過這個(gè)地址下載),這個(gè)基礎(chǔ)API構(gòu)建在與SPI之上。這個(gè)API提供如下五個(gè)包
在應(yīng)用程序中,我們實(shí)際上只使到用以上幾個(gè)包的中類.具體調(diào)用類及通信過程對(duì)用戶來說是透明的.
JNDI API提供了訪問不同JNDI服務(wù)的一個(gè)標(biāo)準(zhǔn)的統(tǒng)一的實(shí)現(xiàn),其具體實(shí)現(xiàn)可由不同的 Service Provider來完成。前面講的為第一層JNDI API層.
最下層為JNDI SPI API及其具體實(shí)現(xiàn)。
圖中所列的一些SPI可從http://java.sun.com/products/jndi/downloads/index.html下載.

它
- LDAP(Lightweight Directory Access Protocol)服務(wù)提供者
- CORBA COS(Common Object Request Broker Architecture Common Object Services)命名服務(wù)提供者
- RMI(Java Remote Method Invocation)注冊服務(wù)提供者
- DNS(Domain Name System)服務(wù)提供者.
- FSSP(File System Service Provider)文件系統(tǒng)服務(wù)提供者
- 其它服務(wù)提供者
中間層為命名管理層。其功能應(yīng)該由JNDI SPI來完成。上層為JNDI API,這個(gè)API包在Java 2 SDK 1.3及以上的版本中已經(jīng)包括。
前面講解的只是作為應(yīng)用程序客戶端的架構(gòu)實(shí)現(xiàn),其服務(wù)端是由SPI對(duì)應(yīng)的公司/廠商來實(shí)現(xiàn)的,我們只需將服務(wù)端的相關(guān)參數(shù)傳給JNDI API就可以了,具體調(diào)用過程由SPI來完成.
JNDI工作原理
下面通過一個(gè)示例程序來說明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相關(guān)參數(shù),包括初始上下文工廠,服務(wù)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(); } } }
結(jié)果輸出如下:
-- listing properties --
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.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
程序中jbossJndi.properties文件的內(nèi)容為:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
注意:要正確運(yùn)行示例程序,請啟動(dòng)jboss,并將jboss的jbossall-client.jar文件放入classpath中。
上述示例程序在jboss服務(wù)器的jndi樹上建立了幾個(gè)上下文,并bind了幾對(duì)象,大家可通過附錄中的代碼或其它工具查看
查看結(jié)果為:
-----------------------------
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
上述程序中,我們的代碼只涉及到了jndi API,其它細(xì)節(jié)如初始化jboss jndi的初始上下文,建立網(wǎng)絡(luò)連接,與服務(wù)器通信,對(duì)我們來說都是透明的,另外,我們將jboss jndi的spi包中的類名作為參數(shù)傳入了程序中,要訪問一個(gè)遠(yuǎn)程對(duì)象,我們所做的就這么多。
下面,再提供一個(gè)例子,與上例不同,我們不需要jboss,我們使用sun的FSSP(File System Service Provider)文件系統(tǒng)服務(wù)提供者.注意在這個(gè)例子中要使用到前面所說的File System Service Provider for the Java Naming and Directory InterfaceTM (JNDI)相關(guān)類(下載)。
/*
* 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();
}
}
}
上例中fileSystemService.properties文件的內(nèi)容為:java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
這個(gè)例子較簡單,運(yùn)行后,它會(huì)列出C:\下所有的文件和目錄,另外你會(huì)發(fā)現(xiàn)有一個(gè)新目錄被創(chuàng)建了.本例不同于上例,它并不需要服務(wù)端,因?yàn)樗L問的是文件系統(tǒng).有關(guān)幫助可查閱包內(nèi)的相關(guān)文檔。
通過對(duì)比這兩個(gè)例子,應(yīng)該JNDI的工作原理有了一個(gè)大致的了解。
總結(jié):
jndi技術(shù)體現(xiàn)了分布式應(yīng)用的優(yōu)點(diǎn),同進(jìn)它的產(chǎn)生也為分布式對(duì)象提供了統(tǒng)一的訪問接口。由于篇幅所限,對(duì)目錄的操作本文未作介紹,其它內(nèi)容將在接下來的系列中討論。要對(duì)JNDI技術(shù)作全面的了解,請參閱參考資料.要對(duì)于JNDI技術(shù)深入學(xué)習(xí),仍有許多地方值得進(jìn)一步了解,例如EJB容器所使用的JNDI所提供的對(duì)象就有 Local和Remote之分,對(duì)于Local Object,對(duì)于不同的JVM是不可訪問的;對(duì)于遠(yuǎn)程對(duì)象的訪問,還涉及到Java安全機(jī)制。
附錄:
查看jboss jndi內(nèi)容的代碼:
//----------------------------------------
/*
* 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);
}
}
作者簡介:
施祖陽,網(wǎng)名sylilzy,1979年生。
2002年起從事軟件開發(fā)工作,主要研究為JAVA、Linux及相關(guān)技術(shù)。
你可通過sylilzy@163.com與作者聯(lián)系。
參考資料: