1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.util.Iterator ; 27 import java.util.StringTokenizer ; 28 29 30 35 public class CLIMain 36 { 37 private static final String HelpCommandAndOptions = "help|--help|-[?]"; 39 private static final String VERSION_OPTION = "-V"; 40 private static final String VERSION_COMMAND = "version"; 41 42 43 public static void invokeCLI(String cmdline, InputsAndOutputs io) 44 throws CommandException, CommandValidationException 45 { 46 InputsAndOutputs.setInstance(io); 47 String [] args = splitStringToArray(cmdline); 48 invokeCommand(args); 49 } 50 51 52 public static void main(String [] args) 53 { 54 long startTime = 0; 55 boolean time = false; 56 if (System.getProperty("com.sun.appserv.cli.timing") != null) { 57 time = true; 58 startTime = System.currentTimeMillis(); 59 } 60 61 try 62 { 63 invokeCommand(args); 64 if (time) { 65 CLILogger.getInstance().printDebugMessage( 66 "Command execution time: " + 67 (System.currentTimeMillis() - startTime) + " ms"); 68 } 69 System.exit(0); 70 } 71 catch (CommandValidationException cve) 72 { 73 CLILogger.getInstance().printExceptionStackTrace(cve); 74 CLILogger.getInstance().printError(cve.getLocalizedMessage()); 75 System.exit(1); 76 } 77 catch (CommandException ce) 78 { 79 CLILogger.getInstance().printExceptionStackTrace(ce); 80 CLILogger.getInstance().printError(ce.getLocalizedMessage()); 81 System.exit(1); 82 } 83 catch (Throwable ex) 84 { 85 CLILogger.getInstance().printExceptionStackTrace(ex); 86 CLILogger.getInstance().printError(ex.getLocalizedMessage()); 87 System.exit(1); 88 } 89 } 90 91 92 93 97 public static void invokeCommand(String [] args) 98 throws CommandException, CommandValidationException 99 { 100 ValidCommand validCommand = null; 101 102 try 103 { 104 final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance(); 106 107 if (args.length < 1) 108 { 109 cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.DONT_SERIALIZE); 110 validCommand = cliDescriptorsReader.getCommand(null); 113 args = new String [] {cliDescriptorsReader.getDefaultCommand()}; 115 } 116 else 117 { 118 if (args[0].equals(VERSION_OPTION)) 121 args[0] = VERSION_COMMAND; 122 123 cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.SERIALIZE_COMMANDS_TO_FILES); 125 126 validCommand = cliDescriptorsReader.getCommand(args[0]); 128 } 129 setCommandLocalizedProperty(cliDescriptorsReader.getProperties()); 130 131 if (args[0].matches(HelpCommandAndOptions)) 134 throw new HelpException(args); 135 136 137 138 checkValidCommand(validCommand, args[0]); 139 140 final CommandLineParser clp = new CommandLineParser(args, validCommand); 142 143 final CommandValidator commandValidator = new CommandValidator(); 145 commandValidator.validateCommandAndOptions(validCommand, 146 clp.getOptionsList(), 147 clp.getOperands()); 148 149 final Command command = CommandFactory.createCommand( 151 validCommand, clp.getOptionsList(), 152 clp.getValidEnvironmentOptions(), 153 clp.getOperands()); 154 command.runCommand(); 156 return; 157 } 158 catch (HelpException he) 159 { 160 invokeHelpClass(he.getHelpClassName(), he.getCommandName(), he.getUsageText(),he.isShell()); 161 } 162 catch (CommandValidationException cve ) 163 { 164 throw new CommandValidationException(getUsageTextFromValidCommand(validCommand) 166 + "\n" + cve.getLocalizedMessage()); 167 } 168 169 } 170 171 172 176 private static void invokeHelpClass(String helpClassName, 177 String helpCommandName, 178 String commandUsageText, 179 boolean isShell) 180 throws CommandException 181 { 182 try 183 { 184 Command helpCommand = null; 185 Class helpClass = Class.forName(helpClassName); 186 helpCommand = (Command)helpClass.newInstance(); 187 helpCommand.setName(helpCommandName); 188 if (helpCommandName != null) 189 helpCommand.setOperands(new java.util.Vector (java.util.Arrays.asList( 190 new String []{helpCommandName}))); 191 if(isShell){ 192 helpCommand.setOption("isMultiMode","true"); 193 final String interactiveVal = (String )CommandEnvironment.getInstance(). 194 getEnvironments().get("interactive"); 195 helpCommand.setOption("interactive", 197 interactiveVal==null?"true":interactiveVal); 198 } 199 helpCommand.runCommand(); 200 } 201 catch (Exception e) 202 { 203 if (commandUsageText == null) 204 throw new CommandException(getLocalizedString("NoUsageText", 205 new Object [] {helpCommandName})); 206 else 207 CLILogger.getInstance().printMessage(getLocalizedString("Usage", 208 new Object []{commandUsageText})); 209 } 210 } 211 212 217 private static String getUsageTextFromValidCommand(final ValidCommand validCommand) 218 { 219 if (validCommand != null && validCommand.getUsageText() != null) 220 { 221 return getLocalizedString("Usage", new Object []{validCommand.getUsageText()}); 222 } 223 return ""; 224 } 225 226 227 230 private static void checkValidCommand(ValidCommand validCommand, 231 String commandName) 232 throws CommandValidationException 233 { 234 if (validCommand == null) 235 { 236 throw new CommandValidationException(getLocalizedString( 237 "InvalidCommand", 238 new Object [] {commandName})); 239 } 240 241 } 242 243 247 private static void setCommandLocalizedProperty(Iterator localizePropertiesIter) 248 throws CommandValidationException 249 { 250 LocalStringsManagerFactory.setCommandLocalStringsManagerProperties( 251 localizePropertiesIter); 252 } 253 254 255 262 private static String getLocalizedString(String key, Object [] toInsert) 263 { 264 try 265 { 266 final LocalStringsManager lsm = LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 267 if (toInsert == null) 268 return lsm.getString(key); 269 else 270 return lsm.getString(key, toInsert); 271 } 272 catch (CommandValidationException cve) 273 { 274 return LocalStringsManager.DEFAULT_STRING_VALUE; 275 } 276 } 277 278 279 288 private static String [] splitStringToArray( String line ) 289 throws CommandException 290 { 291 final CLITokenizer cliTokenizer = new CLITokenizer(line, " "); 292 String [] strArray = new String [cliTokenizer.countTokens()]; 293 int ii=0; 294 while (cliTokenizer.hasMoreTokens()) 295 { 296 strArray[ii++] = cliTokenizer.nextTokenWithoutEscapeAndQuoteChars(); 297 CLILogger.getInstance().printDebugMessage("CLIToken = [" + strArray[ii-1] +"]"); 298 } 299 return strArray; 300 } 301 302 } 303 | Popular Tags |