40
JNDI Shobana .P III MCA Ethiraj college for Women 20/10/2014

JNDI

Embed Size (px)

Citation preview

Page 1: JNDI

JNDI

Shobana .P

III MCA

Ethiraj college for Women

20/10/2014

Page 2: JNDI

JNDI

The Java Naming and Directory Interface(JNDI) is a Java API for a directory service

that allows Java software clients to discover

and look up data and objects via a name.

Page 3: JNDI

JNDI Naming

In a distributed application, components need to access other components and resources such as databases.

For example, a servlet might invoke remote methods on an enterprise bean that retrieves information from a database.

The Java Naming and Directory Interface (JNDI) naming service enables components to locate other components and resources.

Page 4: JNDI

JNDI Naming

To locate a JDBC resource, for example, an

enterprise bean invokes the JNDI lookup

method.

The JNDI naming service maintains a set of

bindings that relate names to objects.

The lookup method passes a JNDI name

parameter and returns the related object.

Page 5: JNDI

Naming Service

A naming service is an entity that

i. associates names with objects.

We call this binding names to objects.

This is similar to a telephone company ’s associating a person ’s name with a specific residence ’s telephone number

Page 6: JNDI

Naming Service

ii. provides a facility to find an object based on a name.

We call this looking up or searchingfor an object.

This is similar to a telephone operator finding a person ’s telephone number based on that person ’s name and connecting the two people.

Page 7: JNDI

Naming Service

Page 8: JNDI

Directory Service

A directory object differs from a generic

object because we can store attributes with directory objects.

For example,we can use a directory object to represent a user in your company.we can store information about that user,like the user ’s password,as attributes in the directory object.

Page 9: JNDI

Directory Service

A directory service is a naming service

that has been extended and enhanced to

provide directory object operations for

manipulating attributes.

A directory is a system of directory

objects that are all connected. Some examples of directory products are Netscape Directory Server and Microsoft ’s Active Directory.

Page 10: JNDI

Directory Service

Page 11: JNDI

Directory Service

Directories are similar to DataBases,

except that they typically are organized in

a hierarchical tree-like structure.

Page 12: JNDI

Directory Service

Page 13: JNDI

Directory Service Examples of Directory Service

1. Netscape Directory Server

2. Microsoft ’s Active Directory

3. Lotus Notes (IBM)

4. NIS (Network Information System) by Sun

5. NDS (Network Directory Service) by Novell

6. LDAP (Lightweight Directory Access Protocol) mostcommonly used

Page 14: JNDI

LDAP

LDAP stands for Lightweight Directory Access

Protocol.

It is a lightweight client-server protocol for

accessing directory services, specifically

X.500-based (electronic)directory services.

LDAP runs over TCP/IP or other connection

oriented transfer services.

Page 15: JNDI

LDAP A directory is similar to a database, but tends to contain more descriptive, attribute-based information.

give quick-response to high-volume lookup or search operations

They may have the ability to replicate information widely in order to increase availability and reliability, while reducing response time.

Page 16: JNDI

JNDI Architecture

Page 17: JNDI

JNDI Packagesjavax.naming

Accesses simple naming services.

javax.naming.directory

Accesses directory services.

javax.naming.event

Handles event notification when

dealing with naming and directory

services.

Page 18: JNDI

JNDI Packagesjavax.naming.ldap

Deals with LDAP v3 controls and

extended operations.

javax.naming.spi

Consists of the Service Provider Interface (SPI) classes and interfaces used by LDAP service implementers to provide access

to a specific type of naming or directory

service.

Page 19: JNDI

Naming Operations

Adding, Replacing, and Removing a Binding

The Context interface contains methods for adding, replacing, and removing a binding in a

context (collection of bindings is a context).

Page 20: JNDI

Naming OperationsAdding a Binding

Context.bind() is used to add a binding to a context. It accepts as arguments the name of the object and the object to be bound.

// Create the object to be bound

Fruit fruit = new Fruit("orange");

// Perform the bind

ctx.bind("favorite", fruit);

This example creates an object of class Fruit and binds it to the name "favorite" in the context ctx. If you subsequently looked up the name "favorite" in ctx, then you would get the fruit object. Note that to compile the Fruit class, you need the FruitFactoryclass.

Page 21: JNDI

Naming OperationsAdding or Replacing a Binding

rebind() is used to add or replace a binding. It accepts the same arguments as bind(), but the semantics are such that if the name is already bound, then it will be unbound and the newly given object will be bound.

// Create the object to be bound

Fruit fruit = new Fruit(“lemon");

// Perform the bind

ctx.rebind("favorite", fruit);

When you run this example, it will replace the binding created by the bind() example.

Page 22: JNDI

Naming Operations

Removing a Binding

To remove a binding, use unbind().

// Remove the binding

ctx.unbind("favorite");

when run, removes the binding that was

created by the bind() or rebind() example

Page 23: JNDI

Naming OperationsRenaming an Object

rename an object in a context by using Context.rename().

// Rename to old_report.txt

ctx.rename("report.txt", "old_report.txt");

This example renames the object that was bound to "report.txt" to "old_report.txt".

After verifying that the object got renamed, the program renames it to its original name ("report.txt"), as follows.

// Rename back to report.txt

ctx.rename("old_report.txt", "report.txt");

Page 24: JNDI

Naming Operations

Looking up an object

To look up an object from the naming service,

use Context.lookup() and pass it the name of

the object that you want to retrieve.

Suppose that there is an object in the

naming service with the name "report.txt".

To retrieve the object, use

//look up for report.txt

Object obj = ctx.lookup("report.txt");

Page 25: JNDI

Naming OperationsListing a Context

Instead of getting a single object at a time, as with Context.lookup(), we can list an entire context by using a single operation ,

There are two methods for listing a context:

(i )one that returns the bindings

Context.listBindings()

(ii) and one that returns only the name-to-object class name pairs.

Context.list()

Page 26: JNDI

Naming OperationsCreating and Destroying a Context

The Context interface contains methods for creating and destroying a subcontext, a context that is bound in another context of the same type.

Creating a ContextTo create a context, use createSubcontext()

// Create the context

Context result = ctx.createSubcontext("new");

This example creates a new context, called "new", that is a child of ctx. If you list the context ctx, you will see that there is now an entry for "new".

Page 27: JNDI

Naming OperationsDestroying a Context

To destroy a context, use destroySubcontext()the name of the context to destroy.

// Destroy the context

ctx.destroySubcontext("new");

This example destroys the context "new" in the context ctx.

Page 28: JNDI

JNDI ExampleStudent.java

import java.io.Serializable;

//The interface itself has no methods defined in it. So any class can easily implement

this interface by simply implementing it:

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;

}

Page 29: JNDI

JNDI Examplepublic 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();

}

}

Page 30: JNDI

JNDI Example

Now let us see how to bind a student object

using naming service. The FirstApp.java is a

stand alone application which binds a Student

object.

FirstApp.java

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

Page 31: JNDI

JNDI Examplepublic class FirstApp

{

public void bindObject()

{

Student student = new Student(1, "Bijoy");

Properties initialProperties = new Properties();

initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

initialProperties.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");

initialProperties.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");

Page 32: JNDI

JNDI ExampleTry

{

Context context = new InitialContext(initialProperties); context.bind("student", student);

System.out.println("Bound object = "+student);

}

catch (NamingException e)

{

e.printStackTrace();

}

}

Page 33: JNDI

JNDI Examplepublic static void main(String[] args)

{

new FirstApp().bindObject();

}

}

The bindObject() method creates a Context object and a Student object is binding to the specified context . The initialProperties has the details for initializing the context.It is desirable to give these properties through jndi.properties in src folder , if we need these things in a generic way.

Now lets see the SecondApp.java. It simply receoversthe already bound Student object.

Page 34: JNDI

JNDI Example

SecondApp.Java

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

Page 35: JNDI

JNDI Examplepublic class SecondApp

{

public void readObject()

{

Properties initialProperties = new Properties();

initialProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");

initialProperties.put(InitialContext.PROVIDER_URL,12 "jnp://localhost:1099");

initialProperties.put(InitialContext.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

Page 36: JNDI

JNDI ExampleTry

{

Context context = new

InitialContext(initialProperties);

Student student = (Student)

context.lookup("student");

System.out.println("Object received from context =

"+student);

}

catch (NamingException e)

{

e.printStackTrace();

}

}

Page 37: JNDI

JNDI Example

public static void main(String[] args)

{

new SecondApp().readObject();

}

}

Page 38: JNDI

JNDI Example

Output:

Output of FirstApp.Java

Bound object = Id = 1Name = Bijoy

Now run SecondApp.java as java application.

Output of SecondApp.Java

Object received from context = Id = 1Name =

Bijoy

Page 39: JNDI

JNDI Uses connecting a Java application to an external directory service (such as an address database or an LDAP server)

allowing a Java Servlet to look up configuration information provided by the hosting web container

JNDI allows distributed applications to look up services in an abstract, resource-independent way.

Page 40: JNDI

Thank you