1 22 23 package org.continuent.sequoia.console.text.module; 24 25 import java.io.IOException ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.UndeclaredThrowableException ; 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.Properties ; 34 import java.util.Set ; 35 import java.util.StringTokenizer ; 36 import java.util.TreeSet ; 37 38 import jline.ArgumentCompletor; 39 import jline.Completor; 40 import jline.FileNameCompletor; 41 import jline.SimpleCompletor; 42 43 import org.continuent.sequoia.common.i18n.ConsoleTranslate; 44 import org.continuent.sequoia.console.text.Console; 45 import org.continuent.sequoia.console.text.commands.ConsoleCommand; 46 import org.continuent.sequoia.console.text.commands.Help; 47 import org.continuent.sequoia.console.text.commands.History; 48 import org.continuent.sequoia.console.text.commands.Quit; 49 50 57 public abstract class AbstractConsoleModule 58 { 59 Console console; 60 61 TreeSet commands; 62 63 boolean quit = false; 64 65 protected Completor consoleCompletor; 66 67 70 public static final String DEFAULT_COMMAND_PROPERTIES_FILE = "org/continuent/sequoia/console/text/console.ini"; 71 72 77 public AbstractConsoleModule(Console console) 78 { 79 this.console = console; 80 this.commands = new TreeSet (); 81 commands.add(new Help(this)); 82 commands.add(new History(this)); 83 commands.add(new Quit(this)); 84 if (console.isInteractive()) 85 console.printInfo(ConsoleTranslate.get("module.loading", 86 getDescriptionString())); 87 this.loadCommands(); 88 this.loadCompletor(); 89 } 90 91 94 protected final void loadCommands() 95 { 96 commands.clear(); 97 String commandClassesAsString = loadCommandsFromProperties(getModuleID()); 98 String [] commandClasses = parseCommands(commandClassesAsString); 99 addCommands(commandClasses, commands); 100 } 101 102 111 protected String [] parseCommands(String commandClassesAsString) 112 { 113 if (commandClassesAsString == null) 114 { 115 return new String [0]; 116 } 117 String [] cmds = commandClassesAsString.split("\\s*,\\s*"); return cmds; 119 } 120 121 129 protected void addCommands(String [] commandClasses, Set commands) 130 { 131 for (int i = 0; i < commandClasses.length; i++) 132 { 133 String commandClass = commandClasses[i]; 134 Class clazz; 135 try 136 { 137 clazz = Class.forName(commandClass); 138 Constructor constructor; 139 try 140 { 141 constructor = clazz.getConstructor(new Class []{this.getClass()}); 142 } 143 catch (NoSuchMethodException e) 144 { 145 constructor = clazz 146 .getConstructor(new Class []{AbstractConsoleModule.class}); 147 } 148 ConsoleCommand command = (ConsoleCommand) constructor 149 .newInstance(new Object []{this}); 150 commands.add(command); 151 } 152 catch (Exception e) 153 { 154 } 156 } 157 } 158 159 167 protected String loadCommandsFromProperties(String moduleID) 168 { 169 Properties props = new Properties (); 170 try 171 { 172 String propertiesFile = System.getProperty("console.commands", 173 DEFAULT_COMMAND_PROPERTIES_FILE); 174 props.load(ClassLoader.getSystemResourceAsStream(propertiesFile)); 175 } 176 catch (IOException e) 177 { 178 } 180 String commandClassesAsString = props.getProperty(moduleID, ""); 181 return commandClassesAsString; 182 } 183 184 190 protected abstract String getModuleID(); 191 192 195 protected void loadCompletor() 196 { 197 List completors = new LinkedList (); 198 int size = commands.size(); 199 if (size > 0) 200 { 201 TreeSet set = new TreeSet (); 202 Iterator it = commands.iterator(); 203 while (it.hasNext()) 204 { 205 set.add(((ConsoleCommand) it.next()).getCommandName()); 206 } 207 completors.add(new SimpleCompletor((String []) set 208 .toArray(new String [size]))); 209 } 210 completors.add(new FileNameCompletor()); 211 212 Completor[] completorsArray = (Completor[]) completors 213 .toArray(new Completor[completors.size()]); 214 consoleCompletor = new ArgumentCompletor(completorsArray, 215 new CommandDelimiter()); 216 } 217 218 222 protected synchronized void reloadCompletor() 223 { 224 console.getConsoleReader().removeCompletor(consoleCompletor); 225 loadCompletor(); 226 console.getConsoleReader().addCompletor(consoleCompletor); 227 } 228 229 234 public abstract String getDescriptionString(); 235 236 239 public void help() 240 { 241 console.println(ConsoleTranslate.get("module.commands.available", 242 getDescriptionString())); 243 ConsoleCommand command; 244 Iterator it = commands.iterator(); 245 while (it.hasNext()) 246 { 247 command = (ConsoleCommand) it.next(); 248 console.println(command.getCommandName() + " " 249 + command.getCommandParameters()); 250 console.println(" " + command.getCommandDescription()); 251 } 252 } 253 254 257 public void quit() 258 { 259 quit = true; 260 console.getConsoleReader().removeCompletor(getCompletor()); 261 console.getConsoleReader().addCompletor( 262 console.getControllerModule().getCompletor()); 263 } 264 265 270 public TreeSet getCommands() 271 { 272 return commands; 273 } 274 275 280 public abstract String getPromptString(); 281 282 285 public void handlePrompt() 286 { 287 288 if (quit) 289 { 290 if (console.isInteractive()) 291 console.printError(ConsoleTranslate.get("module.quitting", 292 getDescriptionString())); 293 return; 294 } 295 296 quit = false; 298 while (!quit) 299 { 300 301 Hashtable hashCommands = getHashCommands(); 302 try 303 { 304 String commandLine = console.readLine(getPromptString()); 305 if (commandLine == null) 306 { 307 quit(); 308 break; 309 } 310 if (commandLine.equals("")) 311 continue; 312 313 handleCommandLine(commandLine, hashCommands); 314 315 } 316 catch (Exception e) 317 { 318 if (e instanceof UndeclaredThrowableException ) 321 { 322 try 323 { 324 e = (Exception ) ((UndeclaredThrowableException ) e) 325 .getUndeclaredThrowable(); 326 } 327 catch (Throwable ignored) 328 { 329 } 330 } 331 console.printError(ConsoleTranslate.get("module.command.got.error", e 332 .getMessage()), e); 333 if (!console.isInteractive() && console.isExitOnError()) 334 { 335 System.exit(1); 336 } 337 } 338 } 339 } 340 341 346 public final Hashtable getHashCommands() 347 { 348 Hashtable hashCommands = new Hashtable (); 349 ConsoleCommand consoleCommand; 350 Iterator it = commands.iterator(); 351 while (it.hasNext()) 352 { 353 consoleCommand = (ConsoleCommand) it.next(); 354 hashCommands.put(consoleCommand.getCommandName(), consoleCommand); 355 } 356 return hashCommands; 357 } 358 359 366 public final void handleCommandLine(String commandLine, Hashtable hashCommands) 367 throws Exception 368 { 369 StringTokenizer st = new StringTokenizer (commandLine); 370 if (st.hasMoreTokens()) 371 { 372 ConsoleCommand command = findConsoleCommand(commandLine, hashCommands); 373 if (command != null) 374 { 375 command.execute(commandLine 376 .substring(command.getCommandName().length())); 377 return; 378 } 379 } 380 throw new Exception (ConsoleTranslate.get("module.command.not.supported", 381 commandLine)); 382 } 383 384 396 public ConsoleCommand findConsoleCommand(String commandLine, 397 Hashtable hashCommands) 398 { 399 ConsoleCommand foundCommand = null; 400 for (Iterator iter = hashCommands.entrySet().iterator(); iter.hasNext();) 401 { 402 Map.Entry commandEntry = (Map.Entry ) iter.next(); 403 String commandName = (String ) commandEntry.getKey(); 404 if (commandLine.startsWith(commandName)) 405 { 406 ConsoleCommand command = (ConsoleCommand) commandEntry.getValue(); 407 if (foundCommand == null) 408 { 409 foundCommand = command; 410 } 411 if (command.getCommandName().length() > foundCommand.getCommandName() 412 .length()) 413 { 414 foundCommand = command; 415 } 416 } 417 } 418 return foundCommand; 419 } 420 421 427 public abstract void login(String [] params) throws Exception ; 428 429 434 public Console getConsole() 435 { 436 return console; 437 } 438 439 444 public Completor getCompletor() 445 { 446 return consoleCompletor; 447 } 448 449 453 class CommandDelimiter extends ArgumentCompletor.AbstractArgumentDelimiter 454 { 455 459 public boolean isDelimiterChar(String buffer, int pos) 460 { 461 String tentativeCmd = buffer.substring(0, pos); 462 return isACompleteCommand(tentativeCmd); 463 } 464 465 473 private boolean isACompleteCommand(String input) 474 { 475 boolean foundCompleteCommand = false; 476 for (Iterator iter = commands.iterator(); iter.hasNext();) 477 { 478 ConsoleCommand command = (ConsoleCommand) iter.next(); 479 if (input.equals(command.getCommandName())) 480 { 481 foundCompleteCommand = !otherCommandsStartWith(command 482 .getCommandName()); 483 } 484 } 485 return foundCompleteCommand; 486 } 487 488 private boolean otherCommandsStartWith(String commandName) 489 { 490 for (Iterator iter = commands.iterator(); iter.hasNext();) 491 { 492 ConsoleCommand command = (ConsoleCommand) iter.next(); 493 if (command.getCommandName().startsWith(commandName) 494 && !command.getCommandName().equals(commandName)) 495 { 496 return true; 497 } 498 } 499 return false; 500 } 501 } 502 } | Popular Tags |