FileDocCategorySizeDatePackage
Lookup.javaAPI DocExample2811Thu Dec 15 21:27:06 GMT 2005com.oreilly.jent.jndi

Lookup

public class Lookup extends Object
Lookup: Bind to a JNDI context and lookup a given object.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    String name = "";
    if (args.length > 0)
      name = args[0];

    try {
      // Create a Properties object and set default properties
      Properties props = new Properties();
      props.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
      props.put(Context.PROVIDER_URL, "file:///");
      
      // Optional command-line args to specify alternate factory and URL
      if (args.length > 1) {
        props.put(Context.INITIAL_CONTEXT_FACTORY, args[1]);
      }
      if (args.length > 2) {
        props.put(Context.PROVIDER_URL, args[2]);
      }

      // Create the initial context from the properties we just created
      Context initialContext = new InitialContext(props);

      // Look up the named object provided on the command-line
      Object obj = initialContext.lookup(name);
      if (name.equals(""))
        System.out.println("Looked up the initial context");
      else {
        System.out.println(name + " is bound to: " + obj
                           + " of type " + obj.getClass().getName());
        if (obj instanceof java.io.File) {
          System.out.println("It's a file");
        }
      }
    }
    catch (NamingException ne) {
      System.out.println("Encountered a naming exception");
      ne.printStackTrace();
    }