Previous Table of Contents Next


import java.util.Hashtable;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;

/*
   This is an object factory that when given a reference for a Car
   object, will create an instance of that Car.
*/
public class CarFactory implements ObjectFactory 
{
          public CarFactory()
    {
    }

         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(Car.class.getName()))
            {
            RefAddr addr = ref.get(“Car Description”);
                if (addr != null)
                {
                        String s = (String)addr.getContent();
                    int n = s.indexOf(“:”);
                    String make = s.substring(0,n);
                    String model = s.substring(n+1);
                 return new Car(make,model);
                }
            }
        }
        return null;
    }
}

The method getObjectInstance parses the reference’s components and tries to construct a reference that describes a Car object. The factory may ignore non-relevant attributes of the reference. For example, there could be an IP address of a remote Car object; this factory could ignore it and just create a new Car instead. If the factory fails to construct a car from the information in the reference, it can return null or throw an exception. The values of a reference object are interpreted by the factory object, so it is possible to have two different factory objects that construct completely different objects based on the same reference. The reference could be valid in both cases. For example, the car factory creates a simple Car object and returns it based on the make and model. However, the same reference could contain other information about the car such as the plant in which it is physically being built as well as the order id. A second factory class could read this extra information out of the reference and create an object that checks on the status of the car at the assembly plant.

The naming package provides quick access to objects provided by naming services. The next section looks at how directory packages can be used to perform more sophisticated queries.

The Directory Package

The directory package javax.naming.directory extends the javax.naming package to provide functionality for accessing directory services in addition to naming services. This package allows applications to do the following:

  Search a directory for objects based on specific attributes or values
  Control and optimize the search results
  Modify attributes of an object

The directory package is an extension to the naming package and leverages the functionality of the naming context operations. The directory package extends this functionality by providing more sophisticated searching and modification facilities that reflect the nature of directory services and tend to be more complex than a simple naming service. However, all of the searching methods described for naming services also apply to directory services.

Search for Objects

In order to search for objects in a directory, first create an object that implements the javax.naming.directory.DirContext interface, such as the class InitialDirContext. Once the context has been initialized, the methods listed in Table 5.6 can be used to execute several types of searches. DirContext extends the Context interface. This means any directory object can also be treated as a naming context.

There are several techniques for searching using the methods of DirContext. The LDAP example configuration and the LDAP service provider are used for the examples in this chapter.

The code snippet that follows Table 5.6 defines a class DirectoryExample that accesses an LDAP directory server. The provider URL references an LDAP server by using the ldap: protocol. The host, MyHost, should be replaced with the name of your LDAP server. The example assumes that there is a context called JNDIExample in your directory server.

Table 5.6 DirContext Searching Methods That Return NamingEnumeration

METHOD USE

search(String name, Attributes attrs) or search(Name name, Attributes attrs) Searches a context for objects that contain a set of attributes.

search(String name, Attributes attrs, String[] returnAttrs) or search(Name name, Attributes attrs, String[] returnAttrs) Searches a context for objects that contain a set of attributes and retrieves the attributes specified in String[].

search(String name, String filter, SearchControls sc) or search(Name name, String filter, SearchControls sc) Searches in the named context for entries that satisfy the search filter.

search(String name, String filter, Object[] filterArgs, SearchControls sc) or search(Name name, String filter, Object[] filterArgs, SearchControls sc) Searches in the named context for entries that satisfy the search filter.

import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

public class DirectoryExample
{
    public static void main(String[] argc)
    {
        Hashtable env = new Hashtable(11);


        env.put(Context.INITIAL_CONTEXT_FACTORY, 
            “com.sun.jndi.ldap.LdapCtxFactory”);
        env.put(Context.PROVIDER_URL, 
            “ldap://MyHost/o=JNDIExample”);
        try 
        {
            DirContext dctx = new InitialDirContext(env);
...

The DirContext interface defines methods for examining and updating attributes associated with an object in the directory context. The rest of this section describes these search techniques in more detail.

Searching with Attributes

The simplest technique for searching with DirContext is to search for all objects that contain a certain set of attributes. To do this:

1.  Set the environment properties as you did for the Naming context.
2.  Create a DirContext as demonstrated in the beginning of the “Search for Objects” section.
3.  Specify the attributes you are looking for by defining javax.naming.directory .Attribute objects and storing them in a javax.naming.directory.Attributes array.
4.  Execute the search by calling the search method of DirContext.
5.  Process the results returned in a NamingEnumeration.

The directory package encapsulates an object’s values in a javax.naming.directory.Attributes object. The javax.naming.directory.BasicAttributes class provides an implementation of the Attributes interface. Table 5.7 lists the methods defined in the Attributes interface.


NOTE:  Don’t confuse Attributes, an interface for managing collections of attributes, with Attribute, an interface for an object that represents a single attribute from a directory service.

Attributes objects manage collections of objects that implement another interface called Attribute. Objects that implement the Attribute interface map Java objects to specific values from a directory service. The class javax.naming.directory.BasicAttribute implements the Attribute interface.


Previous Table of Contents Next