1 22 23 package org.continuent.sequoia.console.text; 24 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.InetAddress ; 30 import java.net.UnknownHostException ; 31 import java.util.Properties ; 32 33 import org.apache.commons.cli.CommandLine; 34 import org.apache.commons.cli.CommandLineParser; 35 import org.apache.commons.cli.GnuParser; 36 import org.apache.commons.cli.HelpFormatter; 37 import org.apache.commons.cli.Option; 38 import org.apache.commons.cli.OptionGroup; 39 import org.apache.commons.cli.Options; 40 import org.apache.commons.cli.ParseException; 41 import org.continuent.sequoia.common.i18n.ConsoleTranslate; 42 import org.continuent.sequoia.common.jmx.JmxConstants; 43 import org.continuent.sequoia.common.util.Constants; 44 import org.continuent.sequoia.console.jmx.RmiJmxClient; 45 import org.continuent.sequoia.console.text.module.AbstractConsoleModule; 46 47 54 public class ConsoleLauncher 55 { 56 59 public static final String PRODUCT_NAME; 60 61 static 62 { 63 Properties props = new Properties (); 64 try 65 { 66 String propertiesFile = System.getProperty("console.commands", AbstractConsoleModule.DEFAULT_COMMAND_PROPERTIES_FILE); 68 props.load(ClassLoader.getSystemResourceAsStream(propertiesFile)); 69 PRODUCT_NAME = props.getProperty("product.name"); } 71 catch (IOException e) 72 { 73 throw new ExceptionInInitializerError (e.getMessage()); 74 } 75 } 76 77 113 public static void main(String [] args) throws Exception 114 { 115 Options options = createOptions(); 117 118 CommandLineParser parser = new GnuParser(); 120 CommandLine commandLine = null; 121 try 122 { 123 commandLine = parser.parse(options, args); 124 } 125 catch (ParseException e) 126 { 127 System.err.println("Syntax error (" + e + ")"); 128 printUsage(options); 129 System.exit(1); 130 } 131 132 int n = commandLine.getArgs().length; 134 for (int i = 0; i < n; i++) 135 { 136 System.err.println("Syntax error (unrecognized option: " 137 + commandLine.getArgs()[i] + ")"); 138 printUsage(options); 139 System.exit(1); 140 } 141 142 if (commandLine.hasOption('h')) 144 { 145 if (commandLine.getOptions().length > 1) 146 System.err.println("Syntax error"); 147 148 printUsage(options); 149 System.exit(1); 150 } 151 152 if (commandLine.hasOption('v')) 154 { 155 if (commandLine.getOptions().length > 1) 156 { 157 System.err.println("Syntax error"); 158 printUsage(options); 159 } 160 else 161 System.out.println(ConsoleTranslate.get("console.version", 162 new String []{PRODUCT_NAME, Constants.VERSION})); 163 164 System.exit(1); 165 } 166 167 startTextConsole(commandLine); 168 } 169 170 176 public static void startTextConsole(CommandLine commandLine) throws Exception 177 { 178 boolean isInteractive = !commandLine.hasOption('f'); 180 181 String ip; 183 try 184 { 185 ip = InetAddress.getByName(null).getHostName(); 187 } 188 catch (UnknownHostException e1) 189 { 190 ip = "127.0.0.1"; 192 } 193 if (commandLine.hasOption('i')) 194 { 195 String tmp = commandLine.getOptionValue('i'); 196 if (tmp != null) 197 { 198 ip = tmp; 199 } 200 } 201 202 boolean exitOnError = commandLine.hasOption('e'); 203 204 boolean debug = commandLine.hasOption('d'); 206 boolean silent = commandLine.hasOption('l'); 208 209 Console console; 211 InputStream in = null; 212 if (commandLine.hasOption('f')) 213 { 214 String filename = commandLine.getOptionValue('f'); 215 if ("-".equals(filename)) 216 { 217 in = System.in; 218 } 219 else 220 { 221 try 222 { 223 in = new FileInputStream (filename); 224 } 225 catch (FileNotFoundException e) 226 { 227 System.err.println("Failed to open file '" + filename + "' (" + e 228 + ")"); 229 System.exit(1); 230 } 231 } 232 } 233 else 234 { 235 System.out.println(ConsoleTranslate.get( 236 "console.interactive.mode", PRODUCT_NAME)); in = System.in; 238 } 239 240 RmiJmxClient jmxClient = null; 241 boolean sqlClientOnly = commandLine.hasOption('q'); 242 if (! sqlClientOnly) 243 { 244 jmxClient = getJmxClient(commandLine, ip, exitOnError); 245 } 246 console = new Console(jmxClient, in, isInteractive, debug, silent, 247 exitOnError, sqlClientOnly); 248 console.setPrintColor(!commandLine.hasOption('n')); 249 console.enableMultilineStatements(commandLine.hasOption('m')); 250 if (commandLine.hasOption('r')) 251 { 252 console.setRequestDelimiter(commandLine.getOptionValue('r')); 253 } 254 console.handlePrompt(); 255 System.exit(0); 256 } 257 258 static RmiJmxClient getJmxClient(CommandLine commandLine, String ip, 259 boolean exitOnError) throws IOException 260 { 261 int port; 263 if (commandLine.hasOption('p')) 264 { 265 String s = commandLine.getOptionValue('p'); 266 if (s == null) 267 { 268 port = JmxConstants.DEFAULT_JMX_RMI_PORT; 269 } 270 else 271 try 272 { 273 port = Integer.parseInt(s); 274 } 275 catch (NumberFormatException e) 276 { 277 System.err.println("Bad port number (" + e + "), using default " 278 + JmxConstants.DEFAULT_JMX_RMI_PORT + " port number"); 279 port = JmxConstants.DEFAULT_JMX_RMI_PORT; 280 } 281 } 282 else 283 { 284 port = JmxConstants.DEFAULT_JMX_RMI_PORT; 285 } 286 RmiJmxClient jmxClient = null; 288 if (commandLine.hasOption('u') && commandLine.hasOption('s')) 289 { 290 String username = commandLine.getOptionValue('u'); 291 String password = commandLine.getOptionValue('s'); 292 jmxClient = new RmiJmxClient("" + port, ip, username, password); 293 } 294 else 295 { 296 try 297 { 298 jmxClient = new RmiJmxClient("" + port, ip, null); 299 } 300 catch (Exception e) 301 { 302 System.err 303 .println("Cannot connect to the administration port of the controller. Is a controller running at " 304 + ip + ":" + port + " ?"); 305 if (exitOnError) 308 { 309 System.exit(1); 310 } 311 } 315 } 316 317 return jmxClient; 318 } 319 320 326 private static Options createOptions() 327 { 328 Options options = new Options(); 329 OptionGroup group = new OptionGroup(); 330 331 group.addOption(new Option("h", "help", false, 334 "Displays usage information.")); 335 group.addOption(new Option("t", "text", false, 336 "Ignored - only for previous version compatibility.")); 337 group.addOption(new Option("v", "version", false, 338 "Displays version information.")); 339 group 340 .addOption(new Option( 341 "f", 342 "file", 343 true, 344 "Use a given file as the source of commands instead of reading commands interactively.")); 345 options.addOptionGroup(group); 346 347 351 String defaultIp = "0.0.0.0"; 352 options.addOption(new Option("i", "ip", true, 354 "The JMX server of the controller binds to this address (the default is '" 355 + defaultIp + "').")); 356 357 options.addOption(new Option("p", "port", true, 359 "JMX/RMI port number of (the default is " 360 + JmxConstants.DEFAULT_JMX_RMI_PORT + ").")); 361 362 options.addOption(new Option("u", "username", true, 364 "Username for JMX connection.")); 365 options.addOption(new Option("s", "secret", true, 366 "Password for JMX connection.")); 367 368 options.addOption(new Option("d", "debug", false, 369 "Show stack trace when error occurs.")); 370 options.addOption(new Option("l", "silent", false, 371 "Show only most meaningful messages.")); 372 373 options.addOption(new Option("n", "nocolor", false, 374 "Do not print colors in interactive mode for supported systems.")); 375 376 options.addOption(new Option("e", "exitonerror", false, 377 "Stop on error in non interactive mode.")); 378 379 options.addOption(new Option("r", "requestdelimiter", true, 380 "Request delimiter for multiline statements in the SQL console.")); 381 382 options.addOption(new Option("m", "multiline", false, 383 "Enable multiline statements in the SQL console.")); 384 385 options.addOption(new Option("q", "sqlclient", false, 386 "Launch SQL client console.")); 387 388 return options; 389 } 390 391 396 private static void printUsage(Options options) 397 { 398 String header = ConsoleTranslate.get("console.launches", PRODUCT_NAME) 399 + System.getProperty("line.separator") + "Options:"; 400 401 (new HelpFormatter()).printHelp(80, "console(.sh|.bat) [options]", header, 402 options, ""); 403 } 404 405 } | Popular Tags |