- 浏览: 415733 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
Glogo:
楼主您好,我在试验您的代码的时候发现新开的三个子线程并没有一直 ...
java 高并发 ReentrantLock -- 可重入的锁 -
univasity:
最近发觉也被限速了,投诉一下就好一会~~ 看来明天又要和电信M ...
ADSL上网速度慢 都是帐号限速惹的祸 -
liuyuanhui0301:
java 高并发 ReentrantLock -- 可重入的锁 -
dang_java:
呵.很好的说明文档.
JXTA技术与应用发展 -
helloqidi:
谢谢,学习了
SQL中exists和in的区别
关键字:JNDI,J2EE,Java,命名和目录接口,Java Naming and Directory Interface
摘要:本文详细介绍了JNDI的目录相关内容,并以DNS Service Provider为例进行了示例代码的演示.本文为系列文章的第二篇,JNDI的基础内容请见本系列的第一篇
总述:
目录(Directory)可看作是对命名(Naming)的一个扩充,一个目录对象不仅像命名一样,而且还提供的对属性(Attributes)的操作.由API文档可知,javax.naming.directory.DirContext 类扩展自Context接口,同样,javax.naming.directory.InitialDirContext也扩展自 javax.naming.InitialContext,由此也可看出目录操作完全支持命名操作。下面给出一个DNS Service Provider例子以演示有关目录的一些操作:
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 等没有详细介绍,请参考其它相关文档.
发表评论
-
深入JVM锁机制
2011-09-19 01:00 944目前在Java中存在两种 ... -
java 正则表达式 非捕获组(特殊构造)Special Constructs(Non-Capturing)
2011-06-20 23:15 1599针对Java API文档中的正则表达式关于特殊构造(非捕获组) ... -
Java文件映射[mmap]揭秘
2011-06-08 20:10 1095前言 相信现在 ... -
原创 java的Mmap二三事
2011-06-08 19:46 1158转自 :http://blog.csdn.net/kabini ... -
java 安全沙箱模型详解
2011-04-18 16:29 907起到第一道安全保障作 ... -
非阻塞算法-ReentrantLock代码剖析之ReentrantLock.lock
2011-04-15 13:59 1048ReentrantLock是java.util.concurr ... -
CyclicBarrier与CountDownLatch、栅栏与计数器
2011-04-15 10:39 1432在多线程设计中,我猜常常会遇到线程间相互等待以及某个线程等待1 ... -
Java KeyStore
2011-04-13 17:17 1427简介Java自带的keytool工具是个密钥和证书管理工具。它 ... -
Security Managers and the JavaTM 2 SDK
2011-04-12 13:37 740The original Link : http://do ... -
Something about SecurityManager
2011-04-12 13:33 751The Java Security was made up o ... -
Using the Java SecurityManager with Tomcat
2011-04-12 13:30 963Why use a SecurityManager? The ... -
Java安全管理器(Security Manager)(
2011-04-11 14:54 857转载自: http://blog.sina.com.cn/s/ ... -
Java对象的强、软、弱和虚引用(1)
2011-04-01 08:44 774本文介绍Java对象的强 ... -
Java对象的强引用、软引用、弱引用和虚引用
2011-04-01 08:39 873在JDK1.2以前的版本中, ... -
java 高并发 ReentrantLock -- 可重入的锁
2011-03-30 08:09 2289ReentrantLock -- 可重入的锁 可重入锁指 ... -
线程运行栈信息的获取
2011-03-24 17:23 1273线程运行栈信息的获取 ... -
java序列化——Serializable
2011-03-15 23:17 1033类通过实现 java.io.Serializable 接口 ... -
Java aio(异步网络IO)初探
2011-03-11 16:34 1528按照《Unix网络编程》的 ... -
JAVA NIO 简介
2011-03-11 13:38 10811. 基本 概念 IO 是主存和外部设备 ( 硬盘、终 ... -
[字节码系列]ObjectWeb ASM构建Method Monitor
2011-03-08 18:08 833在前面的篇章中,我们看到Java Instru ...
相关推荐
简单我tomcat5.0中的JNDI应用
JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试...
jndi,就是命名服务(n:naming)和目录服务(d:directory). 命名服务:把对象映射到方便记忆的名字的机制.可以通过域名系统dns来理解,例如我们访问百度,只要输入www.baidu.com而不是百度的IP地址. 目录服务:其实也是一种...
JNDI服务利用工具 RMI/LDAP,支持部分场景回显、内存shell,高版本JDK场景下利用等,fastjson rce命令执行,log4j rce命令执行 漏洞检测辅助工具
jndi配置,jndi配置jndi配置jndi配置jndi配置jndi配置jndi配置jndi配置
jndi入门学习资料,介绍jndi基本原理,安装和使用,基本配置
jboss配置MySql的JNDI
JNDI java操作示例,JNDI java操作示例
hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...
jndi.jndi.jndi.jndi.jndi.jndi.jndi.jndi.jndi.
在JAVA编程中对JNDI的支持.是一个开放的源码.
java asm jndi_JNDI-Injection-Exploit,用于log4j2漏洞验证 可执行程序为jar包,在命令行中运行以下命令: $ java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar [-C] [command] [-A] [address] 其中: -C ...
自定义jndi数据源factory类,用于解密jndi中的加密数据,解密方式为DES,具体可根据实际需求修改。
tomcat的JNDI配置详细介绍 介绍详细,思路清晰
Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI
TOMCAT JNDI配置
JNDI配置原理详解 JNDI配置原理详解.doc
java的JNDI 技术介绍及应用 以及整理的代码。很适合学习与交流用
04_JNDI.pdf。。。。。