1 17 18 package org.apache.tools.ant.taskdefs.optional.sitraka; 19 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.util.Vector ; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.Task; 30 import org.apache.tools.ant.taskdefs.Execute; 31 import org.apache.tools.ant.taskdefs.LogStreamHandler; 32 import org.apache.tools.ant.types.Commandline; 33 import org.apache.tools.ant.types.CommandlineJava; 34 import org.apache.tools.ant.types.EnumeratedAttribute; 35 import org.apache.tools.ant.types.FileSet; 36 import org.apache.tools.ant.types.Path; 37 import org.apache.tools.ant.util.JavaEnvUtils; 38 39 51 public class Coverage extends CovBase { 52 53 protected Commandline cmdl = new Commandline(); 54 55 protected CommandlineJava cmdlJava = new CommandlineJava(); 56 57 protected String function = "coverage"; 58 59 protected String seedName; 60 61 protected File inputFile; 62 63 protected File javaExe; 64 65 protected String vm; 66 67 protected boolean applet = false; 68 69 70 protected String exitPrompt = "never"; 71 72 protected Filters filters = new Filters(); 73 74 protected Triggers triggers; 75 76 protected String finalSnapshot = "coverage"; 77 78 protected String recordFromStart = "coverage"; 79 80 protected File snapshotDir; 81 82 protected File workingDir; 83 84 protected boolean trackNatives = false; 85 86 protected Socket socket; 87 88 protected int warnLevel = 0; 89 90 protected Vector filesets = new Vector (); 91 92 94 95 public void setSeedname(String value) { 96 seedName = value; 97 } 98 99 102 public void setInputfile(File value) { 103 inputFile = value; 104 } 105 106 109 public void setJavaexe(File value) { 110 javaExe = value; 111 } 112 113 public static class Javavm extends EnumeratedAttribute { 114 public String [] getValues() { 115 return new String []{"java2", "jdk118", "jdk117"}; 116 } 117 } 118 119 122 public void setVm(Javavm value) { 123 vm = value.getValue(); 124 } 125 126 129 public void setApplet(boolean value) { 130 applet = value; 131 } 132 133 136 public void setExitprompt(String value) { 137 exitPrompt = value; 138 } 139 140 144 public Filters createFilters() { 145 return filters; 146 } 147 148 156 public Triggers createTriggers() { 157 if (triggers == null) { 158 triggers = new Triggers(); 159 } 160 return triggers; 161 } 162 163 167 public Socket createSocket() { 168 if (socket == null) { 169 socket = new Socket(); 170 } 171 return socket; 172 } 173 174 public static class Finalsnapshot extends EnumeratedAttribute { 175 public String [] getValues() { 176 return new String []{"coverage", "none", "all"}; 177 } 178 } 179 180 184 public void setFinalsnapshot(String value) { 185 finalSnapshot = value; 186 } 187 188 public static class Recordfromstart extends EnumeratedAttribute { 189 public String [] getValues() { 190 return new String []{"coverage", "none", "all"}; 191 } 192 } 193 194 197 public void setRecordfromstart(Recordfromstart value) { 198 recordFromStart = value.getValue(); 199 } 200 201 204 public void setWarnlevel(Integer value) { 205 warnLevel = value.intValue(); 206 } 207 208 214 public void setSnapshotdir(File value) { 215 snapshotDir = value; 216 } 217 218 221 public void setWorkingdir(File value) { 222 workingDir = value; 223 } 224 225 228 public void setTracknatives(boolean value) { 229 trackNatives = value; 230 } 231 232 234 237 public Commandline.Argument createJvmarg() { 238 return cmdlJava.createVmArgument(); 239 } 240 241 244 public Commandline.Argument createArg() { 245 return cmdlJava.createArgument(); 246 } 247 248 251 public Path createClasspath() { 252 return cmdlJava.createClasspath(getProject()).createPath(); 253 } 254 255 258 public void setClassname(String value) { 259 cmdlJava.setClassname(value); 260 } 261 262 265 public void addFileset(FileSet fs) { 266 filesets.addElement(fs); 267 } 268 269 270 272 public Coverage() { 273 } 274 275 276 public void execute() throws BuildException { 277 File paramfile = null; 278 if (inputFile == null) { 280 checkOptions(); 281 paramfile = createParamFile(); 282 } else { 283 paramfile = inputFile; 284 } 285 try { 286 cmdl.setExecutable(findExecutable("jplauncher")); 288 cmdl.createArgument().setValue("-jp_input=" + paramfile.getAbsolutePath()); 289 290 LogStreamHandler handler = new CoverageStreamHandler(this); 292 Execute exec = new Execute(handler); 293 log(cmdl.describeCommand(), Project.MSG_VERBOSE); 294 exec.setCommandline(cmdl.getCommandline()); 295 int exitValue = exec.execute(); 296 if (Execute.isFailure(exitValue)) { 297 throw new BuildException("JProbe Coverage failed (" + exitValue + ")"); 298 } 299 } catch (IOException e) { 300 throw new BuildException("Failed to execute JProbe Coverage.", e); 301 } finally { 302 if (inputFile == null && paramfile != null) { 304 paramfile.delete(); 305 } 306 } 307 } 308 309 310 protected void checkOptions() throws BuildException { 311 if (getHome() == null || !getHome().isDirectory()) { 313 throw new BuildException("Invalid home directory. Must point to JProbe home directory"); 314 } 315 File jar = findCoverageJar(); 316 if (!jar.exists()) { 317 throw new BuildException("Cannot find Coverage directory: " + getHome()); 318 } 319 320 if (snapshotDir == null) { 322 snapshotDir = new File ("."); 323 } 324 snapshotDir = getProject().resolveFile(snapshotDir.getPath()); 325 if (!snapshotDir.isDirectory() || !snapshotDir.exists()) { 326 throw new BuildException("Snapshot directory does not exists :" + snapshotDir); 327 } 328 if (workingDir == null) { 329 workingDir = new File ("."); 330 } 331 workingDir = getProject().resolveFile(workingDir.getPath()); 332 333 if (javaExe == null && (vm == null || "java2".equals(vm))) { 336 if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) { 337 if (vm == null) { 338 vm = "java2"; 339 } 340 javaExe = new File (JavaEnvUtils.getJreExecutable("java")); 341 } 342 } 343 } 344 345 350 protected String [] getParameters() { 351 Vector params = new Vector (); 352 params.addElement("-jp_function=" + function); 353 if (vm != null) { 354 params.addElement("-jp_vm=" + vm); 355 } 356 if (javaExe != null) { 357 params.addElement("-jp_java_exe=" + getProject().resolveFile(javaExe.getPath())); 358 } 359 params.addElement("-jp_working_dir=" + workingDir.getPath()); 360 params.addElement("-jp_snapshot_dir=" + snapshotDir.getPath()); 361 params.addElement("-jp_record_from_start=" + recordFromStart); 362 params.addElement("-jp_warn=" + warnLevel); 363 if (seedName != null) { 364 params.addElement("-jp_output_file=" + seedName); 365 } 366 params.addElement("-jp_filter=" + filters.toString()); 367 if (triggers != null) { 368 params.addElement("-jp_trigger=" + triggers.toString()); 369 } 370 if (finalSnapshot != null) { 371 params.addElement("-jp_final_snapshot=" + finalSnapshot); 372 } 373 params.addElement("-jp_exit_prompt=" + exitPrompt); 374 params.addElement("-jp_track_natives=" + trackNatives); 376 String [] vmargs = cmdlJava.getVmCommand().getArguments(); 379 for (int i = 0; i < vmargs.length; i++) { 380 params.addElement(vmargs[i]); 381 } 382 Path classpath = cmdlJava.getClasspath(); 384 if (classpath != null && classpath.size() > 0) { 385 params.addElement("-classpath " + classpath.toString()); 386 } 387 if (cmdlJava.getClassname() != null) { 389 params.addElement(cmdlJava.getClassname()); 390 } 391 String [] args = cmdlJava.getJavaCommand().getArguments(); 393 for (int i = 0; i < args.length; i++) { 394 params.addElement(args[i]); 395 } 396 397 String [] array = new String [params.size()]; 398 params.copyInto(array); 399 return array; 400 } 401 402 403 411 protected File createParamFile() throws BuildException { 412 File file = createTempFile("jpcov"); 414 file.deleteOnExit(); 415 log("Creating parameter file: " + file, Project.MSG_VERBOSE); 416 417 StringWriter sw = new StringWriter (); 420 PrintWriter pw = new PrintWriter (sw); 421 String [] params = getParameters(); 422 for (int i = 0; i < params.length; i++) { 423 pw.println(params[i]); 424 } 425 pw.flush(); 426 log("JProbe Coverage parameters:\n" + sw.toString(), Project.MSG_VERBOSE); 427 428 FileWriter fw = null; 430 try { 431 fw = new FileWriter (file); 432 fw.write(sw.toString()); 433 fw.flush(); 434 } catch (IOException e) { 435 throw new BuildException("Could not write parameter file " + file, e); 436 } finally { 437 if (fw != null) { 438 try { 439 fw.close(); 440 } catch (IOException ignored) { 441 } 442 } 443 } 444 return file; 445 } 446 447 448 static class CoverageStreamHandler extends LogStreamHandler { 449 CoverageStreamHandler(Task task) { 450 super(task, Project.MSG_INFO, Project.MSG_WARN); 451 } 452 453 459 public void setProcessInputStream(OutputStream os) { 460 try { 461 os.close(); 462 } catch (IOException ignored) { 463 } 464 } 465 } 466 467 } 468 | Popular Tags |