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有一些收获,写下来与大家分享.高手可以帮忙纠正和指点,在此谢过!
一:
几个概念,Context,entry/object,filter.Attribute
Context:上下文,我的理解是相当与文件系统的中的目录(JNDI的Naming Service是可以用操作系统的文件系统的,哈哈).
entry/object:一个节点,相当与文件系统中的目录或文件.
filter:查询/过滤条件是一个字符串表达式如:(&(objectClass=top)(cn=*))查询出objectClass属性为top,cn属性为所有情况的entry.
Attribute:entry/object的属性可以理解成JAVA对象的属性,不同的是这个属性可以多次赋值.
package study.jndi; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class Lookup{ public static void main(String[] args){ Hashtable env = new Hashtable(); String name = "C:/aaa.txt"; try{ env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory"); //use the file system as service provider Context ctx = new InitialContext(env); Object obj = ctx.lookup(name); System.out.println("the name " + name + " is bound to object:" + obj); }catch(Exception ex){ ex.printStackTrace(); } } }
package study.jndi; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.InitialDirContext; public class GetAttr{ public static final String ENTRY_NAME = "cn=bsj,ou=xc,ou=t,c=com"; public static void main(String[] args){ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL,"ldap://127.0.0.1:10389/"); //login the ldap server try{ DirContext ctx = new InitialDirContext(env); Attributes allAttr = ctx.getAttributes(ENTRY_NAME); //get *a* object's all attributes for(NamingEnumeration attrs = allAttr.getAll();attrs.hasMore();){ Attribute attr = (Attribute)attrs.next(); System.out.println("Attribute is : " + attr.getID()); for(NamingEnumeration values = attr.getAll();values.hasMore();){ System.out.println(" value is : " + values.next()); } } }catch(NamingException ex){ ex.printStackTrace(); } System.out.println("SUCCESS!"); } }
package study.jndi; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; public class GetAttr2{ public static final String ENTRY_NAME = "cn=bsj,ou=xc,ou=t,c=com"; public static void main(String[] args){ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL,"ldap://127.0.0.1:10389/"); //login the ldap server String[] attrNames = new String[]{"abc"}; //get a attribute's value try{ DirContext ctx = new InitialDirContext(env); Attributes attrs = ctx.getAttributes(ENTRY_NAME,attrNames); for(NamingEnumeration ae = attrs.getAll();ae.hasMore();){ Attribute attr = (Attribute)ae.next(); System.out.println("Attribute : " + attr.getID()); for(NamingEnumeration ve = attr.getAll();ve.hasMore();){ System.out.println(" Value : " + ve.next()); } } }catch(NamingException ex){ ex.printStackTrace(); } System.out.println("SUCCESS!"); } }
package study.jndi; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.NamingEnumeration; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchResult; public class BasicSearch{ public static final String ROOT_ENTRY = "ou=xc,ou=t,c=com"; public static void main(String[] args){ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL,"ldap://127.0.0.1:10389/"); try{ DirContext ctx = new InitialDirContext(env); Attributes matchAttrs = new BasicAttributes(true);//the attributes is interface matchAttrs.put(new BasicAttribute("cn","txc")); matchAttrs.put(new BasicAttribute("objectClass","top")); NamingEnumeration se = ctx.search(ROOT_ENTRY,matchAttrs); while(se.hasMore()){ SearchResult searchResult = (SearchResult)se.next(); System.out.println("**SearchResult : " + searchResult.getName()); JNDITools.listAttributes(searchResult.getAttributes()); } }catch(NamingException ex){ ex.printStackTrace(); } System.out.println("SUCCESS!"); } }
package study.jndi; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchResult; import javax.naming.directory.SearchControls; public class FilterSearch{ public static final String ROOT_ENTRY = "ou=xc,ou=t,c=com"; public static void main(String[] args){ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL,"ldap://127.0.0.1:10389/"); try{ DirContext ctx = new InitialDirContext(env); SearchControls controls = new SearchControls(); String filter = "(&(objectClass=top)(cn=*))"; /** * OBJECT_SCOPE *controls.setSearchScope(SearchControls.OBJECT_SCOPE); *NamingEnumeration se = ctx.search("cn=txc,ou=xc,ou=t,c=com",filter,controls); */ controls.setCountLimit(100); //If the program attempts to get more than the count limit number of results, then a SizeLimitExceededException will be thrown controls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration se = ctx.search("c=com",filter,controls); while(se.hasMore()){ SearchResult result = (SearchResult)se.next(); System.out.println("**SearchResult : " + result.getName()); JNDITools.listAttributes(result.getAttributes()); } }catch(NamingException ex){ ex.printStackTrace(); } System.out.println("SUCCESS!"); } }
package study.jndi; import javax.naming.NamingException; import javax.naming.NamingEnumeration; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; public class JNDITools{ public static void listAttributes(Attributes attrs)throws NamingException{ for(NamingEnumeration ae = attrs.getAll();ae.hasMore();){ Attribute attr = (Attribute)ae.next(); System.out.println("Attribute : " + attr.getID()); for(NamingEnumeration ve = attr.getAll();ve.hasMore();){ System.out.println(" value : " + ve.next()); } } } }