![]() ![]() ![]() ![]() |
What's in a Name? |
A naming convention, such as that for the LDAP or the file system, typically has characters, called meta characters, that have special meaning. For example, in the LDAP, if one of the following characters appear in the name, it must be preceded by the escape character (which is the backslash character):When you are specifying a name to one of the Context
- a space or "#" character occurring at the beginning of the string
- a space character occurring at the end of the string
- one of the characters ",", "+", """, "\", "<", ">" or ";"
methods, in addition to paying attention to the special characters and naming convention of the underlying naming system, you also need to be concerned about the JNDI composite name syntax, which also defines special characters. The combination of the two syntaxes might lead to many levels of escaping.
For example, suppose you have an LDAP name that contains a backslash character.
Because the LDAP requires that the backslash character be escaped, you must precede the backslash with another backslash:cn=backslash\aBecause the backslash character is also a special character in the JNDI, if you supply this string name as a composite name, you must escape the backslashes, again, by preceding each with a backslash:cn=backslash\\aIf you specify this as a literal in the Java programming language, the Java language requires that a backslash within a string literal be escaped with yet another backslash:cn=backslash\\\\aString cname = "cn=backslash\\\\\\\\a";
String Names are Composite Names
The important point to keep in mind is that the string names that you pass to the Context methods are composite names. To avoid any surprises should the name contain special characters that might conflict with the JNDI composite name syntax, you should use the Context methods that accept a Name. There are two ways to do this. The first way is to use a CompositeName
. You create a CompositeName object and then append the naming system-specific name (such as an LDAP name) to it. Here is an example:
If you apply this technique to the LDAP sample name above, you eliminate the need to add escapes for the JNDI syntax manually because it is taken care of automatically by the CompositeName class.String dn = ...; // an LDAP distinguished name Name composite = new CompositeName().add(dn); Object obj = ctx.lookup(composite);Name composite = new CompositeName().add("cn=backslash\\\\a"); Object obj = ctx.lookup(composite);The second way is to use a compound name. You create a compound name by parsing the naming system-specific name (such as an LDAP name). Here is an example:
If you apply this technique to the LDAP sample name above, you eliminate the need to add escapes for the JNDI syntax because you are not using JNDI composite names.String dn = ...; // an LDAP distinguished name NameParser ldapParser = ctx.getNameParser(""); Name compound = ldapParser.parse(dn); Object obj = ctx.lookup(compound);Name compound = ldapParser.parse("cn=backslash\\\\a"); Object obj = ctx.lookup(compound);
![]() ![]() ![]() ![]() |
What's in a Name? |