1 24 25 package org.objectweb.cjdbc.console.text; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.PrintWriter ; 30 import java.util.Arrays ; 31 import java.util.prefs.Preferences ; 32 33 import jline.ConsoleReader; 34 import jline.History; 35 36 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate; 37 import org.objectweb.cjdbc.console.jmx.RmiJmxClient; 38 import org.objectweb.cjdbc.console.text.module.ControllerConsole; 39 import org.objectweb.cjdbc.console.text.module.MonitorConsole; 40 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseAdmin; 41 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseConsole; 42 import org.objectweb.cjdbc.console.views.InfoViewer; 43 44 53 public class Console 54 { 55 private static final Character PASSWORD_CHAR = new Character ('\u0000'); 56 57 58 private ConsoleReader consoleReader; 59 60 61 private boolean interactive; 62 63 private RmiJmxClient jmxClient; 64 65 66 private VirtualDatabaseAdmin adminModule; 67 68 69 private MonitorConsole monitorModule; 70 71 72 private VirtualDatabaseConsole consoleModule; 73 74 75 private ControllerConsole controllerModule; 76 77 78 private boolean debug; 79 80 84 private boolean printColor; 85 86 94 public Console(RmiJmxClient jmxClient, InputStream in, boolean interactive, 95 boolean debug) 96 { 97 try 98 { 99 consoleReader = new ConsoleReader(in, new PrintWriter (System.out)); 100 } 101 catch (IOException e) 102 { 103 System.err.println("Unable to create console: " + e.toString()); 104 } 105 this.interactive = interactive; 106 this.jmxClient = jmxClient; 107 this.debug = debug; 108 109 controllerModule = new ControllerConsole(this); 110 adminModule = new VirtualDatabaseAdmin(this); 111 monitorModule = new MonitorConsole(this); 112 consoleModule = new VirtualDatabaseConsole(this); 113 setPrintColor(true); 114 consoleReader.addCompletor(controllerModule.getCompletor()); 115 consoleReader.setHistory(loadJLineHistory()); 116 } 117 118 private History loadJLineHistory() 119 { 120 jline.History jHistory = new jline.History(); 121 try 122 { 123 Preferences prefs = Preferences.userRoot() 124 .node(ControllerConsole.class.getName()); 125 String [] historyKeys = prefs.keys(); 126 Arrays.sort(historyKeys, 0, historyKeys.length); 127 for (int i = 0; i < historyKeys.length; i++) 128 { 129 String key = historyKeys[i]; 130 String value = prefs.get(key, ""); 131 jHistory.addToHistory(value); 132 } 133 } 134 catch (Exception e) 135 { 136 } 138 return jHistory; 139 } 140 141 148 public void setPrintColor(boolean b) 149 { 150 String os = System.getProperty("os.name").toLowerCase(); 151 boolean windows = os.indexOf("nt") > -1 || os.indexOf("windows") > -1; 152 if (windows) 153 printColor = false; 154 else 155 printColor = b; 156 157 if (System.getProperty("cjdbc.console.nocolor") != null) 158 printColor = false; 159 } 160 161 166 public boolean isInteractive() 167 { 168 return interactive; 169 } 170 171 174 public void handlePrompt() 175 { 176 controllerModule.handlePrompt(); 177 } 178 179 182 public String readLine(String prompt) throws ConsoleException 183 { 184 String line = ""; 185 try 186 { 187 if (interactive) 188 { 189 prompt += " > "; 190 if (printColor) 191 { 192 prompt = ColorPrinter.getColoredMessage(prompt, ColorPrinter.PROMPT); 193 } 194 line = consoleReader.readLine(prompt); 195 } 196 else 197 { 198 line = consoleReader.readLine(); 199 } 200 } 201 catch (IOException e) 202 { 203 throw new ConsoleException(ConsoleTranslate.get( 204 "console.read.command.failed", e)); 205 } 206 if (line != null) 207 line = line.trim(); 208 return line; 209 } 210 211 214 public String readPassword(String prompt) throws ConsoleException 215 { 216 String password; 217 try 218 { 219 if (interactive) 220 { 221 prompt += " > "; 222 if (printColor) 223 { 224 prompt = ColorPrinter.getColoredMessage(prompt, ColorPrinter.PROMPT); 225 } 226 password = consoleReader.readLine(prompt, PASSWORD_CHAR); 227 } 228 else 229 { 230 password = consoleReader.readLine(PASSWORD_CHAR); 231 } 232 } 233 catch (IOException e) 234 { 235 throw new ConsoleException(ConsoleTranslate.get( 236 "console.read.password.failed", e)); 237 } 238 return password; 239 } 240 241 244 public void print(String s) 245 { 246 System.out.print(s); 247 } 248 249 252 public void print(String s, int color) 253 { 254 if (printColor) 255 ColorPrinter.printMessage(s, System.out, color, false); 256 else 257 System.out.print(s); 258 } 259 260 263 public void println(String s) 264 { 265 System.out.println(s); 266 } 267 268 274 public void println(String s, int color) 275 { 276 if (printColor) 277 ColorPrinter.printMessage(s, System.out, color); 278 else 279 System.out.println(s); 280 } 281 282 285 public void println() 286 { 287 System.out.println(); 288 } 289 290 293 public void printError(String message) 294 { 295 if (printColor) 296 ColorPrinter.printMessage(message, System.err, ColorPrinter.ERROR); 297 else 298 System.err.println(message); 299 } 300 301 304 public void printInfo(String message) 305 { 306 println(message, ColorPrinter.INFO); 307 } 308 309 315 public void printError(String message, Exception e) 316 { 317 if (debug) 318 e.printStackTrace(); 319 printError(message); 320 } 321 322 329 public void showInfo(String [][] info, InfoViewer viewer) 330 { 331 if (printColor) 332 println(viewer.displayText(info), ColorPrinter.STATUS); 333 else 334 System.out.println(viewer.displayText(info)); 335 } 336 337 342 public RmiJmxClient getJmxClient() 343 { 344 return jmxClient; 345 } 346 347 352 public VirtualDatabaseAdmin getAdminModule() 353 { 354 return adminModule; 355 } 356 357 362 public VirtualDatabaseConsole getConsoleModule() 363 { 364 return consoleModule; 365 } 366 367 372 public ControllerConsole getControllerModule() 373 { 374 return controllerModule; 375 } 376 377 382 public MonitorConsole getMonitorModule() 383 { 384 return monitorModule; 385 } 386 387 392 public final ConsoleReader getConsoleReader() 393 { 394 return consoleReader; 395 } 396 } | Popular Tags |