|
ShellCommandTable |
|
1 // joi/7/juno/ShellCommandTable.java
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 import java.util.*;
7
8 /**
9 * A ShellCommandTable object maintains a dispatch table of
10 * ShellCommand objects keyed by the command names used to invoke
11 * them.
12 *
13 * To add a new shell command to the table, install it from
14 * method fillTable().
15 *
16 * @see ShellCommand
17 *
18 * @version 7
19 */
20
21 public class ShellCommandTable
22 {
23 private Map table = new TreeMap();
24
25 /**
26 * Construct and fill a shell command table.
27 */
28
29 public ShellCommandTable()
30 {
31 fillTable();
32 }
33
34 /**
35 * Get a ShellCommand, given the command name key.
36 *
37 * @param key the name associated with the command we're
38 * looking for.
39 *
40 * @return the command we're looking for, null if none.
41 */
42
43 public ShellCommand lookup( String key )
44 {
45 ShellCommand commandObject = (ShellCommand) table.get( key );
46 if (commandObject != null) {
47 return commandObject;
48 }
49
50 // try to load dynamically
51 // construct classname = "KeyCommand"
52 char[] chars = (key + "Command").toCharArray();
53 chars[0] = key.toUpperCase().charAt(0);
54 String classname = new String(chars);
55 try {
56 commandObject =
57 (ShellCommand)Class.forName(classname).newInstance();
58 }
59 catch (Exception e) { // couldn't find class
60 return null;
61 }
62 install(key, commandObject); // put it in table for next time
63 return commandObject;
64 }
65
66 /**
67 * Get an array of the command names.
68 *
69 * @return the array of command names.
70 */
71
72 public String[] getCommandNames()
73 {
74 return (String[]) table.keySet().toArray( new String[0] );
75 }
76
77 // Associate a command name with a ShellCommand.
78
79 private void install( String commandName, ShellCommand command )
80 {
81 table.put( commandName, command );
82 }
83
84 // Fill the dispatch table with ShellCommands, keyed by their
85 // command names.
86
87 private void fillTable()
88 {
89 install( "list", new ListCommand() );
90 install( "cd", new CdCommand() );
91 install( "newfile", new NewfileCommand() );
92 install( "remove", new RemoveCommand() );
93 install( "help", new HelpCommand() );
94 install( "mkdir", new MkdirCommand() );
95 install( "type", new TypeCommand() );
96 install( "logout", new LogoutCommand() );
97 }
98 }
99
|
ShellCommandTable |
|