Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Console.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet. 
00022  * Contributor(s): Mathieu Peltier,Nicolas Modrzyk
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  * This is the C-JDBC controller console that allows remote administration and
00043  * monitoring of any C-JDBC controller.
00044  * 
00045  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00046  * @author <a href="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
00047  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00048  * @version 1.0
00049  */
00050 public class Console
00051 {
00052 
00053   /** <code>ConsoleReader</code> allowing to reading input. */
00054   private ConsoleReader          consoleReader;
00055 
00056   private static final Character PASSWORD_CHAR = new Character('*');
00057 
00058   /** <code>true</code> if the console is used in interactive mode. */
00059   private boolean                interactive;
00060 
00061   private RmiJmxClient           jmxClient;
00062 
00063   /** Virtual database administration console. */
00064   private VirtualDatabaseAdmin   adminModule;
00065 
00066   /** Monitoring console */
00067   private MonitorConsole         monitorModule;
00068 
00069   /** Virtual database console. */
00070   private VirtualDatabaseConsole consoleModule;
00071 
00072   /** Controller Console */
00073   private ControllerConsole      controllerModule;
00074 
00075   /** Debug Mode */
00076   private boolean                debug         = false;
00077 
00078   private boolean                printColor    = true;
00079 
00080   /**
00081    * Alert this console to show additionnal debugging information
00082    * 
00083    * @param debug <code>true</code> if debug information needed
00084    */
00085   public void setDebug(boolean debug)
00086   {
00087     this.debug = debug;
00088   }
00089 
00090   /**
00091    * Creates a new <code>Console</code> instance.
00092    * 
00093    * @param jmxClient to connect to the jmxServer
00094    * @param in the inputstream to get the command from
00095    * @param interactive if set to <code>true</code> will display prompt
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    * Returns the interactive value.
00129    * 
00130    * @return Returns the interactive.
00131    */
00132   public boolean isInteractive()
00133   {
00134     return interactive;
00135   }
00136 
00137   /**
00138    * Main menu prompt handling.
00139    */
00140   public void handlePrompt()
00141   {
00142     controllerModule.handlePrompt();
00143   }
00144 
00145   /**
00146    * @see Console#readLine(java.lang.String)
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    * Print prompt line
00169    * 
00170    * @param string the string to pring
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    * @see Console#readPassword(java.lang.String)
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    * @see Console#print(java.lang.String)
00202    */
00203   public void print(String s)
00204   {
00205     System.out.print(s);
00206   }
00207 
00208   /**
00209    * @see Console#println(java.lang.String)
00210    */
00211   public void println(String s)
00212   {
00213     System.out.println(s);
00214   }
00215 
00216   /**
00217    * Print in color
00218    * 
00219    * @param s the message to display
00220    * @param color the color to use
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    * @see Console#println()
00232    */
00233   public void println()
00234   {
00235     System.out.println();
00236   }
00237 
00238   /**
00239    * @see Console#printError(java.lang.String)
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    * @see Console#println()
00251    */
00252   public void printInfo(String message)
00253   {
00254     println(message, ColorPrinter.INFO);
00255   }
00256 
00257   /**
00258    * Display an error and stack trace if in debug mode.
00259    * 
00260    * @param message error message
00261    * @param e exception that causes the error
00262    */
00263   public void printError(String message, Exception e)
00264   {
00265     if (debug)
00266       e.printStackTrace();
00267     printError(message);
00268   }
00269 
00270   /**
00271    * Show a table of info in a formatted way
00272    * 
00273    * @param info the data to display
00274    * @param viewer the viewer to use to get the name of the columns and the
00275    *                 format of data
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    * Returns the jmxClient value.
00287    * 
00288    * @return Returns the jmxClient.
00289    */
00290   public RmiJmxClient getJmxClient()
00291   {
00292     return jmxClient;
00293   }
00294 
00295   /**
00296    * Returns the adminModule value.
00297    * 
00298    * @return Returns the adminModule.
00299    */
00300   public VirtualDatabaseAdmin getAdminModule()
00301   {
00302     return adminModule;
00303   }
00304 
00305   /**
00306    * Returns the consoleModule value.
00307    * 
00308    * @return Returns the consoleModule.
00309    */
00310   public VirtualDatabaseConsole getConsoleModule()
00311   {
00312     return consoleModule;
00313   }
00314 
00315   /**
00316    * Returns the controllerModule value.
00317    * 
00318    * @return Returns the controllerModule.
00319    */
00320   public ControllerConsole getControllerModule()
00321   {
00322     return controllerModule;
00323   }
00324 
00325   /**
00326    * Returns the monitorModule value.
00327    * 
00328    * @return Returns the monitorModule.
00329    */
00330   public MonitorConsole getMonitorModule()
00331   {
00332     return monitorModule;
00333   }
00334 
00335   /**
00336    * Returns the consoleReader value.
00337    * 
00338    * @return Returns the consoleReader.
00339    */
00340   public final ConsoleReader getConsoleReader()
00341   {
00342     return consoleReader;
00343   }
00344 }

Generated on Mon Apr 11 22:01:30 2005 for C-JDBC by  doxygen 1.3.9.1