Previous | Next | Trail Map | Building a Service Provider | The Essential Components

Names

Almost all methods in the Context(in the API reference documentation) interface must process names. For consistency, you should use the same pattern in each method to process names. One way to achieve this conveniently is to define and use a utility method to process the input name. Two examples are given here, one for a context implementation of a flat namespace and one for a context implementation of a hierarchical namespace. Whether a context implementation supports a flat namespace or a hierarchical namespace is typically determined by the underlying naming/directory service or the requirements of the service provider.

As recommended in The Ground Rules (in the Building a Service Provider trail) lesson, for both examples, each method in the Context interface that accepts a java.lang.String name is defined simply to call its counterpart that accepts a Name(in the API reference documentation). The rest of this discussion focuses on dealing with structured names.

A Flat Namespace Example

The first example implements a flat namespace and does not support federation. The methods that accept a Name use the following utility method to determine the components of the name that should be processed.
protected String getMyComponents(Name name) throws NamingException {
    if (name instanceof CompositeName) {
        if (name.size() > 1) {
	    throw new InvalidNameException(name.toString() +
		" has more components than namespace can handle");
	}
	return name.get(0);
    } else {
	// A compound name
	return name.toString();
    }
}
Because the context does not support federation, it accepts only single-component composite names or compound names. If it receives a multicomponent composite name, then it throws an InvalidNameException(in the API reference documentation).

A Hierarchical Namespace Example

The second example implements a hierarchical namespace and does not support federation. The methods that accept a Name use the following utility method to determine the components of the name that should be processed.
protected Name getMyComponents(Name name) throws NamingException {
    if (name instanceof CompositeName) {
	if (name.size() > 1) {
	    throw new InvalidNameException(name.toString() +
		" has more components than namespace can handle");
	}

	// Turn component that belongs to you into a compound name
	return myParser.parse(name.get(0));
    } else {
	// Already parsed
	return name;
    }
}
Unlike in the flat namespace example, this method returns a Name that represents a structured compound name instead of a java.lang.String. When the method gets a CompositeName(in the API reference documentation), it parses the first component into a compound name and returns it.


Previous | Next | Trail Map | Building a Service Provider | The Essential Components