示例代码 2 Resolve2.java
import javax.naming.Binding; import javax.naming.NamingEnumeration; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; public class Resolve2 { public static void main(String argv[]) { // The user should provide a file to lookup if (argv.length != 1) { System.err.println("Usage: java Resolve2 "); System.exit(-1); } // Here we use the file system service provider Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.FSContextFactory"); env.put(Context.PROVIDER_URL, argv[0]); try { // Create the initial context Context ctx = new InitialContext(env); NamingEnumeration ne = ctx.listBindings(""); while(ne.hasMore()) { Binding b = (Binding) ne.next(); System.out.println(b.getName() + " " + b.getObject()); } // close the context ctx.close(); } catch (NamingException e) { System.err.println("Problem looking up " + argv[0] + ": " + e); } } } |
要测试这个应用程序,遵照与上一个例子同样的编辑和运行步骤即可。下面是一次示范运行:
prompt>: java Resolve2 file:///uddi fig1.gif C:\uddi\fig1.gif fig2.gif C:\uddi\fig2.gif fig3.gif C:\uddi\fig3.gif fig4.gif C:\uddi\fig4.gif fig5.gif C:\uddi\fig5.gif impl.txt C:\uddi\impl.txt |
目录服务
承前所述,目录服务便是其对象具有属性及名称的命名服务。具有属性和名称的对象被称为目录入口。应用程序可以使用目录服务存储和检索目录对象的属性。它甚至可以被用于对象存储。
LDAP
轻量级目录访问协议(LDAP)来源于X.500 协议(由位于Ann Arbor的密歇根大学开发),是一个用于访问和管理目录服务的协议;它定义了客户端应该如何访问存储在服务器上的数据,但没有定义应该如何存储数据。LDAP目录由带有描述性信息的入口组成,这些描述性信息描述了人(例如,姓名、电话号码、电子邮件地址,等等)或网络资源(比如打印机、传真机之类的)。这类描述性信息被存储在一个入口的属性中,入口的每个属性均描述了一种特定类型的信息。下面给出一个例子,内容是用于描述一个人的属性:
cn: Qusay H. Mahmoud mail: qmahmoud@javacourses.com telephoneNumber: 123-4567 |
LDAP 目录服务可以用于基于属性查找某个人的电话号码或电子邮件地址。表2列出了一些常见的LDAP 属性:
表 2: 一些常见的 LDAP 属性 | |
属性 | 意义 |
o | 组织 |
cn | 常用名 |
sn | 姓 |
uid | 用户id |
| 电子邮件地址 |
c | 国家 |
LDAP名称是一个 (名称,值) 对的序列,比如姓名、组织、国家。
cn=Qusay Mahmoud, o=javacourses.com, c=<st1:country-region>Canada</st1:country-region> |
javax.naming.directory.DirContext是一个JNDI的目录服务接口,它扩展了javax.naming.Context。它提供的方法有:
- search: 搜索匹配目录入口的目录,并比较一个目录入口和一组属性。
- bind 和createSubcontext: 添加一个新的目录入口。
- modifyAttributes: 修改一个目录入口的特定属性。rename 方法可以用于修改入口名称本身。
- unbind 和destroySubcontext: 删除一个特定的目录入口。
- close: 结束与一台LDAP服务器的会话。
使用JNDI 进行LDAP编程
要操作一台LDAP 服务器(比如Sun ONE Directory Server)中的对象,您必须首先连接到该服务器;您可能还需要使您自己通过服务器的身份验证。要连接到服务器,您可以从DirContext 接口获得对一个对象的引用。使用InitialDirContext 类可以做到这一点,而该类需要一个 Hashtable。
下面的代码片断可以使用户通过一台LDAP服务器的身份验证,并连接到该服务器上。注意,这里使用的是简单的身份验证。简单身份验证包括把用户的完全限定的DN和用户的明文口令发送给LDAP 服务器。要避免暴露明文口令,使用带有加密通道的SSL机制,如果您的LDAP服务器支持这种机制的话。想要了解关于身份验证模式的更多信息,请参见 JNDI Tutorial。
Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // specify where the ldap server is running env.put(Context.PROVIDER_URL, "ldap://GH308C-N-MAHMOUD.humber.org:61596"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager"); env.put(Context.SECURITY_CREDENTIALS, "password"); // Create the initial directory context DirContext ctx = new InitialDirContext(env); |
连接到LDAP 服务器上之后,您可以在LDAP服务器上添加新的入口、或者修改、删除、搜索一个入口。下面的代码片断说明了如何添加或存储一个新的入口。注意:要存储一个对象,您需要使用Java Schema装载它,而 Java Schema并没有在目录服务器上被预配置。想要了解关于此点的更多信息,请参见JNDI指南中的Java Objects and the Directory 部分。
SomeObject Obj = new SomeObjct("param1", "param2", "param3"); ctx.bind("cn=myobject", obj); |
您可以使用lookup 方法查找一个对象,如下:
SomeObject obj = (SomeObject) ctx.lookup("cn=myobject"); |
示例代码3 给出了一个如何检索命名对象的属性的例子。正如您所看到的那样,用于选择工厂类的代码与前面相同。我们使用InitialDirContext 类创建了一个目录上下文DirContext,getAttributes 方法用于返回对象的属性,而最后,get方法找到了姓并打印之。相当直观,是不是?
示例代码 3: GetAttrib.java
import javax.naming.Context; import javax.naming.directory.InitialDirContext; import javax.naming.directory.DirContext; import javax.naming.directory.Attributes; import javax.naming.NamingException; import java.util.Hashtable; class GetAttrib { public static void main(String[] argv) { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // specify where the ldap server is running env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=javacourses.com,c=<st1:country-region>Canada</st1:country-region>"); // use simple authenticate to authenticate the user env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager"); env.put(Context.SECURITY_CREDENTIALS, "password"); try { // Create the initial directory context DirContext ctx = new InitialDirContext(env); // Ask for all attributes of the object Attributes attrs = ctx.getAttributes("cn=Qusay Mahmoud"); // Find the surname ("sn") attribute of this object and print it System.out.println("Last Name: " + attrs.get("sn").get()); // Close the context ctx.close(); } catch (NamingException e) { System.err.println("Problem getting attribute: " + e); } } } |
JNDI提供用于进行基本和高级(使用过滤器)搜索的 API 。例如,使用一组入口必须具有的属性,以及要在其中执行搜索的目标上下文,便可以执行一次简单的搜索。下面的代码片断说明了如何在一棵子树中搜索一个具有uid=qmahmoud 属性的入口。使用过滤器的高级搜索不在本文的讨论范围之内。
// ignore attribute name case Attributes matchattribs = new BasicAttributes(true); matchattribs.put(new BasicAttribute("uid", "qmahmoud")); // search for objects with those matching attributes NamingEnumeration answer = ctx.search("ou=People,o=javacourses.com", matchattribs); while (answer.hasMore()) { SearchResult sr = (SearchResult)answer.next(); // print the results you need } |
想要了解使用JNDI编写LDAP 客户端方面的更多信息,请参见Tips for LDAP Users。
JNDI 的CORBA COS命名服务提供程序
CORBA 公共对象服务 (COS) 名称服务器用于存储CORBA对象引用。您可以使用COS命名包(org.omg.CORBA.CosNaming)在CORBA 应用程序中访问它。
JNDI COS命名服务提供程序基于COS命名包实现了javax.naming.Context 接口,这样CORBA 应用程序就能够使用JNDI访问 COS 名称服务器。因此,使用 JNDI 的CORBA 应用程序具有一个用于访问所有命名和目录服务的接口。这使得CORBA应用程序能够使用像LDAP这样的分布式企业级服务来存储对象引用。
要选择COS 命名服务提供程序,使用:
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); |
要转换您的CORBA 应用程序以使用JNDI,考虑AddServer.java 和AddClient.java,它们在另一篇文章中有更加详细的描述。
1. 在客户端和服务器中均使用javax.naming,将:
import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; |
替换为:
import javax.naming.*; |
2. 在客户端和服务器中使用InitialContext 代替 NameService :
将:
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); |
替换为:
Hashtable env = new Hashtable(); env.put("java.naming.corba.orb", orb); Context ctx = new InitialContext(env); |
3. 使用lookup 代替resolve:
将:
String name = "Add"; Add href = AddHelper.narrow(ncRef.resolve_str(name)); |
替换为:
Add href = AddHelper.narrow((org.omg.CORBA.Object)ctx.lookup("Add")); |
JNDI 的RMI 注册表服务提供程序
RMI 注册表服务提供程序允许JNDI 应用程序访问使用RMI注册表注册的远程对象。已知注册表所在的位置之后,提供程序使用绑定为注册在注册表中的对象创建一个命名上下文。接下来,这个上下文可以被绑定到另一个JNDI可访问的命名空间中,比如LDAP。这项新功能包含了java.rmi.Naming 类提供的功能。
这样使用RMI的主要优点是,客户端不再需要知道RMI注册表运行之处的主机名和端口号;它与位置无关。
下面的代码片断说明了如何将JNDI 与 RMI一起使用:
// select the registry service provider as the initial context env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); // specify where the registry is running env.put(Context.PROVIDER_URL, "rmi://server:1099"); // create an initial context that accesses the registry Context ctx = new InitialContext(env); // now, the names stored in registry can be listed NamingEnumeration enum = ctx.list(""); // bind the registry context into LDAP directory Context ldapctx = (Context)ctx.lookup("ldap://server:port/o=comp,c=ca"); ldapctx.bind("cn=rmi", ctx); |
</str>