1 24 25 package org.objectweb.cjdbc.console.text; 26 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.InputStream ; 30 import java.net.InetAddress ; 31 import java.net.UnknownHostException ; 32 import java.util.Properties ; 33 34 import javax.swing.UIManager ; 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 57 public class ConsoleLauncher 58 { 59 60 62 64 65 93 public static void main(String [] args) throws Exception 94 { 95 Options options = createOptions(); 97 98 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 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 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 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 if (commandLine.hasOption('t') || commandLine.hasOption('f')) 149 { 150 startTextConsole(commandLine); 151 } 152 else 153 { 154 try 155 { 156 startGuiConsole(); 157 } 158 catch (Throwable t) 159 { 160 System.out 161 .println("Cannot initiate graphic mode. Loading text console instead."); 162 startTextConsole(commandLine); 163 } 164 } 165 166 } 167 168 173 public static void startGuiConsole() throws Exception 174 { 175 String 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 e) 185 { 186 } 187 } 188 189 Properties pi = System.getProperties(); 191 pi.put("file.encoding", "UTF-8"); System.setProperties(pi); 194 new CjdbcGui(); 195 } 196 197 203 public static void startTextConsole(CommandLine commandLine) throws Exception 204 { 205 boolean isInteractive = !commandLine.hasOption('f'); 207 208 String ip; 210 try 211 { 212 ip = InetAddress.getLocalHost().getHostName(); 213 } 214 catch (UnknownHostException e1) 215 { 216 ip = "127.0.0.1"; 217 } 218 if (commandLine.hasOption('i')) 219 { 220 String tmp = commandLine.getOptionValue('i'); 221 if (tmp != null) 222 { 223 ip = tmp; 224 } 225 } 226 227 int port; 229 if (commandLine.hasOption('p')) 230 { 231 String 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 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 RmiJmxClient jmxClient = null; 257 if (commandLine.hasOption('u') && commandLine.hasOption('s')) 258 { 259 String username = commandLine.getOptionValue('u'); 260 String 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 e) 270 { 271 System.out.println("Cannot connect to the JMX server"); 272 System.exit(1); 273 } 274 } 275 276 boolean debug = commandLine.hasOption('d'); 278 279 Console console; 281 InputStream in = null; 282 if (commandLine.hasOption('f')) 283 { 284 String 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 (filename); 295 } 296 catch (FileNotFoundException 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 324 private static Options createOptions() 325 { 326 Options options = new Options(); 327 OptionGroup group = new OptionGroup(); 328 329 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 String 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 options.addOption(new Option("p", "port", true, 356 "JMX/RMI port number of (the default is " 357 + JmxConstants.DEFAULT_JMX_RMI_PORT + ").")); 358 359 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 379 private static void printUsage(Options options) 380 { 381 String 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 |