<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    天若有情

    到教堂懺悔,愿主安撫我罪惡的心靈......
    posts - 4, comments - 35, trackbacks - 0, articles - 24

    JNDI全攻略之(二)

    Posted on 2008-09-08 12:27 freedoom 閱讀(208) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA技術(shù)(好文保存)

    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)見(jiàn)本系列的第一篇

    總述:

    目錄(Directory)可看作是對(duì)命名(Naming)的一個(gè)擴(kuò)充,一個(gè)目錄對(duì)象不僅像命名一樣,而且還提供的對(duì)屬性(Attributes)的操作.由API文檔可知,javax.naming.directory.DirContext 類(lèi)擴(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一定要為要訪問(wèn)的DNS服務(wù)器的IP,可通過(guò)網(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)行通過(guò)。對(duì)于DNS Service Provider更詳細(xì)的文檔,大家可通過(guò)此URL下載:http://java.sun.com/products/jndi/downloads/index.html

    上例一個(gè)可能運(yùn)行結(jié)果如下:

    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")得到的結(jié)果與ctx1.getAttributes("")結(jié)果一樣,這便是目錄操作的兩種模式,這兩種模式取得的結(jié)果是一樣的,這點(diǎn)可以參考API文檔(http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/directory/DirContext.html):
    There are 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,其它詳細(xì)內(nèi)容請(qǐng)參考API文檔
    最后,代碼
    NamingEnumeration answer = ctx1.search("www.sina.com", matchAttrs);試圖對(duì)ctx1進(jìn)行屬性查找,但是拋出了異常,查看 DNS Service Provider 的文檔可知,DNS Service Provider 沒(méi)有提供對(duì)search方法的支持,大家可用其它的SP來(lái)測(cè)試此方法,如LDAP SP

    總結(jié):

    此例只是簡(jiǎn)單地演示的JNDI的目錄操作,對(duì)于目錄操作的其它高級(jí)主題如Search,Search Scope,Count Limit,Composite Names 等沒(méi)有詳細(xì)介紹,請(qǐng)參考其它相關(guān)文檔.


    作者簡(jiǎn)介:
        施祖陽(yáng),網(wǎng)名sylilzy,1979年生。
        2002年起從事軟件開(kāi)發(fā)工作,主要研究JAVA、Linux及相關(guān)技術(shù)。
        你可通過(guò)sylilzy@163.com與作者聯(lián)系。
     
    參考資料:

    主站蜘蛛池模板: 一级一级毛片免费播放| 成人午夜大片免费7777| 国产亚洲日韩在线a不卡| 亚洲高清视频在线播放| 中文字幕在亚洲第一在线| 成全影视免费观看大全二| 亚洲人成免费电影| 免费无码作爱视频| 国产无遮挡色视频免费观看性色| 亚洲欧洲专线一区| 麻豆狠色伊人亚洲综合网站 | 亚洲国产欧美日韩精品一区二区三区 | 亚洲色欲久久久综合网| 免费A级毛片无码久久版| 免费观看的av毛片的网站| 91精品视频免费| 5555在线播放免费播放| 久久国产精品免费看| 91视频免费网站| 久久成人18免费网站| 一级毛片人与动免费观看| 在线观看亚洲视频| 麻豆亚洲AV成人无码久久精品 | 免费国产黄网站在线观看动图| 亚洲人成自拍网站在线观看| 亚洲人成网站色在线观看| 亚洲a级片在线观看| 亚洲乱码卡一卡二卡三| 亚洲六月丁香六月婷婷蜜芽 | 思思99re66在线精品免费观看| 99re热免费精品视频观看| 日韩免费精品视频| 大地资源在线观看免费高清| 免费精品国偷自产在线在线| 免费av欧美国产在钱| 成人人观看的免费毛片| 日韩一区二区三区免费体验| 国产免费怕怕免费视频观看| 亚洲成?v人片天堂网无码| 亚洲一区二区三区免费| 亚洲国产一二三精品无码|