FileDocCategorySizeDatePackage
NamingShell.javaAPI DocExample6709Thu Dec 15 21:27:30 GMT 2005com.oreilly.jent.jndi

NamingShell

public class NamingShell extends Object
NamingShell: A command-line shell for driving the command examples.

Fields Summary
private static Hashtable
COMMAND_TABLE
private static String
PROMPT
private static String
VERSION
private static Context
CURRENT_CONTEXT
private static Context
INITIAL_CONTEXT
private static String
CURRENT_NAME
private static String
INITIAL_NAME
private static boolean
RUNNING
private static String
CMD_PKG
Constructors Summary
NamingShell(String[] args)

  
Methods Summary
private static voidexecute(Command c, java.util.Vector v)

    if (c == null) {
      System.out.println("No command was loaded; cannot execute the command.");
      return;
    }
    try {
      c.execute(CURRENT_CONTEXT, v);
    }
    catch (CommandException ce) {
      System.out.println(ce.getMessage());
    }
  
private static voidexecute(java.lang.String s, java.util.Vector v)

    execute(loadCommand(s), v);
  
private static voidexit(int status)


  // Shell operations
        System.exit(status); 
public static java.util.HashtablegetCommands()

 return COMMAND_TABLE; 
public static javax.naming.ContextgetCurrentContext()

 return CURRENT_CONTEXT; 
public static java.lang.StringgetCurrentName()

 return CURRENT_NAME; 
public static javax.naming.ContextgetInitialContext()

 return INITIAL_CONTEXT; 
public static java.lang.StringgetInitialName()

 return INITIAL_NAME; 
public static java.lang.StringgetPrompt()

 return PROMPT; 
public static CommandloadCommand(java.lang.String commandName)

    // The method returns a null command unless some of its 
    // internal logic assigns a new reference to it
    Command theCommand = null;
        
    // First see if the command is already present in the hashtable
    if (COMMAND_TABLE.containsKey(commandName)) {
      theCommand = (Command)COMMAND_TABLE.get(commandName);
      return theCommand;
    }

    try {
      // Here we use a little introspection to see if a class
      // implements Command before we instantiate it
      Class commandInterface = Class.forName(CMD_PKG + ".Command");
      Class commandClass = Class.forName(CMD_PKG + "." + commandName);
            
      // Check to see if the class is assignable from Command 
      // and if so, put the instance in the command table
      if (!(commandInterface.isAssignableFrom(commandClass))) 
        System.out.println("[" + commandName + "]: Not a command");
      else {
        theCommand = (Command)commandClass.newInstance();
        COMMAND_TABLE.put(commandName, theCommand);
      }
    }
    catch (ClassNotFoundException cnfe) {
      System.out.println("[" + commandName + "]: command not found");
    }
    catch (IllegalAccessException iae) {
      System.out.println("[" + commandName + "]: illegal access");
    }
    catch (InstantiationException ie) {
      System.out.println("["+commandName+"]: command couldn't be instantiated");
    }
    
    return theCommand;
  
public static voidmain(java.lang.String[] args)

    NamingShell shell = new NamingShell(args);
    NamingShell.readInput();
    System.out.println("Exiting");
  
private static voidreadInput()

    // Get the input from System.in
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        
    // Begin reading input
    try {
      while (RUNNING) {
        System.out.print(PROMPT + "% ");
                
        // Tokenize the line, read each token, and pass the token
        // into a convenient remaining arguments Vector that we
        // pass into the Command
        StringTokenizer tokenizer = new StringTokenizer(br.readLine());
        Vector remainingArgs = new Vector();
        String commandToken = "";
        if (tokenizer.hasMoreTokens()) {
          commandToken = tokenizer.nextToken();
          while (tokenizer.hasMoreTokens())
            remainingArgs.addElement(tokenizer.nextToken());
        }
                
        // Dynamically load the class for the appropriate command
        // based upon the case-sensitive name of the first token,
        // which is the command token
        if (!(commandToken.equals(""))) 
          execute(commandToken, remainingArgs);
      }
    }
    catch (java.io.IOException ioe) {
      System.out.println("Caught an IO exception reading a line of input");
    }
  
public static voidsetCurrentContext(javax.naming.Context ctx)

    CURRENT_CONTEXT = ctx; 
public static voidsetCurrentName(java.lang.String name)

    CURRENT_NAME = name;
    setPrompt(name);
  
public static voidsetInitialContext(javax.naming.Context ctx)

 
    INITIAL_CONTEXT = ctx; 
public static voidsetInitialName(java.lang.String name)

    INITIAL_NAME = name; 
public static voidsetPrompt(java.lang.String prompt)

 PROMPT = prompt;