關(guān)鍵字:JNDI,J2EE,Java,命名和目錄接口,Java Naming and Directory Interface
摘要:本文詳細介紹了JNDI的目錄相關(guān)內容,并以DNS Service Provider為例進(jìn)行了示例代碼的演示.本文為系列文章的第二篇,JNDI的基礎內容請見(jiàn)本系列的第一篇
總述:
目錄(Directory)可看作是對命名(Naming)的一個(gè)擴充,一個(gè)目錄對象不僅像命名一樣,而且還提供的對屬性(Attributes)的操作.由API文檔可知,javax.naming.directory.DirContext類(lèi)擴展自Context接口,同樣,javax.naming.directory.InitialDirContext也擴展自javax.naming.InitialContext,由此也可看出目錄操作完全支持命名操作。下面給出一個(gè)DNS ServiceProvider例子以演示有關(guān)目錄的一些操作:
上例中,在jdk1.4中可運行通過(guò)。對于DNS Service Provider更詳細的文檔,大家可通過(guò)此URL下載:http://java.sun.com/products/jndi/downloads/index.html
上例一個(gè)可能運行結果如下:
a:javax.naming.directory.InitialDirContext@1bf216a
b:com.sun.jndi.dns.DnsContext@3a6727
c:-----------------------------------------------
attribute: CNAME
value: us.sina.com.cn.
d:-----------------------------------------------
attribute: CNAME
value: us.sina.com.cn.
e:-----------------------------------------------
attribute: A
value: 218.30.66.67
value: 218.30.66.68
value: 218.30.66.69
value: 218.30.66.70
value: 218.30.66.71
value: 218.30.66.56
value: 218.30.66.57
value: 218.30.66.58
value: 218.30.66.59
value: 218.30.66.60
value: 218.30.66.61
value: 218.30.66.62
value: 218.30.66.63
value: 218.30.66.64
value: 218.30.66.65
value: 218.30.66.66
f:-----------------------------------------------
attribute: A
value: 220.181.28.42
g:-----------------------------------------------
attribute: A
value: 218.30.66.68
value: 218.30.66.69
value: 218.30.66.70
value: 218.30.66.71
value: 218.30.66.56
value: 218.30.66.57
value: 218.30.66.58
value: 218.30.66.59
value: 218.30.66.60
value: 218.30.66.61
value: 218.30.66.62
value: 218.30.66.63
value: 218.30.66.64
value: 218.30.66.65
value: 218.30.66.66
value: 218.30.66.67
attrs4:-----------------------------------------------
attribute: A
value: 220.181.27.5
nameParse:www.sina.com.
Exception in thread "main" javax.naming.OperationNotSupportedException
at 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)
示例分析:
通過(guò)分析代碼,我們可以看出我們從DNS服務(wù)器獲取了指定域名的IP地址,而且可以看出www.sina.com有多個(gè)IP.
另外,可以看出從ctx.getAttributes("www.sina.com")得到的結果與ctx1.getAttributes("")結果一樣,這便是目錄操作的兩種模式,這兩種模式取得的結果是一樣的,這點(diǎn)可以參考API文檔(http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/directory/DirContext.html):
Thereare 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...
另外,還有一點(diǎn)需要注意,從ctx.getAttributes()方法返回的Attributes中包含多個(gè)Attribute,每個(gè)Attribute包含多個(gè)values,其它詳細內容請參考API文檔
最后,代碼NamingEnumeration answer = ctx1.search("www.sina.com", matchAttrs);試圖對ctx1進(jìn)行屬性查找,但是拋出了異常,查看 DNS Service Provider 的文檔可知,DNS Service Provider 沒(méi)有提供對search方法的支持,大家可用其它的SP來(lái)測試此方法,如LDAP SP
總結:
此例只是簡(jiǎn)單地演示的JNDI的目錄操作,對于目錄操作的其它高級主題如Search,Search Scope,Count Limit,Composite Names 等沒(méi)有詳細介紹,請參考其它相關(guān)文檔.
聯(lián)系客服