1 16 package com.sun.slamd.example; 17 18 19 20 import java.io.*; 21 import java.util.*; 22 import com.sun.slamd.client.*; 23 import com.sun.slamd.job.*; 24 import com.sun.slamd.parameter.*; 25 import com.sun.slamd.stat.*; 26 27 28 29 36 public class ExecJobClass 37 extends JobClass 38 { 39 42 public static final int READ_BUFFER_SIZE = 4096; 43 44 45 46 BooleanParameter logOutputParameter = 48 new BooleanParameter("log_output", "Log Command Output", 49 "Indicates whether the output of the command " + 50 "should be logged.", false); 51 52 MultiLineTextParameter environmentParameter = 55 new MultiLineTextParameter("env_variables", "Environment Variables", 56 "A set of environment variables that " + 57 "should be defined when the job is " + 58 "executed. The environment variables " + 59 "should be specified one per line, using " + 60 "the format name=value", null, false); 61 62 StringParameter commandParameter = 64 new StringParameter("command", "Command to Execute", 65 "Specifies the command to execute.", true, ""); 66 67 StringParameter workingDirParameter = 69 new StringParameter("working_dir", "Working Directory", 70 "The path to the working directory to use when " + 71 "executing the command.", false, ""); 72 73 static boolean logOutput; 75 76 PlaceholderParameter placeholder = new PlaceholderParameter(); 78 79 static String command; 81 82 static File workingDir; 84 85 static String [] environmentVariables; 87 88 byte[] readBuffer; 90 91 92 93 99 public ExecJobClass() 100 { 101 super(); 102 } 103 104 105 106 111 public String getJobName() 112 { 113 return "Exec Job"; 114 } 115 116 117 118 123 public String getJobDescription() 124 { 125 return "This job can be used to execute a specified command on a client " + 126 "system. The output of the command can be captured and logged."; 127 } 128 129 130 131 137 public String getJobCategoryName() 138 { 139 return "Utility"; 140 } 141 142 143 144 151 public ParameterList getParameterStubs() 152 { 153 Parameter[] parameters = new Parameter[] 154 { 155 placeholder, 156 commandParameter, 157 workingDirParameter, 158 environmentParameter, 159 logOutputParameter 160 }; 161 162 return new ParameterList(parameters); 163 } 164 165 166 167 189 public StatTracker[] getStatTrackerStubs(String clientID, String threadID, 190 int collectionInterval) 191 { 192 return new StatTracker[0]; 193 } 194 195 196 197 202 public StatTracker[] getStatTrackers() 203 { 204 return new StatTracker[0]; 205 } 206 207 208 209 231 public void validateJobInfo(int numClients, int threadsPerClient, 232 int threadStartupDelay, Date startTime, 233 Date stopTime, int duration, 234 int collectionInterval, ParameterList parameters) 235 throws InvalidValueException 236 { 237 MultiLineTextParameter envParameter = 240 parameters.getMultiLineTextParameter(environmentParameter.getName()); 241 if (envParameter != null) 242 { 243 String [] lines = envParameter.getNonBlankLines(); 244 for (int i=0; ((lines != null) && (i < lines.length)); i++) 245 { 246 if (lines[i].indexOf('=') <= 0) 247 { 248 throw new InvalidValueException("Invalid environment variable " + 249 "specified: \"" + lines[i] + 250 "\". Expected {name}={value}"); 251 } 252 } 253 } 254 } 255 256 257 258 268 public void initializeClient(String clientID, ParameterList parameters) 269 throws UnableToRunException 270 { 271 command = null; 272 commandParameter = 273 parameters.getStringParameter(commandParameter.getName()); 274 if (commandParameter != null) 275 { 276 command = commandParameter.getStringValue(); 277 } 278 279 280 workingDir = null; 281 workingDirParameter = 282 parameters.getStringParameter(workingDirParameter.getName()); 283 if ((workingDirParameter != null) && (workingDirParameter.hasValue())) 284 { 285 String workingDirStr = workingDirParameter.getStringValue(); 286 workingDir = new File(workingDirStr); 287 288 try 289 { 290 if (! workingDir.exists()) 291 { 292 throw new UnableToRunException("Working directory \"" + 293 workingDirStr + "\" does not exist."); 294 } 295 296 if (! workingDir.isDirectory()) 297 { 298 throw new UnableToRunException("Working directory \"" + 299 workingDirStr + 300 "\" is not a directory."); 301 } 302 } 303 catch (UnableToRunException utre) 304 { 305 throw utre; 306 } 307 catch (Exception e) 308 { 309 throw new UnableToRunException("Unable to verify existence of " + 310 "working directory \"" + workingDirStr + 311 "\": " + e, e); 312 } 313 } 314 315 316 environmentVariables = null; 317 environmentParameter = 318 parameters.getMultiLineTextParameter(environmentParameter.getName()); 319 if (environmentParameter != null) 320 { 321 environmentVariables = environmentParameter.getNonBlankLines(); 322 } 323 324 325 logOutput = false; 326 logOutputParameter = 327 parameters.getBooleanParameter(logOutputParameter.getName()); 328 if (logOutputParameter != null) 329 { 330 logOutput = logOutputParameter.getBooleanValue(); 331 } 332 } 333 334 335 336 352 public void initializeThread(String clientID, String threadID, 353 int collectionInterval, ParameterList parameters) 354 throws UnableToRunException 355 { 356 readBuffer = new byte[READ_BUFFER_SIZE]; 358 } 359 360 361 362 365 public void runJob() 366 { 367 Runtime runtime = Runtime.getRuntime(); 368 Process process = null; 369 370 try 371 { 372 process = runtime.exec(command, environmentVariables, workingDir); 373 } 374 catch (IOException ioe) 375 { 376 logMessage("Unable to execute command \"" + command + "\": " + ioe); 377 indicateStoppedDueToError(); 378 return; 379 } 380 381 382 BufferedInputStream stdOutStream = 383 new BufferedInputStream(process.getInputStream()); 384 BufferedInputStream stdErrStream = 385 new BufferedInputStream(process.getErrorStream()); 386 387 388 while (true) 389 { 390 try 391 { 392 if (logOutput) 393 { 394 if (stdOutStream.available() > 0) 395 { 396 while ((! shouldStop()) && (stdOutStream.available() > 0)) 397 { 398 int bytesRead = stdOutStream.read(readBuffer); 399 String [] outputStrs = byteArrayToStrings(readBuffer, bytesRead); 400 for (int i=0; i < outputStrs.length; i++) 401 { 402 logMessage("STDOUT: " + outputStrs[i]); 403 } 404 } 405 } 406 407 if (stdErrStream.available() > 0) 408 { 409 while ((! shouldStop()) && (stdErrStream.available() > 0)) 410 { 411 int bytesRead = stdErrStream.read(readBuffer); 412 String [] errorStrs = byteArrayToStrings(readBuffer, bytesRead); 413 for (int i=0; i < errorStrs.length; i++) 414 { 415 logMessage("STDERR: " + errorStrs[i]); 416 } 417 } 418 } 419 } 420 421 if (shouldStop()) 422 { 423 try 424 { 425 stdOutStream.close(); 426 stdErrStream.close(); 427 } catch (Exception e) {} 428 429 process.destroy(); 430 logMessage("Terminated process because the client determined it " + 431 "should stop running."); 432 return; 433 } 434 435 try 436 { 437 int returnCode = process.exitValue(); 438 if (returnCode == 0) 439 { 440 logMessage("Command completed successfully (exit code 0)"); 441 } 442 else 443 { 444 logMessage("Command completed abnormally (exit code " + 445 returnCode + ")"); 446 indicateCompletedWithErrors(); 447 } 448 449 try 450 { 451 stdOutStream.close(); 452 stdErrStream.close(); 453 } catch (Exception e) {} 454 455 return; 456 } catch (IllegalThreadStateException itse) {} 457 458 try 459 { 460 Thread.sleep(100); 461 } catch (InterruptedException ie) {} 462 } 463 catch (IOException ioe) 464 { 465 boolean completedSuccessfully = false; 468 try 469 { 470 int returnCode = process.exitValue(); 471 completedSuccessfully = (returnCode == 0); 472 if (completedSuccessfully) 473 { 474 logMessage("Command completed successfully (exit code 0)"); 475 } 476 else 477 { 478 logMessage("Command completed abnormally (exit code " + returnCode + 479 ")"); 480 indicateCompletedWithErrors(); 481 } 482 } 483 catch (IllegalThreadStateException itse) 484 { 485 logMessage("Attempt to read process output failed: " + ioe); 486 indicateCompletedWithErrors(); 487 } 488 489 return; 490 } 491 } 492 } 493 494 495 496 507 private static String [] byteArrayToStrings(byte[] byteArray, int length) 508 { 509 ArrayList stringList = new ArrayList(); 510 511 String byteStr = new String (byteArray, 0, length); 512 StringTokenizer tokenizer = new StringTokenizer(byteStr, "\r\n"); 513 while (tokenizer.hasMoreTokens()) 514 { 515 stringList.add(tokenizer.nextToken()); 516 } 517 518 String [] returnStrings = new String [stringList.size()]; 519 stringList.toArray(returnStrings); 520 return returnStrings; 521 } 522 } 523 524 | Popular Tags |