Download Lecture 1-1: Introduction to JNDI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CPAN423 Enterprise Java Programming
Lecture #1-1: An Introduction to JNDI
A naming service is a term that associates resources and objects with
unique names. This way, applications can access them using their names.
For example DNS maps machines names such as www.mysite.com to IP
addresses. A file system maps a file name to a file handler. The file
handler can be used by an application to access the content of the file. The
mapping or association process called binding.
A directory service is an extension of naming service that allows for
associating attributes with stored objects. Therefore we can access an
object by its name, we can access the object by its attributes, or find the
objects’ attributes.
JNDI architecture consists of an interface which provide a set of API for
accessing naming and directory services and a service provider interface
(SPI) which in turn is used to attache providers to the JNDI. JNDI provide
a centralized storage mechanism for components in distributed computing
environment.
JNDI API consists of 5 packages:
Javax.naming: contains classes and interfaces for accessing naming
services. For example the context interface is used to lookup, bind,
unbind and rename objects.
Javax.naming.directory: Provide mechanism for accessing directory
services as well naming services.
Javax.naming.event: supports event notification in JNDI services.
Javax.naming.ldap: provide mechanism for accessing LDAP v3.0
Javax.naming.spi: enable developers to provide their own implementation
of naming and directory services and make them available via JNDI.
There are 3 types of JNDI:
1- LDAP( Lightweight Directory Access Protocol) is a directory service
2- DNS is a naming service
3- File systems.
There are 3 types of JNDI locations:
1- Global : use any namespace that doesn’t start with java:/ and is
accessible to any component that is allowed to use JNDI
2- Local: begins with java:/comp/ and accessible to components within
the same container.
3- Individual Local: begins with java:/comp/env/.Configured for each
component.
1
Example:
In this example we will use JNDI to store a reference to an object of a
class called Student in the directory c:/temp. We use a reference
because we cannot store an object into a directory directly with the
JNDI naming services.
Here is the code for Student class. It has to implement Referenceable
interface. A Reference consists of an ordered list of addresses and
class information about the object being referenced. The
Referenceable interface defines the method getReference which
return a reference to the same object.
import javax.naming.*;
import java.util.*;
import javax.naming.spi.ObjectFactory;
public class Student implements Referenceable
{
String id;
String name;
public Student(String i,String n)
{
id=i;
name=n;
}
public Reference getReference() throws NamingException {
// creating a reference to the Student object
Reference r= new Reference(
Student.class.getName(),
StudentFactory.class.getName(),
null);
/* the StudentFactory is a class that defines how
the Student object is created
*/
//Add addresses to the end of the list of addresses
r.add(new StringRefAddr("id", id));
r.add(new StringRefAddr("name", name));
return r;
}
public String toString() {
2
return id;
}
public String getId()
{
return id;
}
public String getName()
{
return name; }
}
The StudentFactory is a class that defines how the Student object is
created. Here is the code for this class:
import javax.naming.*;
import java.util.*;
import javax.naming.spi.ObjectFactory;
public class StudentFactory implements ObjectFactory {
public StudentFactory()
{
}
public Object getObjectInstance(Object obj, Name name,
Context ctx, Hashtable env) throws Exception
{
if (obj instanceof Reference)
{
Reference ref = (Reference)obj;
if (ref.getClassName().equals(Student.class.getName()))
{
RefAddr [] addr = new RefAddr[2];
addr[0]=ref.get("id");
addr[1]=ref.get("name");
if (addr != null)
3
return new
Student((String)addr[0].getContent(),(String)addr[1].getContent());
}
}
}
}
return null;
Basically, this class will return a reference to the Student object.
Now, we have both classes. Let’s try to see how we can use JNDI to
store a reference to a Student object to a directory.
import javax.naming.*;
import java.util.*;
import javax.naming.spi.ObjectFactory;
public class JNDIEx
{
public static void main(String args[])
{
Hashtable env = new Hashtable();
/* you need to download the file system service provider from sun
website.
You will need to add the fscontext.jar and providerutil.jar to your
class path
*/
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///c:/temp");
try {
// Create the initial context
Context ct = new InitialContext(env);
// Look up an object
Object obj = ct.lookup("C:/TEMP");
// Print it
System.out.println("temp" + " is bound to: " + obj);
4
Student st= new Student("1","muthana");
// bind the student object to the initial context
ct.rebind("student",st);
// lookup the object from the initial context
Object st1 = ct.lookup("student");
Student stt=(Student) st1;
// Print it
System.out.println("Student id :" + stt.getId());
System.out.println("Student Name :" + stt.getName());
} catch (NamingException e) {
System.err.println("Problem looking up " + "temp" + ": " + e);
}
}
}
After you run the example, check the folder c:/temp to see what
content it has. You will see that a file called .bindings is created by the
JNDI and it contains information about the Student object.
Now, if you comment the following lines in the JNIEx.java, you should
be able to retrieve the same information stored before about the student
object.
Student st= new Student("1","muthana");
ct.rebind("student",st);
5