KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sitraka > CovMerge


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
18 package org.apache.tools.ant.taskdefs.optional.sitraka;
19
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Vector JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.DirectoryScanner;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.taskdefs.Execute;
29 import org.apache.tools.ant.taskdefs.LogStreamHandler;
30 import org.apache.tools.ant.types.Commandline;
31 import 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  */

38 public class CovMerge extends CovBase {
39
40     /** the name of the output snapshot */
41     private File JavaDoc tofile = null;
42
43     /** the filesets that will get all snapshots to merge */
44     private Vector JavaDoc filesets = new Vector JavaDoc();
45
46     private boolean verbose;
47
48     /**
49      * Set the output snapshot file.
50      */

51     public void setTofile(File JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] getSnapshots() {
133         Vector JavaDoc v = new Vector JavaDoc();
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 JavaDoc[] f = ds.getIncludedFiles();
140             for (int j = 0; j < f.length; j++) {
141                 String JavaDoc pathname = f[j];
142                 File JavaDoc file = new File JavaDoc(ds.getBasedir(), pathname);
143                 file = getProject().resolveFile(file.getPath());
144                 v.addElement(file);
145             }
146         }
147
148         File JavaDoc[] files = new File JavaDoc[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 JavaDoc createParamFile() throws BuildException {
159         File JavaDoc[] snapshots = getSnapshots();
160         File JavaDoc file = createTempFile("jpcovm");
161         file.deleteOnExit();
162         FileWriter JavaDoc fw = null;
163         try {
164             fw = new FileWriter JavaDoc(file);
165             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(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 JavaDoc 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 JavaDoc ignored) {
182                 }
183             }
184         }
185         return file;
186     }
187
188 }
189
Popular Tags