KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > console > CommandConsole


1 package org.sapia.console;
2
3 import java.io.*;
4
5
6 /**
7  * A command-line console that can be embedded in applications. The
8  * console parses command-lines to create <code>Command</code> instances
9  * that it executes.
10  * <p>
11  * An instance of this class takes a <code>CommandFactory</code> at
12  * construction, and delegates command object creation to it.
13  * <p>
14  * Embedding a command console is as shown below:
15  * <p>
16  * <pre>
17  * ReflectCommandFactory fac = new ReflectCommandFactory();
18  * fac.addPackage("org.mycommand.package");
19  * CommandConsole cons = new CommandConsole(fac);
20  * cons.start();
21  * </pre>
22  *
23  * @author Yanick Duchesne
24  * 29-Nov-02
25  */

26 public class CommandConsole extends Console {
27   private CommandFactory _fac;
28   private ConsoleListener _listener = new ConsoleListenerImpl();
29
30   /**
31    * Creates an instance of this class with the given factory.
32    *
33    * @param fac A <code>CommandFactory</code>.
34    */

35   public CommandConsole(CommandFactory fac) {
36     super();
37     _fac = fac;
38   }
39
40   /**
41    * Creates an instance of this class with the given factory. The
42    * input and output streams passed in are used internally for
43    * command-line reading and display output respectively.
44    *
45    * @param a <code>CommandFactory</code>.
46    * @param in The input stream of the console.
47    * @param out The output stream of the console.
48    * @param fac A <code>CommandFactory</code>.
49 >>>>>>> 1.6
50    */

51   public CommandConsole(InputStream in, OutputStream out, CommandFactory fac) {
52     super(in, out);
53     _fac = fac;
54   }
55
56   /**
57    * Sets this instance's command listener.
58    */

59   public void setCommandListener(ConsoleListener listener) {
60     _listener = listener;
61   }
62
63   /**
64    * Starts this console in the current thread and loops indefinily on
65    * input/display until an <code>AbortException</code> is thrown by a
66    * <code>Command</code> instance.
67    */

68   public void start() {
69     int idx = 0;
70     String JavaDoc line;
71     String JavaDoc name = null;
72     String JavaDoc args = null;
73     _listener.onStart(this);
74
75     while (true) {
76       try {
77         prompt();
78         line = readLine();
79
80         if (line.length() == 0) {
81           continue;
82         } else {
83           CmdLine cmdLine = CmdLine.parse(line);
84
85           if (cmdLine.size() == 0) {
86             continue;
87           }
88
89           if (cmdLine.isNextArg()) {
90             Command cmd = _fac.getCommandFor(name = cmdLine.chopArg().getName());
91             Context ctx = newContext();
92             ctx.setUp(this, cmdLine);
93             cmd.execute(ctx);
94           } else {
95             println("Command name expected");
96           }
97         }
98       } catch (InputException e) {
99         this.println(e.getMessage());
100       } catch (AbortException e) {
101         _listener.onAbort(this);
102
103         break;
104       } catch (IOException e) {
105         e.printStackTrace();
106
107         break;
108       } catch (CommandNotFoundException e) {
109         _listener.onCommandNotFound(this, name);
110       }
111     }
112   }
113
114   /**
115    * Template method internally called by this instance to create
116    * new <code>Context</code> instances.
117    */

118   protected Context newContext() {
119     return new Context();
120   }
121
122   // public static void main(String[] args) {
123
// try{
124
// ReflectCommandFactory fac = new ReflectCommandFactory();
125
// fac.addPackage("org.sapia.console");
126
// CommandConsole cons = new CommandConsole(fac);
127
// cons.start();
128
//
129
// }catch(Throwable t){
130
// t.printStackTrace();
131
// }
132
// }
133
}
134
Popular Tags