What's in a Name?

 


String Names versus Structured Names

Each naming method in the Context and DirContext interfaces has two overloaded forms: one that accepts a string name (java.lang.String) and one that accepts a structured name ( Name). For example, lookup() has two forms:
lookup(java.lang.String)
lookup(javax.naming.Name)

 

 

String Names

The overloads that accept a java.lang.String name are convenience forms that allow the methods to be invoked without the caller's having to construct a CompositeName instance. The java.lang.String name argument passed to these overloads represents a composite name and follows the syntactic rules specified in the CompositeName class.

For example, the following two invocations of lookup() are equivalent.

Object obj1 = ctx.lookup("cn=Ted Geisel, ou=People, o=JNDIDocs");

CompositeName cname = new CompositeName(
    "cn=Ted Geisel, ou=People, o=JNDIDocs");
Object obj2 = ctx.lookup(cname);

 

 

Structured Names

The overloads that accept a Name accept an instance of CompositeName, CompoundName, or any other class that implements the Name interface.

If the object is an instance of CompositeName, then it is treated as a composite name. A composite name is a name that can span multiple naming systems, not just the naming system on which the method is invoked. See the Composite Names section of this lesson for examples of how to use composite names. In the degenerate case, the name spans only the naming system on which the method is invoked. In the Tips for LDAP Users trail, for instance, all of the examples use string names from the LDAP namespace. Note that even in this degenerate case, the name is still a composite name; that is, it is a composite name having a single component.

If the object is not an instance of CompositeName, then it is treated as a compound name--a name that spans only one namespace. See the Compound Names section of this lesson for examples of how to use compound names.

 

 

When to Use Which

Typically, your program would use a string name if it was supplied a string name by the user. It would use a Name instance if it was composing the name by using input from the user. For example, in a namespace browser application, as the user traverses different parts of the namespace, the application might need to compose names that direct the browser toward those parts of the namespace. It could do so by creating string representations of the names for those parts of the namespace and by being cognizant of and using the appropriate naming syntax(es). Or, it could use Name instances created with the help of utilities provided by the JNDI.

See Handling Special Characters section of this lesson about how best to accommodate special characters in a name that conflicts with the JNDI composite name syntax.