JNDI - Java Naming and Directory Interface

 

API for naming and directory services.

Naming: service to provide name-to-object mapping

Directory: service provies info about objects and tools for finding them.

 

javax.naming package

main class InitialContext,

2 key interfaces: Context, Name

Context

Context ctx = new IntialContext();

String name = "mary";
String email = "mary@fun.com"
ctx.bind(name,email);

 

......

String str = (String) ctx.lookup("mary");

ctx.unbind("mary");

......
ctx.close();

 

  • bind()
        
    adds an entry to context
  • rebind()
       
     
  • lookup()
        
    find an object.
  • unbind()
        
    delete an entry
 
Context InitialContext()

Connects to default JNDI service and establishes a new context

Context InitialContext(Properties p) Connects to specific JNDI service and makes new context
void bind(Name name, Object obj) Binds name with obj
void bind(String name, Object obj)  
void rebind(String name, Object obj) replaces an entry name
void unbind(Name name) removes binding of object to name
Object lookup(Name name) looks up object in naming service using name
void rename(String oldName, String newName) renames name of binding
NamingENumeration listBindings(Name contextName) enumerates all the names bound in the context name along with objects bound to them
NamingEnumeration listBindings(String contextName) Enumerates all names bount in the context anem along with objectgs
void close() disconnects from JNDI service.

 

Example Client Application connecting to JNDI service

import java.util.*;
import java.rmi.*;
import java.io.*;
import javax.naming.*;
import.javax.ejb.*;

//this client demonstrates use of JNDI

public class Client{

       public static InitialContext ctx;

public static void main(String[] argv) {
   

    try{
       print("Conneting to a JNDI service ....");
       ctx = new InitialContext();
       System.out.print("connected successfully");

       //add binding
       String name = "mary"; String email="mary@fun.com";
       ctx.bind(name, email);

      //lookup binding
      String s = (String) ctx.lookup("mary");
      print(s);

      //delete binding
      ctx.unbind("mary");

      //close

      ctx.close();

}catch(CommunicationException e) { //failed to connect}

}}

       

Connecting to a specific JNDI Provider

Context.INITIAL_CONTEXT_FACTORY

specifies JNDI provider

Context.PROVIDER_URL

specifies URL location.

Programmatically

//Set JNDI environment in the properties
Properties prop = new Properties();

//Set JNDI provider as Weblogic
prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

//set JNDI URL to access teh service
prop.put(Context.PROVIDER_URL, "t3://localhost:7001");

//Connect to the JNDI service as specified above
Context ctx = new InitialContext(prop);

 

Declarative Method

edit JAVA_HOME/lib/jndi.properties file (ON clients machine)

 

e.g. for WebLogic

java.naming.factory.initial=weblogic.jndi.WLInitialConextFactory

java.naming.provider.url=t3://localhost:7001

 

e.g. for JBoss
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

java.naming.provider.url=jnp://localhost:1099

java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

 

 

 

 

Example Looking up EJB Component

 

from client code do the following.....(here is example of a remote interface)

NOTE: name of EJB interface HelloWorldInterface and bean HelloWorldBean, they are both with the client in the package helloworld

 

public static void main(final String[] args) throws Exception {

String JNDI_NAME = "helloworld.HelloWorldBean" + "_" + HelloWorldInterface.class.getName() + "@Remote“;   

//grab content of container
Context initialContext = new InitialContext(); 

//look up EJB in this container and grab interface object
HelloWorldInterface businessItf = (HelloWorldInterface) initialContext.lookup(JNDI_NAME);

//now use the EJB
System.out.println("Calling helloWorld method...");

//call method of bean remotely through client’s interface object.businessItf.helloWorld();

}

 

 

© Lynne Grewe