source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/CovMerge.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 6.3 KB
Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.sitraka;
19
20import java.io.File;
21import java.io.FileWriter;
22import java.io.IOException;
23import java.io.PrintWriter;
24import java.util.Vector;
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.ant.DirectoryScanner;
27import org.apache.tools.ant.Project;
28import org.apache.tools.ant.taskdefs.Execute;
29import org.apache.tools.ant.taskdefs.LogStreamHandler;
30import org.apache.tools.ant.types.Commandline;
31import org.apache.tools.ant.types.FileSet;
32
33/**
34 * Runs the snapshot merge utility for JProbe Coverage.
35 *
36 * @ant.task name="jpcovmerge" category="metrics"
37 */
38public class CovMerge extends CovBase {
39
40 /** the name of the output snapshot */
41 private File tofile = null;
42
43 /** the filesets that will get all snapshots to merge */
44 private Vector filesets = new Vector();
45
46 private boolean verbose;
47
48 /**
49 * Set the output snapshot file.
50 */
51 public void setTofile(File value) {
52 this.tofile = value;
53 }
54
55 /**
56 * If true, perform the merge in verbose mode giving details
57 * about the snapshot processing.
58 */
59 public void setVerbose(boolean flag) {
60 this.verbose = flag;
61 }
62
63 /**
64 * add a fileset containing the snapshots to include.
65 */
66 public void addFileset(FileSet fs) {
67 filesets.addElement(fs);
68 }
69
70 //---------------- the tedious job begins here
71
72 public CovMerge() {
73 }
74
75 /** execute the jpcovmerge by providing a parameter file */
76 public void execute() throws BuildException {
77 checkOptions();
78
79 File paramfile = createParamFile();
80 try {
81 Commandline cmdl = new Commandline();
82 cmdl.setExecutable(findExecutable("jpcovmerge"));
83 if (verbose) {
84 cmdl.createArgument().setValue("-v");
85 }
86 cmdl.createArgument().setValue(getParamFileArgument()
87 + paramfile.getAbsolutePath());
88
89 if (isJProbe4Plus()) {
90 // last argument is the output snapshot - JProbe 4.x
91 // doesn't like it in the parameter file.
92 cmdl.createArgument().setValue(tofile.getPath());
93 }
94
95 LogStreamHandler handler
96 = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
97 Execute exec = new Execute(handler);
98 log(cmdl.describeCommand(), Project.MSG_VERBOSE);
99 exec.setCommandline(cmdl.getCommandline());
100
101 // JProbe process always return 0 so we will not be
102 // able to check for failure ! :-(
103 int exitValue = exec.execute();
104 if (Execute.isFailure(exitValue)) {
105 throw new BuildException("JProbe Coverage Merging failed (" + exitValue + ")");
106 }
107 } catch (IOException e) {
108 throw new BuildException("Failed to run JProbe Coverage Merge: " + e);
109 } finally {
110 //@todo should be removed once switched to JDK1.2
111 paramfile.delete();
112 }
113 }
114
115 /** check for mandatory options */
116 protected void checkOptions() throws BuildException {
117 if (tofile == null) {
118 throw new BuildException("'tofile' attribute must be set.");
119 }
120
121 // check coverage home
122 if (getHome() == null || !getHome().isDirectory()) {
123 throw new BuildException("Invalid home directory. Must point to JProbe home directory");
124 }
125 File jar = findCoverageJar();
126 if (!jar.exists()) {
127 throw new BuildException("Cannot find Coverage directory: " + getHome());
128 }
129 }
130
131 /** get the snapshots from the filesets */
132 protected File[] getSnapshots() {
133 Vector v = new Vector();
134 final int size = filesets.size();
135 for (int i = 0; i < size; i++) {
136 FileSet fs = (FileSet) filesets.elementAt(i);
137 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
138 ds.scan();
139 String[] f = ds.getIncludedFiles();
140 for (int j = 0; j < f.length; j++) {
141 String pathname = f[j];
142 File file = new File(ds.getBasedir(), pathname);
143 file = getProject().resolveFile(file.getPath());
144 v.addElement(file);
145 }
146 }
147
148 File[] files = new File[v.size()];
149 v.copyInto(files);
150 return files;
151 }
152
153
154 /**
155 * create the parameters file that contains all file to merge
156 * and the output filename.
157 */
158 protected File createParamFile() throws BuildException {
159 File[] snapshots = getSnapshots();
160 File file = createTempFile("jpcovm");
161 file.deleteOnExit();
162 FileWriter fw = null;
163 try {
164 fw = new FileWriter(file);
165 PrintWriter pw = new PrintWriter(fw);
166 for (int i = 0; i < snapshots.length; i++) {
167 pw.println(snapshots[i].getAbsolutePath());
168 }
169 if (!isJProbe4Plus()) {
170 // last file is the output snapshot - JProbe 4.x doesn't
171 // like it in the parameter file.
172 pw.println(getProject().resolveFile(tofile.getPath()));
173 }
174 pw.flush();
175 } catch (IOException e) {
176 throw new BuildException("I/O error while writing to " + file, e);
177 } finally {
178 if (fw != null) {
179 try {
180 fw.close();
181 } catch (IOException ignored) {
182 }
183 }
184 }
185 return file;
186 }
187
188}
Note: See TracBrowser for help on using the repository browser.