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;
00026
00027 import java.io.IOException;
00028 import java.io.InputStream;
00029 import java.io.PrintWriter;
00030
00031 import jline.ConsoleReader;
00032
00033 import org.objectweb.cjdbc.common.i18n.ConsoleTranslate;
00034 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
00035 import org.objectweb.cjdbc.console.text.module.ControllerConsole;
00036 import org.objectweb.cjdbc.console.text.module.MonitorConsole;
00037 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseAdmin;
00038 import org.objectweb.cjdbc.console.text.module.VirtualDatabaseConsole;
00039 import org.objectweb.cjdbc.console.views.InfoViewer;
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 public class Console
00051 {
00052
00053
00054 private ConsoleReader consoleReader;
00055
00056 private static final Character PASSWORD_CHAR = new Character('*');
00057
00058
00059 private boolean interactive;
00060
00061 private RmiJmxClient jmxClient;
00062
00063
00064 private VirtualDatabaseAdmin adminModule;
00065
00066
00067 private MonitorConsole monitorModule;
00068
00069
00070 private VirtualDatabaseConsole consoleModule;
00071
00072
00073 private ControllerConsole controllerModule;
00074
00075
00076 private boolean debug = false;
00077
00078 private boolean printColor = true;
00079
00080
00081
00082
00083
00084
00085 public void setDebug(boolean debug)
00086 {
00087 this.debug = debug;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 public Console(RmiJmxClient jmxClient, InputStream in, boolean interactive)
00098 {
00099 try
00100 {
00101 consoleReader = new ConsoleReader(in, new PrintWriter(System.out));
00102 }
00103 catch (IOException e)
00104 {
00105 System.err.println("Unable to create console: " + e.toString());
00106 }
00107 this.interactive = interactive;
00108 this.jmxClient = jmxClient;
00109
00110 controllerModule = new ControllerConsole(this);
00111 adminModule = new VirtualDatabaseAdmin(this);
00112 monitorModule = new MonitorConsole(this);
00113 consoleModule = new VirtualDatabaseConsole(this);
00114 setSupportColors();
00115 }
00116
00117 private void setSupportColors()
00118 {
00119 String os = System.getProperty("os.name").toLowerCase();
00120 boolean windows = os.indexOf("nt") > -1 || os.indexOf("windows") > -1;
00121 if (windows)
00122 printColor = false;
00123 else
00124 printColor = true;
00125 }
00126
00127
00128
00129
00130
00131
00132 public boolean isInteractive()
00133 {
00134 return interactive;
00135 }
00136
00137
00138
00139
00140 public void handlePrompt()
00141 {
00142 controllerModule.handlePrompt();
00143 }
00144
00145
00146
00147
00148 public String readLine(String prompt) throws ConsoleException
00149 {
00150 if (interactive)
00151 printPrompt(prompt + " > ");
00152 String line = "";
00153 try
00154 {
00155 line = consoleReader.readLine();
00156 }
00157 catch (IOException e)
00158 {
00159 throw new ConsoleException(ConsoleTranslate.get(
00160 "console.read.command.failed", e));
00161 }
00162 if (line != null)
00163 line = line.trim();
00164 return line;
00165 }
00166
00167
00168
00169
00170
00171
00172 private void printPrompt(String string)
00173 {
00174 if (printColor)
00175 ColorPrinter.printMessage(string, System.out, ColorPrinter.PROMPT, false);
00176 else
00177 print(string);
00178 }
00179
00180
00181
00182
00183 public String readPassword(String prompt) throws ConsoleException
00184 {
00185 if (interactive)
00186 printPrompt(prompt + " > ");
00187 String password;
00188 try
00189 {
00190 password = consoleReader.readLine(PASSWORD_CHAR);
00191 }
00192 catch (IOException e)
00193 {
00194 throw new ConsoleException(ConsoleTranslate.get(
00195 "console.read.password.failed", e));
00196 }
00197 return password;
00198 }
00199
00200
00201
00202
00203 public void print(String s)
00204 {
00205 System.out.print(s);
00206 }
00207
00208
00209
00210
00211 public void println(String s)
00212 {
00213 System.out.println(s);
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 public void println(String s, int color)
00223 {
00224 if (printColor)
00225 ColorPrinter.printMessage(s, System.out, color);
00226 else
00227 System.out.println(s);
00228 }
00229
00230
00231
00232
00233 public void println()
00234 {
00235 System.out.println();
00236 }
00237
00238
00239
00240
00241 public void printError(String message)
00242 {
00243 if (printColor)
00244 ColorPrinter.printMessage(message, System.err, ColorPrinter.ERROR);
00245 else
00246 System.err.println(message);
00247 }
00248
00249
00250
00251
00252 public void printInfo(String message)
00253 {
00254 println(message, ColorPrinter.INFO);
00255 }
00256
00257
00258
00259
00260
00261
00262
00263 public void printError(String message, Exception e)
00264 {
00265 if (debug)
00266 e.printStackTrace();
00267 printError(message);
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277 public void showInfo(String[][] info, InfoViewer viewer)
00278 {
00279 if (printColor)
00280 println(viewer.displayText(info), ColorPrinter.STATUS);
00281 else
00282 System.out.println(viewer.displayText(info));
00283 }
00284
00285
00286
00287
00288
00289
00290 public RmiJmxClient getJmxClient()
00291 {
00292 return jmxClient;
00293 }
00294
00295
00296
00297
00298
00299
00300 public VirtualDatabaseAdmin getAdminModule()
00301 {
00302 return adminModule;
00303 }
00304
00305
00306
00307
00308
00309
00310 public VirtualDatabaseConsole getConsoleModule()
00311 {
00312 return consoleModule;
00313 }
00314
00315
00316
00317
00318
00319
00320 public ControllerConsole getControllerModule()
00321 {
00322 return controllerModule;
00323 }
00324
00325
00326
00327
00328
00329
00330 public MonitorConsole getMonitorModule()
00331 {
00332 return monitorModule;
00333 }
00334
00335
00336
00337
00338
00339
00340 public final ConsoleReader getConsoleReader()
00341 {
00342 return consoleReader;
00343 }
00344 }