Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


|Views: 26|Likes:

More info:

Published by: sathish04021984@gmail.com on Nov 13, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





JNDI—Java Naming and Directory Interface
ByMark Wutka<http://www.informit.com/articles/article.aspx?p=26231> Date: Apr 5, 2002 Enterprise-level applications use a lot of different directory services-lookup services that locate re-sources associated with a particular name. When you use RMI, for example, you locate objects witha directory service called the RMI Registry. When you use CORBA, you use the COS Naming fa-cility (CORBA's naming service) to locate objects. When you convert a hostname to an IP address,you usually use a directory service called DNS (Domain Name Service). There are also general dir-ectory services that use protocols, such as X.500 (the CCITT directory standard) and LDAP (Light-weight Directory Access Protocol). These directory services can hold many kinds of data.Although most people tend to use the terms "naming service" and "directory service" interchange-ably, there is a difference. A
naming service
associates a single name with a specific resource. A
dir-ectory service
associates a name with a set of attributes and resources. When you search a namingservice, you can only search for a specific name. When you search a directory, you can search for items matching a specific set of attributes.One of the interesting things about all these types of naming and directory services is that they gen-erally perform the same task-mapping a name to some set of attributes or objects. Of course, not alldirectory services are created equally. Some of them have a flat namespace, whereas others offer atree structure for the names. Some of them allow you to store specific types of objects, whereas oth-ers allow you to store almost any kind of object.The
 Java Naming and Directory Interface (JNDI)
draws a distinction between naming services anddirectory services. A naming service maps a name to an object. The RMI Registry and the CORBA Naming Service are both examples of naming services. You can only store an RMI object in theRMI Registry and you can only store a CORBA object in the CORBA Naming Service. A directoryservice also stores objects, but these objects can have associated attributes that the directory servicerecognizes. You can search a directory using the item attributes. For example, you can search anLDAP directory for everyone in a specific department or everyone named Smith.JNDI provides a uniform way to access naming and directory services. It supports flat namespacesas well as tree namespaces, and it allows you to store many different types of objects. The beauty of JNDI lies it its simplicity and uniformity. After you know the basic JNDI API calls, you can readdata out of any kind of directory as long as there is a JNDI service provider for that directory.You have already encountered JNDI in several earlier chapters. You use JNDI to locate EnterpriseJavaBeans and JDBC connection pools from within your EJB container. You might have implemen-ted simple lookup schemes before in your applications; that is, you create a class with static lookupmethods or store a Hashtable in a static field somewhere. You might choose to use JNDI to replacethese kinds of local storage mechanisms, although you might need to write your own service pro-vider.JNDI is also extremely useful in the area of configuration. If many applications use common con-figuration data, you might consider storing the data in a directory service, such as LDAP, instead of in a file or database. LDAP is especially good if the configuration information is hierarchical-that is,if it is more like a tree structure than a flat list of values.One of the hidden benefits of directory services is the fact that there are a lot of directory service browsers and editors-especially for LDAP. You can view the contents of the directory and edit themusing an off-the-shelf tool. That saves you from having to write a custom configuration editor.
JNDI—Java Naming and Directory Interface
JNDI Basics
The Context class is the core of the JNDI API. You use it to perform any lookup and to add any newname-value associations. When you use JNDI, you typically create an InitialContext object first:
Context ctx = new InitialContext();
The InitialContext constructor looks for a system property called java.naming.factory. initial thatcontains the name of the class that creates the InitialContext. Sometimes, you must supply thisvalue yourself. Some EJB containers, like the one that comes with Sun's J2EE SDK, already havethis property set.JDK 1.3 comes with three built-in service providers: RMI, CORBA, and LDAP. The classnames for the different initial context factories are
Don't worry about setting defining the class for the initial context factory unless you get an error telling you there's no initial context factory.When you run your program, you can specify the initial context factory on the command-line usingthe -D option:
java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
usingj2ee.naming.JNDIDemoYou can also specify the initial context factory in a Hashtable that you can pass to the InitialContextconstructor:
Hashtable props = new Hashtable ();props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");Context ctx = new InitialContext(props);
Bear in mind that if you specify the initial context factory using a Hashtable object, you might belimiting the portability of your classes. For example, most WebLogic examples tell you to create theInitialContext this way:
Hashtable props = new Hashtable();props.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");props.put(Context.PROVIDER_URL,"t3://localhost:7001");Context = new InitialContext(props);
The problem here is that if you want to run your code with another application server, you'll have torecompile your code with a new set of properties. It's better to set these items on the command line:
java Djava.naming.factory.initial=weblogic.jndi.WLInitialContextFact-ory
-Djava.naming.provider.url=t3://localhost:7001 MyTestClient
JNDI—Java Naming and Directory Interface
Rather than specifying the initial factory on the command line, you can put these associations in afile called jndi.properties, which can be located somewhere in your classpath.When you develop Enterprise Java Beans, you can usually count on the environment being set up properly ahead of time, so you normally don't need to initialize any properties or set any system properties. When you run your client programs to test the EJBs, however, you usually need to spe-cify an initial context factory.Although most people use the InitialContext object as their first entry point into JNDI, there is analternative. You can use the javax.naming.spi.NamingManager class to create a service-specificcontext for you based on a URL prefix. A fully qualified JNDI name is of the form
is a name such as iiop, rmi, ldap, and so on, and
is the name of theitem in that service. The NamingManager class lets you create a Context object based on the servicename. For example, to create an LDAP Context object, you can call:
Context ctx = NamingManager.getURLContext("ldap", null);
One thing to keep in mind when you use this technique is that the Context you get back usuallydoesn't understand names for other services. For example, if you create an initial context that is aCORBA naming service, you can still do an LDAP lookup like this:
Object ob = context.lookup("ldap://localhost/dc=wutka,dc=com");
The InitialContext object knows how to resolve references that use other kinds of services. If youtry this with a context returned by getURLContext, however, you'll get an error telling you that thename isn't valid for the context you are using.Okay, now that you have a Context object, you can use the lookup method to locate an object. For example, when you locate an EJB, you usually make a call like this:
Object personHomeRef = context.lookup("java:comp/env/ejb/Person");
Don't forget, if you need to cast the result from context.lookup to a specific Remote or Home inter-face type, you must use PortableRemoteObject.narrow.The java service is available only within an EJB container, and it acts as a local directory service for other objects within the same EJB environment.To create a new name-value association, use the bind method:
ctx.bind("rmi://localhost/MyRemoteObject", remoteObject);
If the object already exists in the directory, bind throws a NameAlreadyBoundException. The re- bind method does the same thing as bind except that it doesn't care whether the object already ex-ists:
ctx.rebind("rmi://localhost/MyRemoteObject", remoteObject);
rebind doesn't throw an exception if the object doesn't exist; that is, you can use rebind to create anew association as well as to overwrite an old one.
JNDI—Java Naming and Directory Interface

Activity (4)

You've already reviewed this. Edit your review.
1 hundred reads
ssoni83 liked this
madhurimapatra1987 liked this
madhurimapatra1987 liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->