RMI-IIOP and the Java Naming and Directory Interface


RMI-IIOP and the Java Naming and Directory Interface

The JNDI is a J2EE API that provides an interface for locating objects, networks, machines, and so on. For example, JNDI can locate a network printer or connect to a remote database residing in an undefined location somewhere on the Internet.

A naming service relates names to objects. This means binding to an object via a name. An example of naming is the following: when a user wants to locate a machine on a network, the JNDI uses the Domain Name System (DNS) to translate a machine name to an IP address.

A directory service is a naming service extended to provide directory operations. In essence, a directory is a collection of interrelated and connected directory objects. Microsoft’s Active Directory is an example of a directory service. Internally, a directory is constructed as a hierarchical treelike structure.

Examining the JNDI Infrastructure

The JNDI consists of the following packages:

  • javax.naming Contains classes and interfaces for accessing naming services.

  • javax.naming.directory Extends the core javax.naming package in order to provide functionality for accessing directories as well as naming services.

  • javax.naming.event Contains classes and interfaces for supporting event notification in naming and directory services.

  • javax.naming.ldap Contains classes and interfaces for supporting LDAPv3 extended controls and operations.

  • javax.naming.spi Contains classes and interfaces that permit various naming and directory service providers to be dynamically attached beneath the JNDI.

    Note

    The complete details of these classes and interfaces are available for viewing at http://www.java.sun.com.

The JNDI consists of two separate units: the client API and the service provider interface (SPI). Client APIs allow clients to program to a single interface, whereas the SPI allows naming and directory service developers to plug their proprietary protocols into the system. The JNDI requires only a modest learning curve because a single API exists for accessing directory service information.

The JNDI must be used with an LDAP-enabled JNDI provider. It offers the following services:

  • Provides a pluggable interface that allows you to talk to any naming service via RMI, LDAP, or DNS.

  • Separates the application from implementation details.

  • Reads and writes Java objects from directories.

  • Makes it possible to link different types of directories, such as an LDAP directory with an NDS directory.

  • Retrieves a reference to the Java Transaction API.

  • Facilitates connections to JDBC drivers or Java Message Service (JMS) drivers.

The following code demonstrates how to look up an object using the JNDI. The application searches for an object named IFCEObject. The program specifies a service provider to use for the initial context. It places the name of the service provider class in the environment properties, utilizing the services of a Hashtable object. The specified service provider ID calls com.sun.jndi.fscontext.RefFSContextFactory, which is passed as the second argument to the put() method. The Hashtable object, called fpe, is passed as an argument to the InitialContext constructor, subsequently returning the initial context, called ctx. The lookup method for InitialContext is handed the search criteria: the name of the object. Finally, the application displays the object returned by the lookup() method. The context is then closed. Here is the code:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
class MyLookupClass {
public static void main(String[] args) {
String MyObject = "MyObject";
Hashtable fpe = new Hashtable(1);
fpe.put(Contect.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
try{
InitialContext ctx = new InitialContext(fpe);
Object obj = ctx.lookup(myObject);
System.out.println("Object: " + obj);
ctx.close();
}
catch (NamingException error) {
System.err.println("Error: " + error.getMessage());
}
}
}

Retrieving Attributes

It is possible to retrieve file attributes using directory services. The following example creates a class called MyRetrieveAttributeClass. The small application fetches and presents on-screen attributes related to MyObject. The main() class creates a Hashtable object named fpe. Then the program invokes the put() method twice. The first call places the directory service provider in the hash table, and the second call puts the directory service URL into the same table. Here is the code:

fpe.put(Context.SECURITY_CHECK, "userID");
fpe.put(Context.SECURITY_CREDENTIALS, "myPassword");

//MyRetrieveAttributeClass program code
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 MyRetrieveAttributeClass {
public static void main(String[] args){
Hashtable fpe = new Hashtable(2);
fpe.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
fpe.put(Context.PROVIDER_URL, "ldap://127.0.0.1:398");
try{ InitialDirContext ctx = new InitialDirContext(fpe)
Attributes atr = ctx.getAttributes("MyObject");
System.out.println(atr.get("MyObject");
ctx.close();
}
catch (NamingException error)
{ System.err.println("Error: " + error.getMessage());
}
}
}

Using Binding in Your Directory Service

It is possible to bind a name to a specified object in a directory service by using the bind() method of the InitialContext class. The following code demonstrates how to achieve this:

//MyAddBinding
import javax.naming.*;
import java.util.Hashtable;
class MyAddBinding {
public static void main (String[] args) {
Hashtable fpe = new Hashtable(2);
fpe.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LDAPCtxFactory");
fpe.put(Context.PROVIDER_URL, "localhost:1099");
try {
InitialContext ctx = new InitialContext(fpe);
User newUser = new User("Steve Heim");
ctx.bind("username", newUser);
Object obj = ctx.lookup("UserName");
System.out.println(obj);
ctx.close();
}
catch (NamingException error) {
System.out.println("error: " + error.getMessage());
}
}
}

It is also possible to replace binding in the directory service. Here is the essential code for replacing binding:

//replace binding
import javax.naming.*;
import java.util.Hashtable;

class MyReplaceBinding {
public static void main (String[] args) {
Hashtable fpe = new Hashtable(2);
fpe.put(Context.INITIAL_CONTEXT_FACTORY,
" com.sun.jndi.ldap.LDAPCtxFactory ");
fpe.put(Context.PROVIDER_URL, "localhost:1099");
try {
InitialContext ctx = new InitialContext(fpe);
User newUser = new User("Jonathan Peltzer");
ctx.rebind("UserName", new user);
Object obj ctx.lookup("UserName");
System.out.println(obj);
ctx.close();
}catch (NamingException error){
System.out.println("Error: " + error.getMessage());
}
}
}

You can remove binding from a directory service. Here is the code for doing so:

//Remove binding
import javax.naming.*;
class MyRemoveBinding {
public static void main (String[] args) {
Hashtable fpe = new Hashtable(2);

fpe.put(Context INTITIAL_CONTEXT_FACTORY,
" com.sun.jndi.ldap.LDAPCtxFactory ");
fpe.put(Context.PROVIDER_URL, "localhost:1099");
try{
InitialContext ctx = new InitialContext(fpe);
ctx.unbind("UserName");
Object obj = null;
try{
obj = ctx.lookup("UserName");
}
catch (NamingNotFoundException error) {
System.out.println("Binding Removal failed");
ctx.close();
}
catch (Naming Exception error){
System.out.println("Error: " + error.getMessage());
}
}
}




.NET & J2EE Interoperability
Microsoft .NET and J2EE Interoperability Toolkit (Pro-Developer)
ISBN: 0735619220
EAN: 2147483647
Year: 2004
Pages: 101
Authors: Simon Guest

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net