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

ConsoleLauncher.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): Nicolas Modrzyk.
00022  * Contributor(s): Mathieu Peltier.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.text;
00026 
00027 import java.io.File;
00028 import java.io.FileInputStream;
00029 import java.io.FileNotFoundException;
00030 import java.net.InetAddress;
00031 import java.net.UnknownHostException;
00032 import java.util.Properties;
00033 
00034 import javax.swing.UIManager;
00035 
00036 import org.apache.commons.cli.CommandLine;
00037 import org.apache.commons.cli.CommandLineParser;
00038 import org.apache.commons.cli.GnuParser;
00039 import org.apache.commons.cli.HelpFormatter;
00040 import org.apache.commons.cli.Option;
00041 import org.apache.commons.cli.OptionGroup;
00042 import org.apache.commons.cli.Options;
00043 import org.apache.commons.cli.ParseException;
00044 import org.objectweb.cjdbc.common.jmx.JmxConstants;
00045 import org.objectweb.cjdbc.common.util.Constants;
00046 import org.objectweb.cjdbc.console.gui.CjdbcGui;
00047 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
00048 import org.objectweb.cjdbc.controller.core.ControllerConstants;
00049 
00050 /**
00051  * This class defines a ConsoleLauncher
00052  * 
00053  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00054  * @author <a href="mailto:mathieu.peltier@inrialpes.fr">Mathieu Peltier </a>
00055  * @version 1.0
00056  */
00057 public class ConsoleLauncher
00058 {
00059 
00060   // TODO use other way of passing credentials between frames
00061   /** the credentials used when starting the gui */
00062   // public static Object credentials;
00063   /* main() method */
00064 
00065   /**
00066    * Launchs the C-JDBC console. The available options are: <il>
00067    * <li><code>-d</code> or <code>--debug</code>: show stack trace when
00068    * error occurs.</li>
00069    * <li><code>-f</code> or <code>--file</code>: use a given file as the
00070    * source of commands instead of reading commands interactively.</li>
00071    * <li><code>-h</code> or <code>--help</code>: displays usage
00072    * information.</li>
00073    * <li><code>-i</code> or <code>--ip</code>: IP address of the host name
00074    * where the JMX Server hosting the controller is running (the default is
00075    * '0.0.0.0').</li>
00076    * <li><code>-p</code> or <code>--port</code>: JMX/RMI Port number of
00077    * (the default is
00078    * {@link org.objectweb.cjdbc.common.jmx.JmxConstants#DEFAULT_JMX_RMI_PORT}).
00079    * </li>
00080    * <li><code>-s</code> or <code>--secret</code>: password for JMX
00081    * connection.</li>
00082    * <li><code>-u</code> or <code>--username</code>: username for JMX
00083    * connection.</li>
00084    * <li><code>-v</code> or <code>--version</code>: displays version
00085    * information.</li>
00086    * </ul>
00087    * 
00088    * @param args command line arguments (see above)
00089    * @throws Exception if fails
00090    */
00091   public static void main(String args[]) throws Exception
00092   {
00093     // Create options object
00094     Options options = createOptions();
00095 
00096     // Parse command line
00097     CommandLineParser parser = new GnuParser();
00098     CommandLine commandLine = null;
00099     try
00100     {
00101       commandLine = parser.parse(options, args);
00102     }
00103     catch (ParseException e)
00104     {
00105       System.err.println("Syntax error (" + e + ")");
00106       printUsage(options);
00107       System.exit(1);
00108     }
00109 
00110     // Non-recognized options
00111     int n = commandLine.getArgs().length;
00112     for (int i = 0; i < n; i++)
00113     {
00114       System.err.println("Syntax error (unrecognized option: "
00115           + commandLine.getArgs()[i] + ")");
00116       printUsage(options);
00117       System.exit(1);
00118     }
00119 
00120     // Handle --help option
00121     if (commandLine.hasOption('h'))
00122     {
00123       if (commandLine.getOptions().length > 1)
00124         System.err.println("Syntax error");
00125 
00126       printUsage(options);
00127       System.exit(1);
00128     }
00129 
00130     // Handle --version option
00131     if (commandLine.hasOption('v'))
00132     {
00133       if (commandLine.getOptions().length > 1)
00134       {
00135         System.err.println("Syntax error");
00136         printUsage(options);
00137       }
00138       else
00139         System.out.println("C-JDBC controller console version "
00140             + Constants.VERSION);
00141 
00142       System.exit(1);
00143     }
00144 
00145     // Handle text/gui console start
00146     if (commandLine.hasOption('t'))
00147     {
00148       startTextConsole(commandLine);
00149     }
00150     else
00151     {
00152       try
00153       {
00154         startGuiConsole(commandLine);
00155       }
00156       catch (Throwable t)
00157       {
00158         System.out
00159             .println("Cannot initiate graphic mode. Loading text console instead.");
00160         startTextConsole(commandLine);
00161       }
00162     }
00163 
00164   }
00165 
00166   /**
00167    * Starts the gui
00168    * 
00169    * @param commandLine parameters to start the gui
00170    * @throws Exception if cannot load gui(probably no graphics)
00171    */
00172   public static void startGuiConsole(CommandLine commandLine) throws Exception
00173   {
00174     // Set look and feel: Kunstoff not supported in Mac os X
00175     // so revert to default one
00176     String system = System.getProperty("os.name");
00177     if (system.indexOf("Mac OS") != -1)
00178     {
00179       try
00180       {
00181         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
00182       }
00183       catch (Exception e)
00184       {
00185       }
00186     }
00187 
00188     // set default encoding to UTF so we support japanese
00189     Properties pi = System.getProperties();
00190     pi.put("file.encoding", "UTF-8"); // To add a new
00191     // one
00192     System.setProperties(pi);
00193     new CjdbcGui();
00194   }
00195 
00196   /**
00197    * Starts the text console with the given commandline
00198    * 
00199    * @param commandLine parameters for the text console
00200    * @throws Exception if fails
00201    */
00202   public static void startTextConsole(CommandLine commandLine) throws Exception
00203   {
00204     // Handle --ip option
00205     String ip;
00206     try
00207     {
00208       ip = InetAddress.getLocalHost().getHostName();
00209     }
00210     catch (UnknownHostException e1)
00211     {
00212       ip = "127.0.0.1";
00213     }
00214     if (commandLine.hasOption('i'))
00215     {
00216       String tmp = commandLine.getOptionValue('i');
00217       if (tmp != null)
00218       {
00219         ip = tmp;
00220       }
00221     }
00222 
00223     // Handle --port option
00224     int port;
00225     if (commandLine.hasOption('p'))
00226     {
00227       String s = commandLine.getOptionValue('p');
00228       if (s == null)
00229       {
00230         port = JmxConstants.DEFAULT_JMX_RMI_PORT;
00231       }
00232       else
00233         try
00234         {
00235           port = Integer.parseInt(s);
00236           System.out.println("Using specified " + port + " port number");
00237         }
00238         catch (NumberFormatException e)
00239         {
00240           System.out.println("Bad port number (" + e + "), using default "
00241               + JmxConstants.DEFAULT_JMX_RMI_PORT + " port number");
00242           port = JmxConstants.DEFAULT_JMX_RMI_PORT;
00243         }
00244     }
00245     else
00246     {
00247       port = JmxConstants.DEFAULT_JMX_RMI_PORT;
00248     }
00249 
00250     RmiJmxClient jmxClient = null;
00251     if (commandLine.hasOption('u') && commandLine.hasOption('s'))
00252     {
00253       String username = commandLine.getOptionValue('u');
00254       String password = commandLine.getOptionValue('s');
00255       jmxClient = new RmiJmxClient("" + port, ip, username, password);
00256     }
00257     else
00258     {
00259       try
00260       {
00261         jmxClient = new RmiJmxClient("" + port, ip, null);
00262       }
00263       catch (Exception e)
00264       {
00265         System.out.println("Cannot connect to the jmx server");
00266         System.exit(1);
00267       }
00268     }
00269 
00270     // Launch the console (handle --text and --file options )
00271     System.out.println("Launching the C-JDBC controller console");
00272 
00273     Console console;
00274     if (commandLine.hasOption('f'))
00275     {
00276       File file = new File(commandLine.getOptionValue('f'));
00277       FileInputStream in = null;
00278       try
00279       {
00280         in = new FileInputStream(file);
00281       }
00282       catch (FileNotFoundException e)
00283       {
00284         System.err.println("Failed to open file '" + file.toString() + "' ("
00285             + e + ")");
00286         System.exit(1);
00287       }
00288       console = new Console(jmxClient, in, false);
00289       console.setDebug(commandLine.hasOption('d'));
00290       console.handlePrompt();
00291       System.exit(0);
00292     }
00293     else
00294     {
00295       console = new Console(jmxClient, System.in, true);
00296       console.setDebug(commandLine.hasOption('d'));
00297       console.handlePrompt();
00298     }
00299   }
00300 
00301   /**
00302    * Creates <code>Options</code> object that contains all available options
00303    * that can be used launching C-JDBC console.
00304    * 
00305    * @return an <code>Options</code> instance
00306    */
00307   private static Options createOptions()
00308   {
00309     Options options = new Options();
00310     OptionGroup group = new OptionGroup();
00311 
00312     // help, verbose, text only console and file options (mutually exclusive
00313     // options)
00314     group.addOption(new Option("h", "help", false,
00315         "Displays usage information."));
00316     group.addOption(new Option("t", "text", false, "Start text console."));
00317     group.addOption(new Option("v", "version", false,
00318         "Displays version information."));
00319     group
00320         .addOption(new Option(
00321             "f",
00322             "file",
00323             true,
00324             "Use a given file as the source of commands instead of reading commands interactively."));
00325     options.addOptionGroup(group);
00326 
00327     // controller ip option
00328     String defaultIp = ControllerConstants.DEFAULT_IP;
00329     options
00330         .addOption(new Option(
00331             "i",
00332             "ip",
00333             true,
00334             "IP address of the host name where the JMX Server hosting the controller is running (the default is '"
00335                 + defaultIp + "')."));
00336 
00337     // controller port option
00338     options.addOption(new Option("p", "port", true,
00339         "JMX/RMI port number of (the default is "
00340             + JmxConstants.DEFAULT_JMX_RMI_PORT + ")."));
00341     options.addOption(new Option("u", "username", true,
00342         "Username for JMX connection."));
00343     options.addOption(new Option("s", "secret", true,
00344         "Password for JMX connection."));
00345 
00346     options.addOption(new Option("d", "debug", false,
00347         "Show stack trace when error occurs."));
00348 
00349     return options;
00350   }
00351 
00352   /**
00353    * Displays usage message.
00354    * 
00355    * @param options available command line options
00356    */
00357   private static void printUsage(Options options)
00358   {
00359     String header = "Launchs the C-JDBC controller console."
00360         + System.getProperty("line.separator") + "Options:";
00361 
00362     (new HelpFormatter()).printHelp(80, "console(.sh|.bat) [options]", header,
00363         options, "");
00364   }
00365 
00366 }

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