4.2 JNDI Tasks

This section covers the following tasks:

4.2.1 Creating an Initial Context

To create an initial context, you must do the following:

  1. Identify all the values needed for the naming system initial context.
    1. Initial Context Factory

      The first value is the initial context factory, which is listed in the documentation for the naming system's service provider. The initial context factories are also stored as static String variables in the NJCL class com.novell.utility.naming.Environment.

      For example, the NDS initial context factory has the String value

         Environment.NDS_INITIAL_CONTEXT_FACTORY
         
    2. Provider URL

      The second value is the provider URL. The NDS provider URL takes the form of

         nds://[tree name]/[fully distinguished name]
         

      as in

         nds://planetary/.admin.administration
         
    3. Other Information On a Per-provider Basis

      You may have to specify other values in order to create the initial context. Refer to the documentation for a naming system's service provider to learn about other values it may require.

  2. Set the JNDI properties for the initial context by creating a Hashtable object with these values.

    Creating an initial context requires that you set certain JNDI properties for the creation of that context. JNDI uses key/value pairs associated with java.util.Hashtable to represent a JNDI property. The JNDI interface javax.naming.Context stores the hashtable keys as static String variables. You determined the value for this key in the previous step.

    For example, the key for the initial context factory pair has the String value

       Context.INITIAL_CONTEXT_FACTORY
       

    and the key for the provider URL pair has the String value

       Context.PROVIDER_URL
       

    Use the Hashtable.put() method to insert key/value pairs into the hashtable as in the following:

       Hashtable hash = new Hashtable(); 
       hash.put("key", "value");
       
  3. Create a new initial context using this Hashtable object.

    In order to construct a new instance of an initial context, you must tell the JNDI naming manager about the appropriate JNDI properties it should use to create that context. You set those properties in the previous step when creating the Hashtable object. Once you have created the initial context, you do not need to do anything else with it in order to start accessing the naming system with JNDI.

Code Sample:

The Java class Acl.java has a static method called createInitialContext(), which demonstrates how to create an initial context. See the section of code in Acl.java beginning with the following line of code:

   private static DirContext createInitialContext
      (String _providerURL)
   

Related Topics:

4.2.2 Looking up a Context

To look up a context, you must do the following:

  1. Get a context.
  2. Call its lookup method.
  3. Cast it to the object you want.

Code Sample:

The Java class NetWareObjectLister.java has the following line of code that demonstrates how to look up a context:

   Object obj =
      initialCtx.lookup(MyEnvironment.DISTINGUISHED_NAME);
   

4.2.3 Accessing Attributes

To access attributes, you must do the following:

  1. Get a DirContext.
  2. Call its getAttributes method.

    The getAttributes method takes the name of the context whose attributes you want to get as a parameter.

    To get your own context's attributes, pass in an empty string.

    When the getAttributes() method is called, an Attributes interface is returned.

  3. With the Attributes interface, do one of the following:
    • Call the getAll() method, which returns a Naming Enumeration containing all of the attributes as a group of Attributes.
    • Call the getIds() method, which returns a Naming Enumeration containing all of the Ids found in the Attributes interface as a group of Strings.

    Both of these methods return a Naming Enumeration, which can contain objects of different classes.

  4. Traverse through the group of Attributes or Strings until you find the one you want.
  5. If you have an individual Attribute, you need to get its values in order to work with it.

Code Sample:

The Java class Acl.java has a static method called printACL(), which demonstrates how to access attributes. See the section of code in Acl.java beginning with the following line or code:

   private static boolean printACL(DirContext target)
   

Related Reference Guide Topics: