1 24 25 package org.objectweb.cjdbc.console.text.module; 26 27 import java.util.Arrays ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.StringTokenizer ; 34 import java.util.TreeSet ; 35 import java.util.prefs.Preferences ; 36 37 import jline.ArgumentCompletor; 38 import jline.Completor; 39 import jline.FileNameCompletor; 40 import jline.SimpleCompletor; 41 42 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate; 43 import org.objectweb.cjdbc.console.text.Console; 44 import org.objectweb.cjdbc.console.text.commands.ConsoleCommand; 45 import org.objectweb.cjdbc.console.text.commands.Help; 46 import org.objectweb.cjdbc.console.text.commands.History; 47 import org.objectweb.cjdbc.console.text.commands.Quit; 48 49 56 public abstract class AbstractConsoleModule 57 { 58 private static final int HISTORY_MAX = 10; 59 60 Console console; 61 62 TreeSet commands; 63 64 boolean quit = false; 65 66 LinkedList history; 67 68 protected Completor consoleCompletor; 69 70 75 public AbstractConsoleModule(Console console) 76 { 77 this.console = console; 78 this.commands = new TreeSet (); 79 this.history = new LinkedList (); 80 commands.add(new Help(this)); 81 commands.add(new History(this)); 82 commands.add(new Quit(this)); 83 if (console.isInteractive()) 84 console.println(ConsoleTranslate.get("module.loading", 85 getDescriptionString())); 86 this.loadCommands(); 87 this.loadCompletor(); 88 } 89 90 93 protected abstract void loadCommands(); 94 95 98 protected void loadCompletor() 99 { 100 List completors = new LinkedList (); 101 int size = commands.size(); 102 if (size > 0) 103 { 104 TreeSet set = new TreeSet (); 105 Iterator it = commands.iterator(); 106 while (it.hasNext()) 107 { 108 set.add(((ConsoleCommand) it.next()).getCommandName()); 109 } 110 completors.add(new SimpleCompletor((String []) set 111 .toArray(new String [size]))); 112 } 113 completors.add(new FileNameCompletor()); 114 115 Completor[] completorsArray = (Completor[]) completors 116 .toArray(new Completor[completors.size()]); 117 consoleCompletor = new ArgumentCompletor(completorsArray, 118 new CommandDelimiter()); 119 } 120 121 125 protected synchronized void reloadCompletor() 126 { 127 console.getConsoleReader().removeCompletor(consoleCompletor); 128 loadCompletor(); 129 console.getConsoleReader().addCompletor(consoleCompletor); 130 } 131 132 137 public abstract String getDescriptionString(); 138 139 142 public void help() 143 { 144 console.println(ConsoleTranslate.get("module.commands.available", 145 getDescriptionString())); 146 ConsoleCommand command; 147 Iterator it = commands.iterator(); 148 while (it.hasNext()) 149 { 150 command = (ConsoleCommand) it.next(); 151 console.printInfo(command.getCommandName() + " " 152 + command.getCommandParameters()); 153 console.println(" " + command.getCommandDescription()); 154 } 155 } 156 157 160 public void quit() 161 { 162 quit = true; 163 storeHistory(); 164 console.getConsoleReader().removeCompletor(getCompletor()); 165 console.getConsoleReader().addCompletor( 166 console.getControllerModule().getCompletor()); 167 } 168 169 172 protected void loadHistory() 173 { 174 try 175 { 176 Preferences prefs = Preferences.userRoot() 177 .node(this.getClass().getName()); 178 String [] historyKeys = prefs.keys(); 179 Arrays.sort(historyKeys, 0, historyKeys.length); 180 for (int i = 0; i < historyKeys.length; i++) 181 { 182 String key = historyKeys[i]; 183 String value = prefs.get(key, ""); 184 manageHistory(value); 185 } 186 } 187 catch (Exception e) 188 { 189 } 191 } 192 193 196 protected void storeHistory() 197 { 198 try 199 { 200 Preferences prefs = Preferences.userRoot() 201 .node(this.getClass().getName()); 202 prefs.clear(); 203 for (int i = 0; i < history.size(); i++) 204 { 205 prefs.put(String.valueOf(i), (String ) history.get(i)); 206 } 207 prefs.flush(); 208 } 209 catch (Exception e) 210 { 211 } 213 } 214 215 220 public TreeSet getCommands() 221 { 222 return commands; 223 } 224 225 230 public abstract String getPromptString(); 231 232 235 public void handlePrompt() 236 { 237 238 loadHistory(); 239 240 if (quit) 241 { 242 if (console.isInteractive()) 243 console.printError(ConsoleTranslate.get("module.quitting", 244 getDescriptionString())); 245 return; 246 } 247 248 quit = false; 250 while (!quit) 251 { 252 253 Hashtable hashCommands = getHashCommands(); 254 try 255 { 256 String commandLine = console.readLine(getPromptString()); 257 if (commandLine == null) 258 { 259 quit = true; 260 break; 261 } 262 if (commandLine.equals("")) 263 continue; 264 else 265 manageHistory(commandLine); 266 267 handleCommandLine(commandLine, hashCommands); 268 269 } 270 catch (Exception e) 271 { 272 console.printError(ConsoleTranslate.get("module.command.got.error", 273 new Object []{e.getClass(), e.getMessage()}), e); 274 } 275 } 276 } 277 278 283 public final Hashtable getHashCommands() 284 { 285 Hashtable hashCommands = new Hashtable (); 286 ConsoleCommand consoleCommand; 287 Iterator it = commands.iterator(); 288 while (it.hasNext()) 289 { 290 consoleCommand = (ConsoleCommand) it.next(); 291 hashCommands.put(consoleCommand.getCommandName(), consoleCommand); 292 } 293 return hashCommands; 294 } 295 296 303 public final void handleCommandLine(String commandLine, Hashtable hashCommands) 304 throws Exception 305 { 306 StringTokenizer st = new StringTokenizer (commandLine); 307 if (!st.hasMoreTokens()) 308 { 309 console.printError(ConsoleTranslate.get("module.command.not.supported", 310 "")); 311 return; 312 } 313 314 ConsoleCommand consoleCommand = findConsoleCommand(commandLine, 315 hashCommands); 316 if (consoleCommand == null) 317 { 318 console.printError(ConsoleTranslate.get("module.command.not.supported", 319 commandLine)); 320 } 321 else 322 { 323 consoleCommand.execute(commandLine.substring(consoleCommand 324 .getCommandName().length())); 325 } 326 } 327 328 341 public ConsoleCommand findConsoleCommand(String commandLine, 342 Hashtable hashCommands) 343 { 344 ConsoleCommand foundCommand = null; 345 for (Iterator iter = hashCommands.entrySet().iterator(); iter.hasNext();) 346 { 347 Map.Entry commandEntry = (Map.Entry ) iter.next(); 348 String commandName = (String ) commandEntry.getKey(); 349 if (commandLine.startsWith(commandName)) 350 { 351 ConsoleCommand command = (ConsoleCommand) commandEntry.getValue(); 352 if (foundCommand == null) 353 { 354 foundCommand = command; 355 } 356 if (command.getCommandName().length() > foundCommand.getCommandName().length()) 357 { 358 foundCommand = command; 359 } 360 } 361 } 362 return foundCommand; 363 } 364 365 371 public final void manageHistory(String command) 372 { 373 history.add(command); 374 if (history.size() > HISTORY_MAX) 375 history.removeFirst(); 376 } 377 378 384 public abstract void login(String [] params) throws Exception ; 385 386 391 public Console getConsole() 392 { 393 return console; 394 } 395 396 401 public LinkedList getHistory() 402 { 403 return history; 404 } 405 406 411 public Completor getCompletor() 412 { 413 return consoleCompletor; 414 } 415 416 420 class CommandDelimiter extends ArgumentCompletor.AbstractArgumentDelimiter 421 { 422 426 public boolean isDelimiterChar(String buffer, int pos) 427 { 428 String tentativeCmd = buffer.substring(0, pos); 429 return isACompleteCommand(tentativeCmd); 430 } 431 432 440 private boolean isACompleteCommand(String input) 441 { 442 boolean foundCompleteCommand = false; 443 for (Iterator iter = commands.iterator(); iter.hasNext();) 444 { 445 ConsoleCommand command = (ConsoleCommand) iter.next(); 446 if (input.equals(command.getCommandName())) 447 { 448 foundCompleteCommand = !otherCommandsStartWith(command 449 .getCommandName()); 450 } 451 } 452 return foundCompleteCommand; 453 } 454 455 private boolean otherCommandsStartWith(String commandName) 456 { 457 for (Iterator iter = commands.iterator(); iter.hasNext();) 458 { 459 ConsoleCommand command = (ConsoleCommand) iter.next(); 460 if (command.getCommandName().startsWith(commandName) 461 && !command.getCommandName().equals(commandName)) 462 { 463 return true; 464 } 465 } 466 return false; 467 }; 468 }; 469 } | Popular Tags |