JNDI全攻略之(二)關(guān)鍵字:JNDI,J2EE,Java,命名和目錄接口,Java Naming and Directory Interface 摘要:本文詳細(xì)介紹了JNDI的目錄相關(guān)內(nèi)容,并以DNS Service Provider為例進(jìn)行了示例代碼的演示.本文為系列文章的第二篇,JNDI的基礎(chǔ)內(nèi)容請(qǐng)見本系列的第一篇 總述: 目錄(Directory)可看作是對(duì)命名(Naming)的一個(gè)擴(kuò)充,一個(gè)目錄對(duì)象不僅像命名一樣,而且還提供的 對(duì)屬性(Attributes)的操作.由API文檔可知,javax.naming.directory.DirContext 類擴(kuò)展自Context接口,同樣,javax.naming.directory.InitialDirContext也擴(kuò)展自 javax.naming.InitialContext,由此也可看出目錄操作完全支持命名操作。下面給出一個(gè)DNS Service Provider例子以演示有關(guān)目錄的一些操作:
* 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服務(wù)器的IP,可通過網(wǎng)絡(luò)設(shè)置查看 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("")結(jié)果一樣 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,此方法會(huì)導(dǎo)致程序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中可運(yùn)行通過。對(duì)于DNS Service Provider更詳細(xì)的文檔,大家可通過此URL下載:http://java./products/jndi/downloads/index.html 上例一個(gè)可能運(yùn)行結(jié)果如下: a:javax.naming.directory.InitialDirContext@1bf216a 另外,還有一點(diǎn)需要注意,從ctx.getAttributes()方法返回的Attributes中包含多個(gè)Attribute,每個(gè)Attribute包含多個(gè)values,其它詳細(xì)內(nèi)容請(qǐng)參考API文檔 總結(jié): 此例只是簡單地演示的JNDI的目錄操作,對(duì)于目錄操作的其它高級(jí)主題如Search,Search Scope,Count Limit,Composite Names 等沒有詳細(xì)介紹,請(qǐng)參考其它相關(guān)文檔. |
|