Generating Code With the JNDI Browser

After connecting to a Naming or Directory Service with the JNDI Browser, you can use the browser to generate code to use in your application.

Generating Look Up or Binding Code

  1. In the Explorer Window's runtime tab, use the browser nodes to locate the naming context or object you with to operate on.
  2. Right-click on the node that represents the naming context or object. On the context menu, choose either "Copy Lookup Code" or "Copy Binding Code:
  3. Switch back to the editing window and paste the generate code in the appropriate place.
Example of Binding Code

The following code was generated by the JNDI Module to bind an object (a PersistenceManagerFactory) in an LDAP Directory Service running on the local host at port 389.

/** Inserted by Jndi module */
java.util.Properties jndiProperties = new java.util.Properties();
jndiProperties.put("java.naming.provider.url","ldap://localhost:389/o=forte4java.com");
jndiProperties.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
try {
    javax.naming.directory.DirContext jndiCtx = new javax.naming.directory.InitialDirContext(jndiProperties);
    javax.naming.Context jndiObject = (javax.naming.Context)jndiCtx.lookup("");
    jndiObject.bind("<Name>",<Object>);
} catch (javax.naming.NamingException ne) {
    ne.printStackTrace();
}
To complete this code you need to specify a name and object:
/** Inserted by Jndi module */
java.util.Properties jndiProperties = new java.util.Properties();
jndiProperties.put("java.naming.provider.url","ldap://localhost:389/o=forte4java.com");
jndiProperties.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
try {
     // Create a PersistenceManagerFactory: (// dixie::1521, db name ORCL)
     PersistenceManagerFactory pmf = new PersistenceManagerFactoryImpl();
     pmf.setConnectionUserName("scott");
     pmf.setConnectionPassword("tiger");
     pmf.setConnectionDriverName("oracle.jdbc.driver.OracleDriver");
     pmf.setConnectionURL("jdbc:oracle:thin:@dixie:1521:ORCL");
     pmf.setOptimistic(true); //it is false by default

    javax.naming.directory.DirContext jndiCtx = new javax.naming.directory.InitialDirContext(jndiProperties);
    javax.naming.Context jndiObject = (javax.naming.Context)jndiCtx.lookup("");
    jndiObject.bind("cn=pmf_for_oracle", pmf);
} catch (javax.naming.NamingException ne) {
    ne.printStackTrace();
}

The object is bound under the name "pmf_for_oracle."

Example of Look Up Code

The following code was generated by the JNDI Module to look up an the object that was bound by the previous example:

/** Inserted by Jndi module */
java.util.Properties jndiProperties = new java.util.Properties();
jndiProperties.put("java.naming.provider.url","ldap://localhost:389/o=forte4java.com");
jndiProperties.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
try {
    javax.naming.directory.DirContext jndiCtx = new javax.naming.directory.InitialDirContext(jndiProperties);
    com.sun.forte4j.persistence.PersistenceManagerFactoryImpl jndiObject =      (com.sun.forte4j.persistence.PersistenceManagerFactoryImpl)jndiCtx.lookup("cn=pmf_for_oracle");
} catch (javax.naming.NamingException ne) {
    ne.printStackTrace();
}

Legal Notices