Basic Lookup
JNDI (Java Naming and Directory Interface) organizes its names into a hierarchy. A name can be any string such as "com.mydomain.ejb.MyBean". A name can also be an object that supports the Name
interface, however a string is the most common way to name an object. A name is bound to an object in the directory by storing either the object or a reference to the object in the directory service identified by the name.
The JNDI API defines a context that specifies where to look for an object. The initial context is typically used as a starting point.
In the simplest case, an initial context must be created using the specific implementation and extra parameters required by the implementation. The initial context will be used to look up a name. The initial context is analogous to the root or top of a directory tree for a file system. Below is an example of creating an initial context:
Hashtable contextArgs = new Hashtable; // First you must specify the context factory. // This is how you choose between jboss implementation // vs. an implementation from Sun or other vendors. contextArgs.put( Context.INITIAL_CONTEXT_FACTORY, "com.jndiprovider.TheirContextFactory" ); // The next argument is the URL specifying where the data store is: contextArgs.put( Context.PROVIDER_URL, "jndiprovider-database" ); // (You may also have to provide security credentials) // Next you create the initial context Context myCurrentContext = new InitialContext(contextArgs);A context is then used to look up previously bound names in that context. For example:
MyBean myBean = (MyBean) myCurrentContext.lookup("com.mydomain.MyBean");Alternative to above code is as below:
The Context object can also be configured by adding jndi.properties file in classpath containing initial context factory class name and provider URL. The above code will be reduced as shown below:
//just need to create initial context object, it will try to read jndi.properties file from //classpath. Context myCurrentContext = new InitialContext;A context is then used to look up previously bound names in that context. For example:
MyBean myBean = (MyBean) myCurrentContext.lookup("com.mydomain.MyBean");Read more about this topic: Java Naming And Directory Interface
Famous quotes containing the word basic:
“The man who is admired for the ingenuity of his larceny is almost always rediscovering some earlier form of fraud. The basic forms are all known, have all been practicised. The manners of capitalism improve. The morals may not.”
—John Kenneth Galbraith (b. 1908)