2
* The Apache Software License, Version 1.1
4
* Copyright (c) 2000 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution, if
20
* any, must include the following acknowlegement:
21
* "This product includes software developed by the
22
* Apache Software Foundation (http://www.apache.org/)."
23
* Alternately, this acknowlegement may appear in the software itself,
24
* if and wherever such third-party acknowlegements normally appear.
26
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27
* Foundation" must not be used to endorse or promote products derived
28
* from this software without prior written permission. For written
29
* permission, please contact apache@apache.org.
31
* 5. Products derived from this software may not be called "Apache"
32
* nor may "Apache" appear in their names without prior written
33
* permission of the Apache Group.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
* ====================================================================
49
* This software consists of voluntary contributions made by many
50
* individuals on behalf of the Apache Software Foundation. For more
51
* information on the Apache Software Foundation, please see
52
* <http://www.apache.org/>.
55
package org.apache.tools.ant.taskdefs.optional.sitraka;
57
import java.io.PrintWriter;
58
import java.io.StringWriter;
59
import java.io.FileWriter;
61
import java.io.OutputStream;
62
import java.io.IOException;
63
import java.util.Vector;
64
import java.util.Random;
65
import java.util.Hashtable;
66
import org.apache.tools.ant.Project;
67
import org.apache.tools.ant.Task;
68
import org.apache.tools.ant.BuildException;
69
import org.apache.tools.ant.types.CommandlineJava;
70
import org.apache.tools.ant.types.Commandline;
71
import org.apache.tools.ant.types.EnumeratedAttribute;
72
import org.apache.tools.ant.types.Path;
73
import org.apache.tools.ant.types.FileSet;
74
import org.apache.tools.ant.taskdefs.Execute;
75
import org.apache.tools.ant.taskdefs.LogStreamHandler;
78
* Convenient task to run Sitraka JProbe Coverage from Ant.
79
* Options are pretty numerous, you'd better check the manual for a full
80
* descriptions of options. (not that simple since they differ from the online
81
* help, from the usage command line and from the examples...)
83
* For additional information, visit <a href="http://www.sitraka.com">www.sitraka.com</a>
85
* @author <a href="sbailliez@imediation.com">Stephane Bailliez</a>
87
public class Coverage extends Task {
91
protected Commandline cmdl = new Commandline();
93
protected CommandlineJava cmdlJava = new CommandlineJava();
95
protected String function = "coverage";
97
protected String seedName;
99
protected File inputFile;
101
protected File javaExe;
105
protected boolean applet = false;
107
/** this is a somewhat annoying thing, set it to never */
108
protected String exitPrompt = "never";
110
protected Filters filters = new Filters();
112
protected Triggers triggers;
114
protected String finalSnapshot = "coverage";
116
protected String recordFromStart = "coverage";
118
protected File snapshotDir;
120
protected File workingDir;
122
protected boolean trackNatives = false;
124
protected Socket socket;
126
protected int warnLevel = 0;
128
protected Vector filesets = new Vector();
130
//--------- setters used via reflection --
132
/** set the coverage home directory where are libraries, jars and jplauncher */
133
public void setHome(File value){
137
/** seed name for snapshot file. can be null, default to snap */
138
public void setSeedname(String value){
142
public void setInputfile(File value){
146
public void setJavaexe(File value){
150
public static class Javavm extends EnumeratedAttribute {
151
public String[] getValues(){
152
return new String[]{"java2", "jdk118", "jdk117"};
155
/** jdk117, jdk118 or java2, can be null, default to java2 */
156
public void setVm(Javavm value) {
157
vm = value.getValue();
160
/** default to false unless file is htm or html */
161
public void setApplet(boolean value){
165
/** always, error, never */
166
public void setExitprompt(String value){
170
public Filters createFilters(){
174
public Triggers createTriggers(){
175
if (triggers == null) {
176
triggers = new Triggers();
181
public Socket createSocket(){
182
if (socket == null ) {
183
socket = new Socket();
188
public static class Finalsnapshot extends EnumeratedAttribute {
189
public String[] getValues(){
190
return new String[]{"coverage", "none", "all"};
194
/** none, coverage, all. can be null, default to none */
195
public void setFinalsnapshot(String value){
196
finalSnapshot = value;
199
public static class Recordfromstart extends EnumeratedAttribute {
200
public String[] getValues(){
201
return new String[]{"coverage", "none", "all"};
204
/** all, coverage, none */
205
public void setRecordfromstart(Recordfromstart value) {
206
recordFromStart = value.getValue();
209
public void setWarnlevel(Integer value){
210
warnLevel = value.intValue();
213
public void setSnapshotdir(File value){
217
public void setWorkingdir(File value){
221
public void setTracknatives(boolean value){
222
trackNatives = value;
227
/** the jvm arguments */
228
public Commandline.Argument createJvmarg() {
229
return cmdlJava.createVmArgument();
232
/** the command arguments */
233
public Commandline.Argument createArg() {
234
return cmdlJava.createArgument();
237
/** classpath to run the files */
238
public Path createClasspath() {
239
return cmdlJava.createClasspath(project).createPath();
242
/** classname to run as standalone or runner for filesets */
243
public void setClassname(String value){
244
cmdlJava.setClassname(value);
247
/** the classnames to execute */
248
public void addFileset(FileSet fs){
249
filesets.addElement(fs);
253
//---------------- the tedious job begins here
258
/** execute the jplauncher by providing a parameter file */
259
public void execute() throws BuildException {
260
File paramfile = null;
261
// if an input file is used, all other options are ignored...
262
if (inputFile == null){
264
paramfile = createParamFile();
266
paramfile = inputFile;
269
// we need to run Coverage from his directory due to dll/jar issues
270
cmdl.setExecutable( new File(home, "jplauncher").getAbsolutePath() );
271
cmdl.createArgument().setValue("-jp_input=" + paramfile.getAbsolutePath());
273
// use the custom handler for stdin issues
274
LogStreamHandler handler = new CoverageStreamHandler(this);
275
Execute exec = new Execute( handler );
276
log(cmdl.toString(), Project.MSG_VERBOSE);
277
exec.setCommandline(cmdl.getCommandline());
278
int exitValue = exec.execute();
279
if (exitValue != 0) {
280
throw new BuildException("JProbe Coverage failed (" + exitValue + ")");
282
} catch (IOException e){
283
throw new BuildException("Failed to execute JProbe Coverage.", e);
285
//@todo should be removed once switched to JDK1.2
286
if (inputFile == null && paramfile != null){
292
/** wheck what is necessary to check, Coverage will do the job for us */
293
protected void checkOptions() throws BuildException {
294
// check coverage home
295
if (home == null || !home.isDirectory() ) {
296
throw new BuildException("Invalid home directory. Must point to JProbe home directory");
298
home = new File(home,"Coverage");
299
File jar = new File(home, "coverage.jar");
301
throw new BuildException("Cannot find Coverage directory: " + home);
304
// make sure snapshot dir exists and is resolved
305
if (snapshotDir == null) {
306
snapshotDir = new File(".");
308
snapshotDir = project.resolveFile(snapshotDir.getPath());
309
if (!snapshotDir.isDirectory() || !snapshotDir.exists()) {
310
throw new BuildException("Snapshot directory does not exists :" + snapshotDir);
312
if (workingDir == null) {
313
workingDir = new File(".");
315
workingDir = project.resolveFile(workingDir.getPath());
317
// check for info, do your best to select the java executable.
318
// JProbe 3.0 fails if there is no javaexe option. So
319
if (javaExe == null && ( vm == null || "java2".equals(vm) ) ) {
320
String version = System.getProperty("java.version");
321
// make we are using 1.2+, if it is, then do your best to
323
if ( !version.startsWith("1.1") ){
327
// if we are here obviously it is java2
328
String home = System.getProperty("java.home");
329
boolean isUnix = File.separatorChar == '/';
330
javaExe = isUnix ? new File(home, "bin/java") : new File(home,"/bin/java.exe");
336
* return the command line parameters. Parameters can either be passed
337
* to the command line and stored to a file (then use the -jp_input=<filename>)
338
* if they are too numerous.
340
protected String[] getParameters(){
341
Vector params = new Vector();
342
params.addElement("-jp_function=" + function);
344
params.addElement("-jp_vm=" + vm);
346
if (javaExe != null) {
347
params.addElement("-jp_java_exe=" + project.resolveFile(javaExe.getPath()));
349
params.addElement("-jp_working_dir=" + workingDir.getPath() );
350
params.addElement("-jp_snapshot_dir=" + snapshotDir.getPath() );
351
params.addElement("-jp_record_from_start=" + recordFromStart);
352
params.addElement("-jp_warn=" + warnLevel);
353
if (seedName != null) {
354
params.addElement("-jp_output_file=" + seedName);
356
params.addElement("-jp_filter=" + filters.toString() );
357
if (triggers != null) {
358
params.addElement("-jp_trigger=" + triggers.toString() );
360
if (finalSnapshot != null) {
361
params.addElement("-jp_final_snapshot=" + finalSnapshot);
363
params.addElement("-jp_exit_prompt=" + exitPrompt);
364
//params.addElement("-jp_append=" + append);
365
params.addElement("-jp_track_natives=" + trackNatives);
368
String[] vmargs = cmdlJava.getVmCommand().getArguments();
369
for (int i = 0; i < vmargs.length; i++) {
370
params.addElement(vmargs[i]);
373
Path classpath = cmdlJava.getClasspath();
374
if (classpath != null && classpath.size() > 0) {
375
params.addElement("-classpath " + classpath.toString());
377
// classname (runner or standalone)
378
if (cmdlJava.getClassname() != null) {
379
params.addElement(cmdlJava.getClassname());
381
// arguments for classname
382
String[] args = cmdlJava.getJavaCommand().getArguments();
383
for (int i = 0; i < args.length; i++) {
384
params.addElement(args[i]);
387
String[] array = new String[params.size()];
388
params.copyInto(array);
394
* create the parameter file from the given options. The file is
395
* created with a random name in the current directory.
396
* @return the file object where are written the configuration to run
398
* @throws BuildException thrown if something bad happens while writing
399
* the arguments to the file.
401
protected File createParamFile() throws BuildException {
402
//@todo change this when switching to JDK 1.2 and use File.createTmpFile()
403
File file = createTmpFile();
404
log("Creating parameter file: " + file, Project.MSG_VERBOSE);
406
// options need to be one per line in the parameter file
407
// so write them all in a single string
408
StringWriter sw = new StringWriter();
409
PrintWriter pw = new PrintWriter(sw);
410
String[] params = getParameters();
411
for (int i = 0; i < params.length; i++){
412
pw.println(params[i]);
415
log("JProbe Coverage parameters:\n" + sw.toString(), Project.MSG_VERBOSE);
417
// now write them to the file
418
FileWriter fw = null;
420
fw = new FileWriter(file);
421
fw.write(sw.toString());
423
} catch (IOException e){
424
throw new BuildException("Could not write parameter file " + file, e);
429
} catch (IOException ignored){}
435
/** create a temporary file in the current dir (For JDK1.1 support) */
436
protected File createTmpFile(){
437
final long rand = (new Random(System.currentTimeMillis())).nextLong();
438
File file = new File("jpcoverage" + rand + ".tmp");
442
/** specific pumper to avoid those nasty stdin issues */
443
static class CoverageStreamHandler extends LogStreamHandler {
444
CoverageStreamHandler(Task task){
445
super(task, Project.MSG_INFO, Project.MSG_WARN);
448
* there are some issues concerning all JProbe executable
449
* In our case a 'Press ENTER to close this window..." will
450
* be displayed in the current window waiting for enter.
451
* So I'm closing the stream right away to avoid problems.
453
public void setProcessInputStream(OutputStream os) {
456
} catch (IOException ignored){