1 22 23 package org.continuent.sequoia.console.text; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.PrintWriter ; 28 import java.util.Arrays ; 29 import java.util.List ; 30 import java.util.prefs.Preferences ; 31 32 import jline.ConsoleReader; 33 import jline.History; 34 35 import org.continuent.sequoia.common.i18n.ConsoleTranslate; 36 import org.continuent.sequoia.console.jmx.RmiJmxClient; 37 import org.continuent.sequoia.console.text.module.ControllerConsole; 38 import org.continuent.sequoia.console.text.module.VirtualDatabaseAdmin; 39 import org.continuent.sequoia.console.text.module.VirtualDatabaseConsole; 40 41 50 public class Console 51 { 52 private static final Character PASSWORD_CHAR = new Character ('\u0000'); 53 54 55 private ConsoleReader consoleReader; 56 57 58 private boolean interactive; 59 60 private RmiJmxClient jmxClient; 61 62 63 private VirtualDatabaseAdmin adminModule; 64 65 66 private VirtualDatabaseConsole consoleModule; 67 68 69 private ControllerConsole controllerModule; 70 71 72 private boolean debug; 73 private boolean silent = false; 74 75 private boolean exitOnError; 76 77 80 private static final int STORED_HISTORY_SIZE = 100; 81 82 86 private boolean printColor; 87 88 private boolean sqlClientOnly; 89 90 103 public Console(RmiJmxClient jmxClient, InputStream in, boolean interactive, 104 boolean debug, boolean silent, boolean exitOnError, boolean sqlClientOnly) 105 { 106 try 107 { 108 consoleReader = new ConsoleReader(in, new PrintWriter (System.out)); 109 } 110 catch (IOException e) 111 { 112 System.err.println("Unable to create console: " + e.toString()); 113 } 114 this.interactive = interactive; 115 this.jmxClient = jmxClient; 116 this.debug = debug; 117 this.silent = silent; 118 this.exitOnError = exitOnError; 119 this.sqlClientOnly = sqlClientOnly; 120 121 controllerModule = new ControllerConsole(this); 122 adminModule = new VirtualDatabaseAdmin(this); 123 consoleModule = new VirtualDatabaseConsole(this); 124 setPrintColor(true); 125 consoleReader.addCompletor(controllerModule.getCompletor()); 126 consoleReader.setHistory(loadHistory()); 127 128 Runtime.getRuntime().addShutdownHook(new Thread () 129 { 130 public void run() 131 { 132 storeHistory(); 133 } 134 }); 135 } 136 137 private History loadHistory() 138 { 139 jline.History jHistory = new jline.History(); 140 try 141 { 142 Preferences prefs = Preferences.userRoot() 143 .node(this.getClass().getName()); 144 String [] historyKeys = prefs.keys(); 145 Arrays.sort(historyKeys, 0, historyKeys.length); 146 for (int i = 0; i < historyKeys.length; i++) 147 { 148 String key = historyKeys[i]; 149 String value = prefs.get(key, ""); jHistory.addToHistory(value); 151 } 152 } 153 catch (Exception e) 154 { 155 } 157 return jHistory; 158 } 159 160 165 public List getHistory() 166 { 167 return consoleReader.getHistory().getHistoryList(); 168 } 169 170 173 public void storeHistory() 174 { 175 List history = consoleReader.getHistory().getHistoryList(); 176 try 177 { 178 Preferences prefs = Preferences.userRoot() 179 .node(this.getClass().getName()); 180 prefs.clear(); 181 int historySize = history.size(); 182 int start = Math.max(0, historySize - STORED_HISTORY_SIZE); 183 for (int i = start; i < historySize; i++) 186 { 187 prefs.put(String.valueOf(i -start), (String ) history.get(i + start)); 188 } 189 prefs.flush(); 190 } 191 catch (Exception e) 192 { 193 } 195 } 196 197 204 public void setPrintColor(boolean b) 205 { 206 String os = System.getProperty("os.name").toLowerCase(); 207 boolean windows = os.indexOf("nt") > -1 || os.indexOf("windows") > -1; 208 if (windows) 209 printColor = false; 210 else 211 printColor = b; 212 213 if (System.getProperty("sequoia.console.nocolor") != null) 214 printColor = false; 215 } 216 217 222 public boolean isInteractive() 223 { 224 return interactive; 225 } 226 227 233 public boolean isExitOnError() 234 { 235 return exitOnError; 236 } 237 238 241 public void handlePrompt() throws Exception 242 { 243 if (sqlClientOnly) 244 sqlClientConsole(); 245 else 246 controllerModule.handlePrompt(); 247 } 248 249 private void sqlClientConsole() 250 { 251 try 252 { 253 consoleModule.login(new String [] { "" }); 254 } 255 catch (Exception e) 256 { 257 printError(ConsoleTranslate.get("module.command.got.error", e 258 .getMessage()), e); 259 System.exit(1); 260 } 261 consoleModule.handlePrompt(); 262 } 263 264 271 public String readLine(String prompt) throws ConsoleException 272 { 273 String line = ""; 274 try 275 { 276 if (interactive) 277 { 278 prompt += " > "; 279 if (printColor) 280 { 281 prompt = ColorPrinter.getColoredMessage(prompt, ColorPrinter.PROMPT); 282 } 283 line = consoleReader.readLine(prompt); 284 } 285 else 286 { 287 line = consoleReader.readLine(); 288 } 289 } 290 catch (IOException e) 291 { 292 throw new ConsoleException(ConsoleTranslate.get( 293 "console.read.command.failed", e)); 294 } 295 if (line != null) 296 line = line.trim(); 297 return line; 298 } 299 300 307 public String readPassword(String prompt) throws ConsoleException 308 { 309 String password; 310 try 311 { 312 if (interactive) 313 { 314 prompt += " > "; 315 if (printColor) 316 { 317 prompt = ColorPrinter.getColoredMessage(prompt, ColorPrinter.PROMPT); 318 } 319 password = consoleReader.readLine(prompt, PASSWORD_CHAR); 320 } 321 else 322 { 323 password = consoleReader.readLine(PASSWORD_CHAR); 324 } 325 } 326 catch (IOException e) 327 { 328 throw new ConsoleException(ConsoleTranslate.get( 329 "console.read.password.failed", e)); 330 } 331 return password; 332 } 333 334 340 public void print(String s) 341 { 342 System.out.print(s); 343 } 344 345 351 public void println(String s) 352 { 353 System.out.println(s); 354 } 355 356 362 private void println(String s, int color) 363 { 364 if (printColor) 365 ColorPrinter.printMessage(s, System.out, color); 366 else 367 System.out.println(s); 368 } 369 370 373 public void println() 374 { 375 System.out.println(); 376 } 377 378 385 public void printError(String message) 386 { 387 if (printColor) 388 ColorPrinter.printMessage(message, System.err, ColorPrinter.ERROR); 389 else 390 System.err.println(message); 391 } 392 393 401 public void printInfo(String message) 402 { 403 if (!silent) 404 { 405 println(message, ColorPrinter.INFO); 406 } 407 } 408 409 418 public void printError(String message, Exception e) 419 { 420 if (debug) 421 e.printStackTrace(); 422 printError(message); 423 } 424 425 430 public RmiJmxClient getJmxClient() 431 { 432 return jmxClient; 433 } 434 435 440 public void setJmxClient(RmiJmxClient jmxClient) 441 { 442 this.jmxClient = jmxClient; 443 } 444 445 450 public VirtualDatabaseAdmin getAdminModule() 451 { 452 return adminModule; 453 } 454 455 460 public VirtualDatabaseConsole getConsoleModule() 461 { 462 return consoleModule; 463 } 464 465 470 public ControllerConsole getControllerModule() 471 { 472 return controllerModule; 473 } 474 475 480 public final ConsoleReader getConsoleReader() 481 { 482 return consoleReader; 483 } 484 485 492 public void setRequestDelimiter(String delimiter) 493 { 494 consoleModule.setRequestDelimiter(delimiter); 495 } 496 497 505 public void enableMultilineStatements(boolean multilineStatementEnabled) 506 { 507 consoleModule.enableMultilineStatement(multilineStatementEnabled); 508 } 509 } 510 | Popular Tags |