00001
00025 package org.objectweb.cjdbc.console.text.module;
00026
00027
import java.util.Hashtable;
00028
import java.util.Iterator;
00029
import java.util.LinkedList;
00030
import java.util.NoSuchElementException;
00031
import java.util.StringTokenizer;
00032
import java.util.TreeSet;
00033
00034
import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
00035
import org.objectweb.cjdbc.console.text.ColorPrinter;
00036
import org.objectweb.cjdbc.console.text.Console;
00037
import org.objectweb.cjdbc.console.text.commands.ConsoleCommand;
00038
import org.objectweb.cjdbc.console.text.commands.Help;
00039
import org.objectweb.cjdbc.console.text.commands.History;
00040
import org.objectweb.cjdbc.console.text.commands.Quit;
00041
00049 public abstract class AbstractConsoleModule
00050 {
00051 Console console;
00052 TreeSet
commands;
00053 boolean quit;
00054 LinkedList
history;
00055 private final static int HISTORY_MAX = 10;
00056
00062 public AbstractConsoleModule(
Console console)
00063 {
00064
this.console = console;
00065
this.commands =
new TreeSet();
00066
this.history =
new LinkedList();
00067
commands.add(
new Help(
this));
00068
commands.add(
new History(
this));
00069
commands.add(
new Quit(
this));
00070 console.
println(
ConsoleTranslate.get(
"module.loading",
00071
getDescriptionString()));
00072
this.loadCommands();
00073 }
00074
00078
protected abstract void loadCommands();
00079
00085
public abstract String
getDescriptionString();
00086
00090 public void help()
00091 {
00092
console.
println(
ConsoleTranslate.get(
"module.commands.available",
00093
getDescriptionString()));
00094
ConsoleCommand command;
00095 Iterator it =
commands.iterator();
00096
while (it.hasNext())
00097 {
00098 command = (
ConsoleCommand) it.next();
00099
console.
println(command.
getCommandName() +
" "
00100 + command.
getCommandParameters(),
ColorPrinter.INFO);
00101
console.
println(
" " + command.
getCommandDescription());
00102 }
00103 }
00104
00108 public void quit()
00109 {
00110
this.quit =
true;
00111 }
00112
00118 public TreeSet
getCommands()
00119 {
00120
return commands;
00121 }
00122
00128
public abstract String
getPromptString();
00129
00133 public void handlePrompt()
00134 {
00135
if (
quit)
00136 {
00137
console.
printError(
ConsoleTranslate.get(
"module.quitting",
00138
getDescriptionString()));
00139
return;
00140 }
00141
00142 Hashtable hashCommands =
getHashCommands();
00143
00144
00145
quit =
false;
00146
while (!
quit)
00147 {
00148
try
00149 {
00150 String commandLine =
console.
readLine(
getPromptString());
00151
if (commandLine == null)
00152 {
00153
quit =
true;
00154
break;
00155 }
00156
if (commandLine.equals(
""))
00157
continue;
00158
else
00159
manageHistory(commandLine);
00160
00161
handleCommandLine(commandLine,hashCommands);
00162
00163 }
00164
catch (Exception e)
00165 {
00166
console.
printError(
ConsoleTranslate.get(
"module.command.got.error",
00167
new Object[]{e.getClass(), e.getMessage()}), e);
00168 }
00169 }
00170 }
00171
00177 public final Hashtable getHashCommands()
00178 {
00179 Hashtable hashCommands =
new Hashtable();
00180
ConsoleCommand consoleCommand;
00181 Iterator it = commands.iterator();
00182
while (it.hasNext())
00183 {
00184 consoleCommand = (
ConsoleCommand) it.next();
00185 hashCommands.put(consoleCommand.
getCommandName(), consoleCommand);
00186 }
00187
return hashCommands;
00188 }
00189
00197 public final void handleCommandLine(String commandLine,Hashtable hashCommands)
throws Exception
00198 {
00199
ConsoleCommand consoleCommand;
00200 StringTokenizer st =
new StringTokenizer(commandLine);
00201
if (!st.hasMoreTokens())
00202 {
00203 console.printError(
ConsoleTranslate.get(
00204
"module.command.not.supported",
""));
00205
return;
00206 }
00207 String command = st.nextToken();
00208 consoleCommand = null;
00209
try
00210 {
00211 consoleCommand = (
ConsoleCommand) hashCommands.get(command);
00212 }
00213
catch (NoSuchElementException e)
00214 {
00215 consoleCommand = null;
00216 }
00217
if (consoleCommand == null)
00218 {
00219 console.printError(
ConsoleTranslate.get(
00220
"module.command.not.supported", command));
00221 }
00222
else
00223 {
00224 consoleCommand.
execute(commandLine.substring(command.length()));
00225 }
00226 }
00227
00234 public final void manageHistory(String command)
00235 {
00236 history.add(command);
00237
if(history.size()>HISTORY_MAX)
00238 history.removeFirst();
00239 }
00240
00247
public abstract void login(String[] params)
throws Exception;
00248
00254 public Console getConsole()
00255 {
00256
return console;
00257 }
00263 public LinkedList getHistory()
00264 {
00265
return history;
00266 }
00267 }