blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JNDI using a simple service provider

If you hadn't downloaded the File System service provider, please do so. Read Prerequisites

Before performing any operation on a naming or directory service, you need to acquire an initial context--the starting point into the namespace. This is because all methods on naming and directory services are performed relative to some context. To get an initial context, you must follow these steps.

1. Select the service provider of the corresponding service you want to access.

You can specify the service provider to use for the initial context by creating a set of environment properties (a Hashtable) and adding the name of the service provider class to it.
To specify the file system service provider from Sun Microsystems, you would write code that like the following.

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
 "com.sun.jndi.fscontext.RefFSContextFactory");
You can use application resource files, or system properties, or applet parameters to specify environment properties.

What if you use more than one of these mechanisms at the same time?
Answer is here.



2. Specify any configuration that the initial context needs.

You specify any additional information the service provider needs by adding it to the same Hashtable.
For example, suppose that the program is using the LDAP service provider. This provider requires that the program specify the location of the LDAP server, as
well as user identity information. To provide this information, you would write code that looks as follows.

env.put(Context.PROVIDER_URL, "ldap://ldap.wiz.com:389");
env.put(Context.SECURITY_PRINCIPAL, "joeuser");
env.put(Context.SECURITY_CREDENTIALS, "joepassword");



3. Call the InitialContext constructor.

Context object is the door of your client program to interact with the NamingManager and the NamingManager looks for configuration and setup information in the context object.

Context ctx = new InitialContext(env);
Once you have a initialContext, you can do operations on it, the most common for a naming service being lookup().
Object obj = ctx.lookup("report.txt");
Instead of getting a single object at a time, as with Context.lookup(), you can list an entire context by using a single operation, list().
NamingEnumeration list = ctx.list("awt");

while (list.hasMore()) {
    NameClassPair nc = (NameClassPair)list.next();
    System.out.println(nc);
}


Let's run a sample.

Create a java application and add fscontext.jar and providerutil.jar (downloaded previously) to it's build path.
If you miss this, you will get
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Create a Folder and few files. I created folder temp on my Desktop with 2 text files, a.txt and b.txt within it.

If you check out the file system service provider documentation, you will see that the service provider actually contains 2 Provider (classes).

- The first class handles accessing the file system. Lookup() operations are handled by this provider.
- The second class handles storage of Java objects. Bind() operations are handled by this provider.

package com.sample;

import java.io.File;
import java.util.Hashtable;

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

public class TestJDNI {

 public static void main(String[] args) throws NamingException {
  Hashtable<String, String> env = new Hashtable<String, String>(2);
  env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");

  Context ctx = new InitialContext(env);
  
  File obj = (File) ctx.lookup("C:/Users/eIPe/Desktop/temp/a.txt");
  
  System.out.println("File: " + obj.getName() + " exists? "
    + obj.exists());

  NamingEnumeration<?> list = ctx.list("C:/Users/eIPe/Desktop/temp");
  System.out.println("Contents in /temp directory");
  while (list.hasMore()) {
   NameClassPair nc = (NameClassPair) list.next();
   System.out.println(nc);
  }

 }
}

Output:
File: a.txt exists? true
Contents in /temp directory
a.txt: java.io.File
b.txt: java.io.File

Continue reading: Bindings and Lookups

No comments:

Post a Comment