Published on


Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. JNDI介绍 Author: Zianed zianed@live.cn 3 Sept. 20082008 By Neusoft Group. All rights reserved Copyright © Neusoft Confidential
  2. 2. JAVA Naming and Directory Interface 1、JNDI介绍 2、JNDI使用范围 3、JNDI包含内容 4、JNDI示例 5、认识LDAP 3 Sept. 2008 Confidential
  3. 3. 1、JNDI介绍 1、基于JAVA,可以跨平台 2、Interface,仅仅是一套接口程序,依靠别人实现的 3、狭义上仅仅是接口;广义上是一个体系(服务提供者、使用 者、交流) 4、 5、 6、是一个主动查找的过程(lookup),与DI相反 3 Sept. 2008 Confidential
  4. 4. SUN 官方说明 1. Naming and directory services play a vital role in intranets and the Internet by providing network-wide sharing of a variety of information about users, machines, networks, services, and applications. 2. JNDI works in concert with other technologies in the Java Platform, Enterprise Edition (Java EE) to organize and locate components in a distributed computing environment. 3. JNDI is designed especially for the Java platform using Java's object model. Using JNDI, applications based on Java technology can store and retrieve named Java objects of any type. 4. JNDI is also defined independent of any specific naming or directory service implementation. It enables applications to access different, possibly multiple, naming and directory services using a common API. 3 Sept. 2008 Confidential
  5. 5. Naming 1. the means by which names are associated with objects and objects are found based on their names. 2. 通过名字来找对象 3. 名字————对象 www.sun.com———— /zianed/hello.txt————文件的内容 4. 命名系统:A naming system is a connected set of contexts of the same type (they have the same naming convention) and provides a common set of operations. 5. 命名空间:A namespace is the set of names in a naming system. 这是一个Where和What的问题?是不是需要When、How、Who 3 Sept. 2008 Confidential
  6. 6. Directory 1. 由目录系统扩展而来,不仅提供对象,还能对它的属性进行访 问。 2. A directory object is a particular type of object that is used to represent the variety of information in a computing environment. A directory object can have associated with it attributes . An attribute has an identifier and a set of values. 3. A directory object provides operations for creating attributes, adding, removing, and modifying attributes associated with the directory object. 4. A directory service is a service that provides operations for creating, adding, removing, and modifying the attributes associated with objects in a directory. The service is accessed through its own interface. 3 Sept. 2008 Confidential
  7. 7. 2、JNDI使用方法 1、JNDI与JDBC 2、Lightweight Directory Access Protocol (LDAP) ——D 3、Common Object Services (COS) 4、Common Object Request Broker Architecture (CORBA) 5、Novell Directory Service (NDS) ——D 6、Java Remote Method Invocation (RMI) Registry 7、Java Messaging Service (JMS) 3 Sept. 2008 Confidential
  8. 8. JNDI Architecture • The JNDI architecture consists of an API and a service provider interface (SPI). 3 Sept. 2008 Confidential
  9. 9. 各种的SPI JDBC——各种Jaee容器 JMS——Apache-Activemq LDAP——Openldap,Lotus Domaino,windows的Server版本 等 3 Sept. 2008 Confidential
  10. 10. JNDI的优缺点分析 • 优点 1、消除硬编码,将应用工程的程序中的提供服务由容器解 决,自己只是负责调用; 2、JNDI可以缓存数据,消除每次都new的资源请求; 3、是JAVA SE的一部分。 • 缺点 1、获取类型不明确,需要自己强制转换; 2、能提供的服务多种多样,相应提供服务步骤繁杂。 3 Sept. 2008 Confidential
  11. 11. 3、JNDI包含内容 • 这是When、How、Who的问题? • When 在服务器启动后后。开始提供Naming和Directory服务, 直到程序 • Who 需要使用该服务的程序以及该工程。 3 Sept. 2008 Confidential
  12. 12. How 区别:a client would use interfaces and classes from javax.naming, while a service provider that is hooking up a naming service would use both javax.naming and javax.naming.spi. 1.配置容器 2.设置初始化LDAP服务的类型 3.进行操作:增加、删除、修改属性 4.关闭 3 Sept. 2008 Confidential
  13. 13. 4、JNDI示例 JDBC与JNDI的例子 在tomcat容器中提供JDBC服务 1.绑定JDBC数据源 jdbc/erp 利用context.xml和web.xml 2.Tomcat容器提供数据源服务,供工程访问 3 Sept. 2008 Confidential
  14. 14. Cont, 3.获取数据源 try { // 初始化tomcat容器 Context env = (Context) new InitialContext() .lookup("java:comp/env"); // 在容器中找到数据源 DataSource pool = (DataSource) env.lookup("jdbc/erp"); } catch (NamingException ne) { // 没有找到命名空间 ne.printStackTrace(); } 3 Sept. 2008 Confidential
  15. 15. 5、认识LDAP 什么是LDAP? 1. 是一个协议Lightweight Directory Access Protocol 2. 需要协议的实现 3. 是协议,就需要至少双方的交流。需要一个服务器来提供支 持这种协议的成功通信 4.通过LDAP获取信息并处理的流程 3 Sept. 2008 Confidential
  16. 16. LDAP Server 提供该服务的服务器有很多,本节使用Openldap服务器: Linux版: http://www.openldap.org/software/download/ Win32 版: http://download.bergmans.us/openldap/openldap- 2.2.29/openldap-2.2.29-db-4.3.29-openssl-0.9.8a- win32_Setup.exe 3 Sept. 2008 Confidential
  17. 17. LDAP演示: 登录的实现。 (暂不考虑细节) 3 Sept. 2008 Confidential
  18. 18. • Zianed • Homepage:http://my.unix-center.net/~Zianed/ • Mail: hxuanzhe86@sina.com • MSN:zianed@live.cn • QQ:1196123432 • QQGroup: 50457022 • Date:2009-10-24 3 Sept. 2008 Confidential
  19. 19. 谢谢