1 23 24 27 28 29 package com.sun.enterprise.cli.commands; 30 31 import java.lang.Character ; 32 import java.io.IOException ; 33 import java.util.StringTokenizer ; 34 import java.util.Vector ; 35 36 import com.sun.enterprise.cli.framework.*; 37 38 43 public class MultiProcessCommand extends S1ASCommand 44 { 45 46 private static final String FILE_OPTION = "file"; 47 private static final String PRINTPROMPT_OPTION = "printprompt"; 48 private static final String ENCODING_OPTION = "encoding"; 49 private static final String EXIT = "exit"; 50 private boolean mDone = false; 51 private boolean printPrompt = true; 52 private final String kPromptString = getLocalizedString("AsadminPrompt"); 53 54 55 58 public MultiProcessCommand() 59 { 60 super(); 61 } 62 63 70 public boolean validateOptions() throws CommandValidationException 71 { 72 printPrompt = getBooleanOption(PRINTPROMPT_OPTION); 73 return super.validateOptions(); 74 } 75 76 77 82 public void runCommand() throws CommandException, CommandValidationException 83 { 84 validateOptions(); 85 String line = null; 86 87 try 88 { 89 if (isFileOptionSpecified()) 90 { 91 CLILogger.getInstance().printDebugMessage("file option specified"); 92 checkForFileExistence(null, getOption(FILE_OPTION)); 93 setInputStreamToFile(); 94 } 95 else 96 printExitMessage(); 97 line = printPromptAndReadLine(); 98 99 while (!isExitLine(line)) 100 { 101 if (isExecutableLine(line)) 102 { 103 processLine(line); 104 } 105 line = printPromptAndReadLine(); 106 } 107 } 108 catch ( CommandException ce ) 109 { 110 throw ce; 111 } 112 catch ( Exception e ) 113 { 114 throw new CommandException(e); 115 } 116 } 117 118 119 120 123 private void printExitMessage() 124 { 125 CLILogger.getInstance().printMessage(getLocalizedString("ExitMessage")); 126 } 127 128 129 134 private String printPromptAndReadLine() throws CommandException 135 { 136 try 137 { 138 if (printPrompt) 139 InputsAndOutputs.getInstance().getUserOutput().print( kPromptString ); 140 String line = InputsAndOutputs.getInstance().getUserInput().getLine(); 141 if (line == null && isFileOptionSpecified() == true) 142 return EXIT; 143 else 144 return line; 145 } 146 catch (IOException ioe) 147 { 148 throw new CommandException(getLocalizedString("CouldNotPrintOrRead"), 149 ioe); 150 } 151 } 152 153 157 private boolean isExecutableLine( final String line ) 158 { 159 boolean isExecutable = true; 160 if ( line == null ) 161 { 162 isExecutable = false; 163 } 164 else if ( line.trim().equals("") || 165 line.startsWith("#") || 166 line.length() < 1 ) 167 { 168 isExecutable = false; 169 } 170 return isExecutable; 171 } 172 173 174 179 private void setInputStreamToFile() throws CommandException 180 { 181 try 182 { 183 final String sEncoding = getOption("ENCODING_OPTION"); 184 if (sEncoding == null) 185 { 186 CLILogger.getInstance().printDebugMessage("Set input stream"); 187 InputsAndOutputs.getInstance().setUserInputFile( 188 getOption(FILE_OPTION)); 189 } 190 else 191 { 192 InputsAndOutputs.getInstance().setUserInputFile( 193 getOption(FILE_OPTION), sEncoding); 194 } 195 } 196 catch (IOException ioe) 197 { 198 throw new CommandException(getLocalizedString("CouldNotSetInputStream"), 199 ioe); 200 } 201 } 202 203 204 213 private String [] splitStringToArray( String line ) 214 throws CommandException 215 { 216 final CLITokenizer cliTokenizer = new CLITokenizer(line, " "); 217 String [] strArray = new String [cliTokenizer.countTokens()]; 218 int ii=0; 219 while (cliTokenizer.hasMoreTokens()) 220 { 221 strArray[ii++] = cliTokenizer.nextTokenWithoutEscapeAndQuoteChars(); 222 CLILogger.getInstance().printDebugMessage("CLIToken = [" + strArray[ii-1] +"]"); 223 224 } 225 return strArray; 226 } 227 228 229 232 private boolean isExitLine( final String line ) 233 { 234 if ( line == null || 235 (line != null && (line.equalsIgnoreCase( "exit" ) || 236 line.equalsIgnoreCase( "quit" )) )) 237 { 238 return true; 239 } 240 return false; 241 } 242 243 244 248 private boolean isFileOptionSpecified() 249 { 250 return ( getOption(FILE_OPTION) != null ); 251 } 252 253 254 257 private void checkValidCommand(ValidCommand validCommand, 258 String commandName) 259 throws CommandException 260 { 261 if (validCommand == null) 262 { 263 throw new CommandException(getLocalizedString( 264 "InvalidCommand", new Object [] {commandName})); 265 } 266 267 } 268 269 273 private void processLine(String line) 274 { 275 ValidCommand validCommand = null; 276 try 277 { 278 String [] commandLine = splitStringToArray(line); 279 280 CLIDescriptorsReader cliDescriptorsReader = 282 CLIDescriptorsReader.getInstance(); 283 284 validCommand = cliDescriptorsReader.getCommand( 286 commandLine[0]); 287 288 if (commandLine[0].equals("help")) throw new HelpException( 291 commandLine.length<2?null:commandLine[1]); 292 293 checkValidCommand(validCommand, commandLine[0]); 294 295 CommandLineParser clp = new CommandLineParser(commandLine, 297 validCommand); 298 299 300 CommandValidator commandValidator = new CommandValidator(); 302 commandValidator.validateCommandAndOptions(validCommand, 303 clp.getOptionsList(), 304 clp.getOperands()); 305 306 Command command = CommandFactory.createCommand( 308 validCommand, clp.getOptionsList(), 309 clp.getValidEnvironmentOptions(), 310 clp.getOperands()); 311 command.runCommand(); 313 } 314 catch (HelpException he) 315 { 316 invokeHelpClass(he.getHelpClassName(), he.getCommandName(), he.getUsageText()); 317 318 } 319 catch (CommandValidationException cve) 320 { 321 printUsageText(validCommand); 322 CLILogger.getInstance().printExceptionStackTrace(cve); 323 CLILogger.getInstance().printError(cve.getLocalizedMessage()); 324 } 325 catch (CommandException ce) 326 { 327 CLILogger.getInstance().printExceptionStackTrace(ce); 328 CLILogger.getInstance().printError(ce.getLocalizedMessage()); 329 } 330 } 331 332 336 private void invokeHelpClass(String helpClassName, 337 String helpCommandName, 338 String commandUsageText) 339 { 340 try 341 { 342 Command helpCommand = null; 343 Class helpClass = Class.forName(helpClassName); 344 helpCommand = (Command)helpClass.newInstance(); 345 helpCommand.setName(helpCommandName); 346 if (helpCommandName != null) 347 helpCommand.setOperands(new java.util.Vector (java.util.Arrays.asList( 348 new String []{helpCommandName}))); 349 helpCommand.setOption("isMultiMode", "true"); 351 352 final String interactiveVal = (String )CommandEnvironment.getInstance(). 354 getEnvironments().get(INTERACTIVE); 355 helpCommand.setOption(INTERACTIVE, 357 interactiveVal==null?"true":interactiveVal); 358 359 helpCommand.runCommand(); 360 } 361 catch (Exception e) 362 { 363 if (commandUsageText == null) 364 CLILogger.getInstance().printMessage(getLocalizedString("NoUsageText", 365 new Object [] {helpCommandName})); 366 else 367 CLILogger.getInstance().printMessage(getLocalizedString("Usage", 368 new Object []{commandUsageText})); 369 } 370 } 371 372 373 377 private void printUsageText(final ValidCommand validCommand) 378 { 379 if (validCommand != null && validCommand.getUsageText() != null) 380 { 381 CLILogger.getInstance().printError(getLocalizedString("Usage", 382 new Object []{validCommand.getUsageText()})); 383 } 384 } 385 386 387 } 388 389 | Popular Tags |