Providing JNDI context information

To use jConnect with JNDI, you should be familiar with the JNDI specification from Sun Microsystems.

In particular, you need to make sure that required initialization properties are set in javax.naming.directory.DirContext when JNDI and jConnect are used together. These properties can be set either at the system level or at runtime.

Two key properties are:

The following example shows how to set context properties at runtime and how to get a connection using JNDI and LDAP. In the example, the INITIAL_CONTEXT_FACTORY context property is set to invoke the Sun Microsystem implementation of an LDAP service provider. The PROVIDER_URL context property is set to the URL of an LDAP directory service located on the host “ldap_server1” at port 389.

Properties props = new Properties();
 
 /* We want to use LDAP, so INITIAL_CONTEXT_FACTORY is set to the
 * class name of an LDAP context factory. In this case, the 
 * context factory is provided by Sun’s implementation of a 
 * driver for LDAP directory service.
 */
 props.put(Context.INITIAL_CONTEXT_FACTORY,
   "com.sun.jndi.ldap.LdapCtxFactory");
 
 /* Now, we set PROVIDER_URL to the URL of the LDAP server that 
 * is to provide directory information for the connection.
 */
 props.put(Context.PROVIDER_URL, "ldap://ldap_server1:389");
 
 /* Set up additional context properties, as needed. */
 props.put("user", "xyz");
 props.put("password", "123");
 
 /* get the connection */
 Connection con = DriverManager.getConnection
   ("jdbc:sybase:jndi:ldap://ldap_server1:389" +
   "/servername=Sybase11,o=MyCompany,c=US",props); 

The connection string passed to getConnection contains LDAP-specific information, which the developer must provide.

When JNDI properties are set at runtime, as in the preceding example, jConnect passes them to JNDI to be used in initializing a server, as in the following jConnect code:

javax.naming.directory.DirContext ctx =
   new javax.naming.directory.InitialDirContext(props);

jConnect then obtains the connection information it needs from JNDI by invoking DirContext.getAtributes, as in the following example, where ctx is a DirContext object:

javax.naming.directory.Attributes attrs =
   ctx.getAttributes("ldap://ldap_server1:389/servername=" +
     "Sybase11", SYBASE_SERVER_ATTRIBUTES);

In the example, SYBASE_SERVER_ATTRIBUTES is an array of strings defined within jConnect. The array values are the OIDs for the required directory information listed in Table 2-3.