JNDI Tutorial-Significance of jndi.properties

So far in this context, we were  discussing with basic JNDI concepts. We discussed naming service with a suitable example.We also discussed the various directory operations in Java using JNDI with  LDAP as reference. In this chapter we are discussing the significance of a configuration file which is very important while developing Java applications which uses JNDI API.The configuration file is the jndi.properties . In this chapter we are discussing the significance of jndi.properties .

Significance of jndi.properties

In the JNDI naming example we discussed earlier in this section , we were initializing the context object by passing a Properties object as argument.The Properties object contains all the initial properties to create the context. There we got the output successfully. The object bound by first application was retrieved by the second application.But , that approach is not practical always. For instance , if we are changing the JNDI service provider implementation , then in each class we need to change the coding. So the apprach we used there is not good in every time. An alternate way to initialize the context is by giving the properties in a properties file.Let us discuss with an example.

First let us see the Student.java class. An instance of this class is going to bind to the context.Later the second application retreives the same. We are using JBossNS as naming service. So we need to have the same set up as we discussed in the naming example..

Student.java

import java.io.Serializable;
public class Student implements Serializable {
public Student(int id, String name) {
this.id = id;
this.name = name;
}

private static final long serialVersionUID = 1L;
private int id;
private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "Id = "+getId() +"Name = "+getName();
}
}

Now let us see the FirstApp.java. It simply initializes a Student object and binds to the context.

FirstApp.java

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class FirstApp {
public void bindObject() {
Student student = new Student(1, "Bijoy");
try {
Context context = new InitialContext();
context.bind("student", student);
System.out.println("Bound object = "+student);
} catch (NamingException e) {

e.printStackTrace();
}
}
public static void main(String[] args) {
new FirstApp().bindObject();
}
}

Now let us see the SecondApp.java. It simply retrieves the Student object from the context.

SecondApp.java

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class SecondApp {
public void readObject() {

try {
Context context = new InitialContext();
Student student = (Student) context.lookup("student");
System.out.println("Object received from context = " + student);
} catch (NamingException e) {

e.printStackTrace();
}
}
public static void main(String[] args) {
new SecondApp().readObject();
}
}

Now before running the applications put the jndi.properties   file shown below in JAVA_HOME/lib/ directory .

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099

Now restart the JBoss server. When server  is up, run the FirstApp.java first and then the secondApp.java.

Output

Output of FirstApp.java

Bound object = Id = 1Name = Bijoy

Output of SecondApp.java

Object received from context = Id = 1Name = Bijoy

See also:

JNDI overview

Naming service  example using JBossNS service provider

Accessing LDAP using JNDI

Updating attribute of LDAP entry using JNDI

Deleting attribute of an LDAP entry

Adding attribute to an LDAP entry

Adding LDAP entry using JNDI

Deleting LDAP entry using JNDI

Displaying user entries in LDAP using JNDI