package com.oreilly.jent.jndi;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

/**
 * initctx: Command to initialize the JNDI context.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class initctx implements Command {
    
  public void execute(Context c, Vector v) {
    String jndiPropsFilename = null;

    // Check for a properties filename
    if (!v.isEmpty())
      jndiPropsFilename = (String)v.firstElement();

    System.out.println("file = " + jndiPropsFilename);
            
    try {
      // If no properties file is specified, let JNDI get its properties from
      // the default jndi.properties file on the CLASSPATH.  Otherwise, use the
      // specified properties file.
      if (jndiPropsFilename != null) {
        Properties props = new Properties();
        File jndiProps = new File(jndiPropsFilename);
        props.load(new FileInputStream(jndiProps));
           
        NamingShell.setInitialContext(new InitialContext(props));
      }
      else {
        NamingShell.setInitialContext(new InitialContext());
      }
      NamingShell.setInitialName("/");
      NamingShell.setCurrentContext(NamingShell.getInitialContext());
      NamingShell.setCurrentName(NamingShell.getInitialName());
      System.out.print("Created initial context using ");
      if (jndiPropsFilename != null) {
        System.out.println(jndiPropsFilename);
      }
      else {
        System.out.println("jndi.properties.");
      }
    }
    catch (NamingException ne) {
      System.out.println("Couldn't create the initial context");
    }
    catch (FileNotFoundException fnfe) {
      System.out.print("Couldn't find properties file: ");
      System.out.println(jndiPropsFilename);
    }
    catch (IOException ioe) {
      System.out.print("Problem loading the properties file: ");
      System.out.println(jndiPropsFilename);
    }
    catch (Exception e) {
      System.out.println("There was a problem starting the shell");
    }
  }
    
  public void help() { System.out.println("Usage: initctx [filename]"); }
}
