JNDI(2)

本文通过一个DNS ServiceProvider示例介绍了JNDI目录操作的基本用法,包括获取域名的IP地址、处理Attributes对象等内容,并分析了不同方法的使用场景。

目录(Directory)可看作是对命名(Naming)的一个扩充,一个目录对象不仅像命名一样,而且还提供的对属性(Attributes)的操作.由API文档可知,javax.naming.directory.DirContext 类扩展自Context接口,同样,javax.naming.directory.InitialDirContext也扩展自 javax.naming.InitialContext,由此也可看出目录操作完全支持命名操作。下面给出一个DNS Service Provider例子以演示有关目录的一些操作:

 

<!-- Code creation by HtmlSave Eclipse Plug-in (C) 2005 Morten Moeller / eclipse.moelleryoung.com -->  * 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服务器的IP,可通过网络设置查看 
        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("")结果一样 
        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,此方法会导致程序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中可运行通过。对于DNS Service Provider更详细的文档,大家可通过此URL下载:http://java.sun.com/products/jndi/downloads/index.html

上例一个可能运行结果如下:

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)


示例分析:
通过分析代码,我们可以看出我们从DNS服务器获取了指定域名的IP地址,而且可以看出www.sina.com有多个IP.
另外,可以看出从ctx.getAttributes("www.sina.com")得到的结果与ctx1.getAttributes("")结果一样,这便是目录操作的两种模式,这两种模式取得的结果是一样的,这点可以参考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...

另外,还有一点需要注意,从ctx.getAttributes()方法返回的Attributes中包含多个Attribute,每个Attribute包含多个values,其它详细内容请参考API文档
最后,代码NamingEnumeration answer = ctx1.search("www.sina.com", matchAttrs);试图对ctx1进行属性查找,但是抛出了异常,查看 DNS Service Provider 的文档可知,DNS Service Provider 没有提供对search方法的支持,大家可用其它的SP来测试此方法,如LDAP SP

总结:

此例只是简单地演示的JNDI的目录操作,对于目录操作的其它高级主题如Search,Search Scope,Count Limit,Composite Names 等没有详细介绍,请参考其它相关文档.

http://www.cnblogs.com/chinafine/archive/2010/06/16/1759250.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值