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.

To generate look up or binding code:

  1. In the Runtime tab of the Explorer window, use the browser nodes to locate the naming context or object you want to operate on.
  2. Right-click the node that represents the naming context or object. From the contextual menu, choose Copy Lookup Code or Copy Binding Code.
  3. In the Source Editor, paste the generated 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 lookup 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();
}
See also
JNDI Browser Commands

Legal Notices