Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

JNDI overview, Part 4: the Doc-u-Matic, a JNDI application

Pull together your JNDI knowledge with a JNDI-enabled application

  • Print
  • Feedback

Page 5 of 6

  /**
   * Creates an object instance given a reference.
   *
   * @param object a reference
   * @param name a name
   * @param context the context
   * @param hashtable the environment
   *
   * @returns an object, or null
   *
   */
  public
  Object
  getObjectInstance(Object object, Name name, Context context, Hashtable 
hashtable)
  throws Exception {
    // Checks whether or not the object is an instance of a reference.
    if (object instanceof Reference) {
      Reference reference = (Reference)object;
      // Checks whether or not it is a valid reference for this factory.
      if (reference.getClassName().equals(JNDILibrary.class.getName())) {
        // Gets the stored payload.
        RefAddr refaddr = reference.get("properties");
        if (refaddr != null) {
          if (refaddr instanceof BinaryRefAddr) {
            BinaryRefAddr binaryrefaddr = (BinaryRefAddr)refaddr;
            // Recreates the stored properties.
            byte [] rgb = (byte [])binaryrefaddr.getContent();
            ByteArrayInputStream bytearrayinputstream = new 
ByteArrayInputStream(rgb);
            Properties properties = new Properties();
            properties.load(bytearrayinputstream);
            // Creates a new JNDI library with the stored properties.
            return new JNDILibrary(properties);
          }
        }
      }
    }
    return null;
  }


The getObjectInstance() and the getReference() methods, provided by the JNDILibrary class, work as a pair.

JNDIDeploy.java

JNDIDeploy class deploys or creates a new library. The body implements as close to a textbook example of binding via JNDI as you're likely to find:

  /**
   * Deploys a JNDILibrary instance.
   *
   * This class should be run as an application.  It deploys a
   * JNDILibrary instance.  It assumes the existence of a
   * properly configured JNDI service (typically, but not necessarily,
   * running LDAP).
   *
   * The application requires a single command-line argument:
   * the name of a file containing the deployment properties.  The
   * properties file may contain any valid JNDI property, as well as the
   * property "library.name", which must contain the name of the
   * library.
   *
   */
  public
  static
  void
  main(String [] rgstring) {
    if (rgstring.length != 1) {
      System.out.println("Usage: java JNDIDeploy < properties file >");
      System.exit(-1);
    }
    try {
      Properties properties = new Properties();
      // Load the properties from the specified file.
      properties.load(new FileInputStream(rgstring[0]));
      // Use the properties to establish an initial directory context.
      DirContext dircontext = new InitialDirContext(properties);
      // Create the JNDI library instance and initialize it with the
      // same set of properties (it will publish objects in the same
      // context as itself).
      JNDILibrary jndilibrary = new JNDILibrary(properties);
      // Get the name of the library.
      String stringName = properties.getProperty("library.name");
      // Bind the library to the specified name.
      dircontext.rebind(stringName, jndilibrary, null);
      // Close the context.
      dircontext.close();
    }
    catch (Exception exception) {
      exception.printStackTrace();
      System.exit(-1);
    }
  }


Publish.java

The Publish class publishes one or more objects in a library. Once again, you're not likely to find a more textbook example of how to look up an object via JNDI -- in this case the library. Once you've retrieved the library, it can be used to publish objects. Notice how the code doesn't have to know anything about how the library is implemented. You'll also notice that the Beans.instantiate() method instantiates the objects, which facilitates the storage of objects that have been put together in the JavaBeans development tool and serialized to disk:

  • Print
  • Feedback

Resources