KickJava   Java API By Example, From Geeks To Geeks.

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


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): Nicolas Modrzyk.
22  * Contributor(s): Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.console.text;
26
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.InetAddress JavaDoc;
31 import java.net.UnknownHostException JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import javax.swing.UIManager JavaDoc;
35
36 import org.apache.commons.cli.CommandLine;
37 import org.apache.commons.cli.CommandLineParser;
38 import org.apache.commons.cli.GnuParser;
39 import org.apache.commons.cli.HelpFormatter;
40 import org.apache.commons.cli.Option;
41 import org.apache.commons.cli.OptionGroup;
42 import org.apache.commons.cli.Options;
43 import org.apache.commons.cli.ParseException;
44 import org.objectweb.cjdbc.common.jmx.JmxConstants;
45 import org.objectweb.cjdbc.common.util.Constants;
46 import org.objectweb.cjdbc.console.gui.CjdbcGui;
47 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
48 import org.objectweb.cjdbc.controller.core.ControllerConstants;
49
50 /**
51  * This class defines a ConsoleLauncher
52  *
53  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
54  * @author <a HREF="mailto:mathieu.peltier@inrialpes.fr">Mathieu Peltier </a>
55  * @version 1.0
56  */

57 public class ConsoleLauncher
58 {
59
60   // TODO use other way of passing credentials between frames
61
/** the credentials used when starting the gui */
62   // public static Object credentials;
63
/* main() method */
64
65   /**
66    * Launchs the C-JDBC console. The available options are: <il>
67    * <li><code>-d</code> or <code>--debug</code>: show stack trace when
68    * error occurs.</li>
69    * <li><code>-f</code> or <code>--file</code>: use a given file as the
70    * source of commands instead of reading commands interactively.</li>
71    * <li><code>-h</code> or <code>--help</code>: displays usage
72    * information.</li>
73    * <li><code>-i</code> or <code>--ip</code>: IP address of the host name
74    * where the JMX Server hosting the controller is running (the default is
75    * '0.0.0.0').</li>
76    * <li><code>-n</code> or <code>--nocolor</code>: do not print colors in
77    * interactive mode for supported systems.</li>
78    * <li><code>-p</code> or <code>--port</code>: JMX/RMI Port number of
79    * (the default is
80    * {@link org.objectweb.cjdbc.common.jmx.JmxConstants#DEFAULT_JMX_RMI_PORT}).
81    * </li>
82    * <li><code>-s</code> or <code>--secret</code>: password for JMX
83    * connection.</li>
84    * <li><code>-u</code> or <code>--username</code>: username for JMX
85    * connection.</li>
86    * <li><code>-v</code> or <code>--version</code>: displays version
87    * information.</li>
88    * </ul>
89    *
90    * @param args command line arguments (see above)
91    * @throws Exception if fails
92    */

93   public static void main(String JavaDoc[] args) throws Exception JavaDoc
94   {
95     // Create options object
96
Options options = createOptions();
97
98     // Parse command line
99
CommandLineParser parser = new GnuParser();
100     CommandLine commandLine = null;
101     try
102     {
103       commandLine = parser.parse(options, args);
104     }
105     catch (ParseException e)
106     {
107       System.err.println("Syntax error (" + e + ")");
108       printUsage(options);
109       System.exit(1);
110     }
111
112     // Non-recognized options
113
int n = commandLine.getArgs().length;
114     for (int i = 0; i < n; i++)
115     {
116       System.err.println("Syntax error (unrecognized option: "
117           + commandLine.getArgs()[i] + ")");
118       printUsage(options);
119       System.exit(1);
120     }
121
122     // Handle --help option
123
if (commandLine.hasOption('h'))
124     {
125       if (commandLine.getOptions().length > 1)
126         System.err.println("Syntax error");
127
128       printUsage(options);
129       System.exit(1);
130     }
131
132     // Handle --version option
133
if (commandLine.hasOption('v'))
134     {
135       if (commandLine.getOptions().length > 1)
136       {
137         System.err.println("Syntax error");
138         printUsage(options);
139       }
140       else
141         System.out.println("C-JDBC controller console version "
142             + Constants.VERSION);
143
144       System.exit(1);
145     }
146
147     // Handle text/gui console start
148
if (commandLine.hasOption('t') || commandLine.hasOption('f'))
149     {
150       startTextConsole(commandLine);
151     }
152     else
153     {
154       try
155       {
156         startGuiConsole();
157       }
158       catch (Throwable JavaDoc t)
159       {
160         System.out
161             .println("Cannot initiate graphic mode. Loading text console instead.");
162         startTextConsole(commandLine);
163       }
164     }
165
166   }
167
168   /**
169    * Starts the gui
170    *
171    * @throws Exception if cannot load gui(probably no graphics)
172    */

173   public static void startGuiConsole() throws Exception JavaDoc
174   {
175     // Set look and feel: Kunstoff not supported in Mac os X
176
// so revert to default one
177
String JavaDoc system = System.getProperty("os.name");
178     if (system.indexOf("Mac OS") != -1)
179     {
180       try
181       {
182         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
183       }
184       catch (Exception JavaDoc e)
185       {
186       }
187     }
188
189     // set default encoding to UTF so we support japanese
190
Properties JavaDoc pi = System.getProperties();
191     pi.put("file.encoding", "UTF-8"); // To add a new
192
// one
193
System.setProperties(pi);
194     new CjdbcGui();
195   }
196
197   /**
198    * Starts the text console with the given commandline
199    *
200    * @param commandLine parameters for the text console
201    * @throws Exception if fails
202    */

203   public static void startTextConsole(CommandLine commandLine) throws Exception JavaDoc
204   {
205     // check if we are in interactive mode, and if so, output no traces
206
boolean isInteractive = !commandLine.hasOption('f');
207
208     // Handle --ip option
209
String JavaDoc ip;
210     try
211     {
212       ip = InetAddress.getLocalHost().getHostName();
213     }
214     catch (UnknownHostException JavaDoc e1)
215     {
216       ip = "127.0.0.1";
217     }
218     if (commandLine.hasOption('i'))
219     {
220       String JavaDoc tmp = commandLine.getOptionValue('i');
221       if (tmp != null)
222       {
223         ip = tmp;
224       }
225     }
226
227     // Handle --port option
228
int port;
229     if (commandLine.hasOption('p'))
230     {
231       String JavaDoc s = commandLine.getOptionValue('p');
232       if (s == null)
233       {
234         port = JmxConstants.DEFAULT_JMX_RMI_PORT;
235       }
236       else
237         try
238         {
239           port = Integer.parseInt(s);
240           if (isInteractive)
241             System.out.println("Using specified " + port + " port number");
242         }
243         catch (NumberFormatException JavaDoc e)
244         {
245           System.out.println("Bad port number (" + e + "), using default "
246               + JmxConstants.DEFAULT_JMX_RMI_PORT + " port number");
247           port = JmxConstants.DEFAULT_JMX_RMI_PORT;
248         }
249     }
250     else
251     {
252       port = JmxConstants.DEFAULT_JMX_RMI_PORT;
253     }
254
255     // Handle --secret and --username options
256
RmiJmxClient jmxClient = null;
257     if (commandLine.hasOption('u') && commandLine.hasOption('s'))
258     {
259       String JavaDoc username = commandLine.getOptionValue('u');
260       String JavaDoc password = commandLine.getOptionValue('s');
261       jmxClient = new RmiJmxClient("" + port, ip, username, password);
262     }
263     else
264     {
265       try
266       {
267         jmxClient = new RmiJmxClient("" + port, ip, null);
268       }
269       catch (Exception JavaDoc e)
270       {
271         System.out.println("Cannot connect to the JMX server");
272         System.exit(1);
273       }
274     }
275
276     // Handle --debug option
277
boolean debug = commandLine.hasOption('d');
278
279     // Launch the console (handle --text and --file options)
280
Console console;
281     InputStream JavaDoc in = null;
282     if (commandLine.hasOption('f'))
283     {
284       String JavaDoc filename = commandLine.getOptionValue('f');
285
286       if ("-".equals(filename))
287       {
288         in = System.in;
289       }
290       else
291       {
292         try
293         {
294           in = new FileInputStream JavaDoc(filename);
295         }
296         catch (FileNotFoundException JavaDoc e)
297         {
298           System.err.println("Failed to open file '" + filename + "' (" + e
299               + ")");
300           System.exit(1);
301         }
302       }
303       System.out
304           .println("Launching the C-JDBC controller console in non interactive mode");
305     }
306     else
307     {
308       System.out.println("Launching the C-JDBC controller console");
309       in = System.in;
310     }
311
312     console = new Console(jmxClient, in, isInteractive, debug);
313     console.setPrintColor(!commandLine.hasOption('n'));
314     console.handlePrompt();
315     System.exit(0);
316   }
317
318   /**
319    * Creates <code>Options</code> object that contains all available options
320    * that can be used launching C-JDBC console.
321    *
322    * @return an <code>Options</code> instance
323    */

324   private static Options createOptions()
325   {
326     Options options = new Options();
327     OptionGroup group = new OptionGroup();
328
329     // help, verbose, text only console and file options (mutually exclusive
330
// options)
331
group.addOption(new Option("h", "help", false,
332         "Displays usage information."));
333     group.addOption(new Option("t", "text", false, "Start text console."));
334     group.addOption(new Option("v", "version", false,
335         "Displays version information."));
336     group
337         .addOption(new Option(
338             "f",
339             "file",
340             true,
341             "Use a given file as the source of commands instead of reading commands interactively."));
342     options.addOptionGroup(group);
343
344     // controller ip option
345
String JavaDoc defaultIp = ControllerConstants.DEFAULT_IP;
346     options
347         .addOption(new Option(
348             "i",
349             "ip",
350             true,
351             "IP address of the host name where the JMX server hosting the controller is running (the default is '"
352                 + defaultIp + "')."));
353
354     // controller port option
355
options.addOption(new Option("p", "port", true,
356         "JMX/RMI port number of (the default is "
357             + JmxConstants.DEFAULT_JMX_RMI_PORT + ")."));
358
359     // JMX options
360
options.addOption(new Option("u", "username", true,
361         "Username for JMX connection."));
362     options.addOption(new Option("s", "secret", true,
363         "Password for JMX connection."));
364
365     options.addOption(new Option("d", "debug", false,
366         "Show stack trace when error occurs."));
367
368     options.addOption(new Option("n", "nocolor", false,
369         "Do not print colors in interactive mode for supported systems."));
370
371     return options;
372   }
373
374   /**
375    * Displays usage message.
376    *
377    * @param options available command line options
378    */

379   private static void printUsage(Options options)
380   {
381     String JavaDoc header = "Launchs the C-JDBC controller console."
382         + System.getProperty("line.separator") + "Options:";
383
384     (new HelpFormatter()).printHelp(80, "console(.sh|.bat) [options]", header,
385         options, "");
386   }
387
388 }
Popular Tags