00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.console.text.module;
00026
00027 import java.util.ArrayList;
00028 import java.util.Hashtable;
00029 import java.util.Iterator;
00030 import java.util.LinkedList;
00031 import java.util.NoSuchElementException;
00032 import java.util.StringTokenizer;
00033 import java.util.TreeSet;
00034
00035 import jline.SimpleCompletor;
00036
00037 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
00038 import org.objectweb.cjdbc.console.text.Console;
00039 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand;
00040 import org.objectweb.cjdbc.console.text.commands.Help;
00041 import org.objectweb.cjdbc.console.text.commands.History;
00042 import org.objectweb.cjdbc.console.text.commands.Quit;
00043
00044
00045
00046
00047
00048
00049
00050
00051 public abstract class AbstractConsoleModule
00052 {
00053 Console console;
00054 TreeSet commands;
00055 boolean quit;
00056 LinkedList history;
00057 private static final int HISTORY_MAX = 10;
00058
00059
00060
00061
00062
00063
00064 public AbstractConsoleModule(Console console)
00065 {
00066 this.console = console;
00067 this.commands = new TreeSet();
00068 this.history = new LinkedList();
00069 commands.add(new Help(this));
00070 commands.add(new History(this));
00071 commands.add(new Quit(this));
00072 console.println(ConsoleTranslate.get("module.loading",
00073 getDescriptionString()));
00074 this.loadCommands();
00075 this.loadCompletor();
00076 }
00077
00078
00079
00080
00081 protected abstract void loadCommands();
00082
00083 protected void loadCompletor()
00084 {
00085 int size = commands.size();
00086 if (size <= 0)
00087 return;
00088 Iterator iter = commands.iterator();
00089 ArrayList list = new ArrayList(size);
00090 while (iter.hasNext())
00091 {
00092 list.add(((ConsoleCommand) iter.next()).getCommandName());
00093 }
00094 console.getConsoleReader().addCompletor(
00095 new SimpleCompletor((String[]) list.toArray(new String[size])));
00096 }
00097
00098
00099
00100
00101
00102
00103 public abstract String getDescriptionString();
00104
00105
00106
00107
00108 public void help()
00109 {
00110 console.println(ConsoleTranslate.get("module.commands.available",
00111 getDescriptionString()));
00112 ConsoleCommand command;
00113 Iterator it = commands.iterator();
00114 while (it.hasNext())
00115 {
00116 command = (ConsoleCommand) it.next();
00117 console.printInfo(command.getCommandName() + " "
00118 + command.getCommandParameters());
00119 console.println(" " + command.getCommandDescription());
00120 }
00121 }
00122
00123
00124
00125
00126 public void quit()
00127 {
00128 this.quit = true;
00129 }
00130
00131
00132
00133
00134
00135
00136 public TreeSet getCommands()
00137 {
00138 return commands;
00139 }
00140
00141
00142
00143
00144
00145
00146 public abstract String getPromptString();
00147
00148
00149
00150
00151 public void handlePrompt()
00152 {
00153 if (quit)
00154 {
00155 console.printError(ConsoleTranslate.get("module.quitting",
00156 getDescriptionString()));
00157 return;
00158 }
00159
00160 Hashtable hashCommands = getHashCommands();
00161
00162
00163 quit = false;
00164 while (!quit)
00165 {
00166 try
00167 {
00168 String commandLine = console.readLine(getPromptString());
00169 if (commandLine == null)
00170 {
00171 quit = true;
00172 break;
00173 }
00174 if (commandLine.equals(""))
00175 continue;
00176 else
00177 manageHistory(commandLine);
00178
00179 handleCommandLine(commandLine, hashCommands);
00180
00181 }
00182 catch (Exception e)
00183 {
00184 console.printError(ConsoleTranslate.get("module.command.got.error",
00185 new Object[]{e.getClass(), e.getMessage()}), e);
00186 }
00187 }
00188 }
00189
00190
00191
00192
00193
00194
00195 public final Hashtable getHashCommands()
00196 {
00197 Hashtable hashCommands = new Hashtable();
00198 ConsoleCommand consoleCommand;
00199 Iterator it = commands.iterator();
00200 while (it.hasNext())
00201 {
00202 consoleCommand = (ConsoleCommand) it.next();
00203 hashCommands.put(consoleCommand.getCommandName(), consoleCommand);
00204 }
00205 return hashCommands;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 public final void handleCommandLine(String commandLine, Hashtable hashCommands)
00216 throws Exception
00217 {
00218 ConsoleCommand consoleCommand;
00219 StringTokenizer st = new StringTokenizer(commandLine);
00220 if (!st.hasMoreTokens())
00221 {
00222 console.printError(ConsoleTranslate.get("module.command.not.supported",
00223 ""));
00224 return;
00225 }
00226 String command = st.nextToken();
00227 consoleCommand = null;
00228 try
00229 {
00230 consoleCommand = (ConsoleCommand) hashCommands.get(command);
00231 }
00232 catch (NoSuchElementException e)
00233 {
00234 consoleCommand = null;
00235 }
00236 if (consoleCommand == null)
00237 {
00238 console.printError(ConsoleTranslate.get("module.command.not.supported",
00239 command));
00240 }
00241 else
00242 {
00243 consoleCommand.execute(commandLine.substring(command.length()));
00244 }
00245 }
00246
00247
00248
00249
00250
00251
00252
00253 public final void manageHistory(String command)
00254 {
00255 history.add(command);
00256 if (history.size() > HISTORY_MAX)
00257 history.removeFirst();
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 public abstract void login(String[] params) throws Exception;
00267
00268
00269
00270
00271
00272
00273 public Console getConsole()
00274 {
00275 return console;
00276 }
00277
00278
00279
00280
00281
00282
00283 public LinkedList getHistory()
00284 {
00285 return history;
00286 }
00287 }