KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > Console


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Mathieu Peltier,Nicolas Modrzyk
23  */

24
25 package org.objectweb.cjdbc.console.text;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.prefs.Preferences JavaDoc;
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 /**
45  * This is the C-JDBC controller console that allows remote administration and
46  * monitoring of any C-JDBC controller.
47  *
48  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
49  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
50  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
51  * @version 1.0
52  */

53 public class Console
54 {
55   private static final Character JavaDoc PASSWORD_CHAR = new Character JavaDoc('\u0000');
56
57   /** <code>ConsoleReader</code> allowing to reading input. */
58   private ConsoleReader consoleReader;
59
60   /** <code>true</code> if the console is used in interactive mode. */
61   private boolean interactive;
62
63   private RmiJmxClient jmxClient;
64
65   /** Virtual database administration console. */
66   private VirtualDatabaseAdmin adminModule;
67
68   /** Monitoring console */
69   private MonitorConsole monitorModule;
70
71   /** Virtual database console. */
72   private VirtualDatabaseConsole consoleModule;
73
74   /** Controller Console */
75   private ControllerConsole controllerModule;
76
77   /** Debug Mode */
78   private boolean debug;
79
80   /**
81    * <code>true</code> if colors should be displayed in interactive mode (work
82    * only on non Windows system).
83    */

84   private boolean printColor;
85
86   /**
87    * Creates a new <code>Console</code> instance.
88    *
89    * @param jmxClient to connect to the jmxServer
90    * @param in the input stream to get the command from
91    * @param interactive if set to <code>true</code> will display prompt
92    * @param debug <code>true</code> if debug mode should be activated.
93    */

94   public Console(RmiJmxClient jmxClient, InputStream JavaDoc in, boolean interactive,
95       boolean debug)
96   {
97     try
98     {
99       consoleReader = new ConsoleReader(in, new PrintWriter JavaDoc(System.out));
100     }
101     catch (IOException JavaDoc 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 JavaDoc prefs = Preferences.userRoot()
124           .node(ControllerConsole.class.getName());
125       String JavaDoc[] historyKeys = prefs.keys();
126       Arrays.sort(historyKeys, 0, historyKeys.length);
127       for (int i = 0; i < historyKeys.length; i++)
128       {
129         String JavaDoc key = historyKeys[i];
130         String JavaDoc value = prefs.get(key, "");
131         jHistory.addToHistory(value);
132       }
133     }
134     catch (Exception JavaDoc e)
135     {
136       // unable to load prefs: do nothing
137
}
138     return jHistory;
139   }
140
141   /**
142    * Should this console display color in interactive mode? Warning, colors only
143    * work on non Windows system.
144    *
145    * @param b <code>true</code> if the console should display color (ignored
146    * on Windows system).
147    */

148   public void setPrintColor(boolean b)
149   {
150     String JavaDoc 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   /**
162    * Returns the interactive value.
163    *
164    * @return Returns the interactive.
165    */

166   public boolean isInteractive()
167   {
168     return interactive;
169   }
170
171   /**
172    * Main menu prompt handling.
173    */

174   public void handlePrompt()
175   {
176     controllerModule.handlePrompt();
177   }
178
179   /**
180    * @see Console#readLine(java.lang.String)
181    */

182   public String JavaDoc readLine(String JavaDoc prompt) throws ConsoleException
183   {
184     String JavaDoc 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 JavaDoc 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   /**
212    * @see Console#readPassword(java.lang.String)
213    */

214   public String JavaDoc readPassword(String JavaDoc prompt) throws ConsoleException
215   {
216     String JavaDoc 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 JavaDoc e)
234     {
235       throw new ConsoleException(ConsoleTranslate.get(
236           "console.read.password.failed", e));
237     }
238     return password;
239   }
240
241   /**
242    * @see Console#print(java.lang.String)
243    */

244   public void print(String JavaDoc s)
245   {
246     System.out.print(s);
247   }
248
249   /**
250    * @see Console#print(java.lang.String)
251    */

252   public void print(String JavaDoc 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   /**
261    * @see Console#println(java.lang.String)
262    */

263   public void println(String JavaDoc s)
264   {
265     System.out.println(s);
266   }
267
268   /**
269    * Print in color
270    *
271    * @param s the message to display
272    * @param color the color to use
273    */

274   public void println(String JavaDoc s, int color)
275   {
276     if (printColor)
277       ColorPrinter.printMessage(s, System.out, color);
278     else
279       System.out.println(s);
280   }
281
282   /**
283    * @see Console#println()
284    */

285   public void println()
286   {
287     System.out.println();
288   }
289
290   /**
291    * @see Console#printError(java.lang.String)
292    */

293   public void printError(String JavaDoc message)
294   {
295     if (printColor)
296       ColorPrinter.printMessage(message, System.err, ColorPrinter.ERROR);
297     else
298       System.err.println(message);
299   }
300
301   /**
302    * @see Console#println()
303    */

304   public void printInfo(String JavaDoc message)
305   {
306     println(message, ColorPrinter.INFO);
307   }
308
309   /**
310    * Display an error and stack trace if in debug mode.
311    *
312    * @param message error message
313    * @param e exception that causes the error
314    */

315   public void printError(String JavaDoc message, Exception JavaDoc e)
316   {
317     if (debug)
318       e.printStackTrace();
319     printError(message);
320   }
321
322   /**
323    * Show a table of info in a formatted way
324    *
325    * @param info the data to display
326    * @param viewer the viewer to use to get the name of the columns and the
327    * format of data
328    */

329   public void showInfo(String JavaDoc[][] 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   /**
338    * Returns the jmxClient value.
339    *
340    * @return Returns the jmxClient.
341    */

342   public RmiJmxClient getJmxClient()
343   {
344     return jmxClient;
345   }
346
347   /**
348    * Returns the adminModule value.
349    *
350    * @return Returns the adminModule.
351    */

352   public VirtualDatabaseAdmin getAdminModule()
353   {
354     return adminModule;
355   }
356
357   /**
358    * Returns the consoleModule value.
359    *
360    * @return Returns the consoleModule.
361    */

362   public VirtualDatabaseConsole getConsoleModule()
363   {
364     return consoleModule;
365   }
366
367   /**
368    * Returns the controllerModule value.
369    *
370    * @return Returns the controllerModule.
371    */

372   public ControllerConsole getControllerModule()
373   {
374     return controllerModule;
375   }
376
377   /**
378    * Returns the monitorModule value.
379    *
380    * @return Returns the monitorModule.
381    */

382   public MonitorConsole getMonitorModule()
383   {
384     return monitorModule;
385   }
386
387   /**
388    * Returns the consoleReader value.
389    *
390    * @return Returns the consoleReader.
391    */

392   public final ConsoleReader getConsoleReader()
393   {
394     return consoleReader;
395   }
396 }
Popular Tags