JNDI的学习
JNDI全称 Java Naming and Directory Interface
JNDI是Java平台的一个标准扩展,提供了一组接口、类和关于命名空间的概念。如同其它很多Java技术一样,JDNI是provider-based的技术,暴露了一个API和一个服务供应接口(SPI)。这意味着任何基于名字的技术都能通过JNDI而提供服务,只要JNDI支持这项技术。JNDI目前所支持的技术包括LDAP、CORBA Common Object Service(COS)名字服务、RMI、NDS、DNS、Windows注册表等等。很多J2EE技术,包括EJB都依靠JNDI来组织和定位实体。
JDNI通过绑定的概念将对象和名称联系起来。在一个文件系统中,文件名被绑定给文件。在DNS中,一个IP地址绑定一个URL。在目录服务中,一个对象名被绑定给一个对象实体。
JNDI中的一组绑定作为上下文来引用。每个上下文暴露的一组操作是一致的。例如,每个上下文提供了一个查找操作,返回指定名字的相应对象。每个上下文都提供了绑定和撤除绑定名字到某个对象的操作。JNDI使用通用的方式来暴露命名空间,即使用分层上下文以及使用相同命名语法的子上下文。
jndi的用途:
1。你可以用jndi来得到object类的属性
如:Attribute attr =directory.getAttributes(personName).get("email");
String email = (String)attr.get();
2。你可以用jndi来搜索对象
如:foxes = directory.search("o=Wiz,c=US", "sn=Fox", controls);
查找谁的名字叫Fox在wiz部门的员工?
3。你可以用jndi通过naming/directory服务查询像printers和databases的对象
如:查询 Printer
Printer printer = (Printer)namespace.lookup(printerName);
printer.print(document);
4。你可以用jndi列表出命名空间的特殊级别的内容
如:
NamingEnumeration list = namespace.list("o=Widget, c=US");
while (list.hasMore()) {
NameClassPair entry = (NameClassPair)list.next();
display(entry.getName(), entry.getClassName());
}
以上根据jndi文档翻译的
地址:
http://java.sun.com/products/jndi/overview.html
tomcat数据库连接池配置中的
java:comp/env代表你的JVM的环境,comp=computer env=environment
JNDI全攻略之(一)
![](http://dev.csdn.net/Develop/article/28/35_files/jndiarch.jpg)
-
LDAP(Lightweight Directory Access Protocol)服务提供者
-
CORBA COS(Common Object Request Broker Architecture Common Object Services)命名服务提供者
-
RMI(Java Remote Method Invocation)注册服务提供者
-
DNS(Domain Name System)服务提供者.
-
FSSP(File System Service Provider)文件系统服务提供者
-
其它服务提供者
* Created on 2005-3-4
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.sily.jndi;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
* @author shizy
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class TestJbossJNDI {
/**
*
*/
public TestJbossJNDI() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) { try {
Properties env = new Properties();
env.load(new FileInputStream("jbossJndi.properties"));
env.list(System.out);
InitialContext ctx = new javax.naming.InitialContext(env);
System.out.println("Got context");
//create a subContext
ctx.createSubcontext("/sylilzy");
ctx.createSubcontext("sylilzy/sily");
ctx.rebind("sylilzy/sily/a", "I am sily a!");
ctx.rebind("sylilzy/sily/b", "I am sily b!");
//lookup context
Context ctx1=(Context)ctx.lookup("sylilzy");
Context ctx2=(Context)ctx1.lookup("/sylilzy/sily");
ctx2.bind("/sylilzy/g", "this is g");
//lookup binded object
Object o;
o=ctx1.lookup("sily/a");
System.out.println("get object from jndi:"+"get object from jndi:"+o);
//rename the object
ctx2.rename("/sylilzy/g", "g1");
o=ctx2.lookup("g1");
System.out.println("get object from jndi:"+"get object from jndi:"+o);
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Got context
get object from jndi:I am sily a!
get object from jndi:this is g
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
/*
* Created on 2005-3-1
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.sily.jndi;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
* @author shizy
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class JndiTest1 {
/**
*
*/
public JndiTest1() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
try {
Properties env = new Properties();
env.load(new FileInputStream("fileSystemService.properties"));
env.put(Context.PROVIDER_URL, "file:///c:/");
Context ctx = new InitialContext(env);
ctx.createSubcontext("sylilzy");
NamingEnumeration list = ctx.list("/");
while (list.hasMore()) {
NameClassPair nc = (NameClassPair) list.next();
System.out.println(nc);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
总结:
/*
* Created on 2005-3-4
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.sily.jndi;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
* @author shizy
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ListJbossJndi {
/**
*
*/
public ListJbossJndi() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
try {
Properties env = new Properties();
env.load(new FileInputStream("jbossJndi.properties"));
//env.list(System.out);
Context ctx = new InitialContext(env);
listCtx(ctx.lookup("sylilzy"));
}
catch (Exception e) {
e.printStackTrace();
}
}
static void listCtx(Object o){
if(!(o instanceof Context))log(":"+o);
else {
log("/n-----------------------------");
try {
Context ctx=(Context)o;
//log(ctx.getNameInNamespace()+"/:");
NamingEnumeration list=ctx.listBindings("");
while(list.hasMore()){
Binding bind=(Binding)list.next();
log("/n/"+ctx.getNameInNamespace()+"/"+bind.getName());
listCtx(bind.getObject());
}
log("/n-----------------------------");
}
catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static void log(Object o){
System.out.print(o);
}
}