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. package org.apache.tools.ant.taskdefs.optional.sitraka;
  18. import java.io.File;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.io.PrintWriter;
  22. import java.util.Vector;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.DirectoryScanner;
  25. import org.apache.tools.ant.Project;
  26. import org.apache.tools.ant.taskdefs.Execute;
  27. import org.apache.tools.ant.taskdefs.LogStreamHandler;
  28. import org.apache.tools.ant.types.Commandline;
  29. import org.apache.tools.ant.types.FileSet;
  30. /**
  31. * Runs the snapshot merge utility for JProbe Coverage.
  32. *
  33. * @ant.task name="jpcovmerge" category="metrics"
  34. */
  35. public class CovMerge extends CovBase {
  36. /** the name of the output snapshot */
  37. private File tofile = null;
  38. /** the filesets that will get all snapshots to merge */
  39. private Vector filesets = new Vector();
  40. private boolean verbose;
  41. /**
  42. * Set the output snapshot file.
  43. */
  44. public void setTofile(File value) {
  45. this.tofile = value;
  46. }
  47. /**
  48. * If true, perform the merge in verbose mode giving details
  49. * about the snapshot processing.
  50. */
  51. public void setVerbose(boolean flag) {
  52. this.verbose = flag;
  53. }
  54. /**
  55. * add a fileset containing the snapshots to include.
  56. */
  57. public void addFileset(FileSet fs) {
  58. filesets.addElement(fs);
  59. }
  60. //---------------- the tedious job begins here
  61. public CovMerge() {
  62. }
  63. /** execute the jpcovmerge by providing a parameter file */
  64. public void execute() throws BuildException {
  65. checkOptions();
  66. File paramfile = createParamFile();
  67. try {
  68. Commandline cmdl = new Commandline();
  69. cmdl.setExecutable(findExecutable("jpcovmerge"));
  70. if (verbose) {
  71. cmdl.createArgument().setValue("-v");
  72. }
  73. cmdl.createArgument().setValue(getParamFileArgument()
  74. + paramfile.getAbsolutePath());
  75. if (isJProbe4Plus()) {
  76. // last argument is the output snapshot - JProbe 4.x
  77. // doesn't like it in the parameter file.
  78. cmdl.createArgument().setValue(tofile.getPath());
  79. }
  80. LogStreamHandler handler
  81. = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
  82. Execute exec = new Execute(handler);
  83. log(cmdl.describeCommand(), Project.MSG_VERBOSE);
  84. exec.setCommandline(cmdl.getCommandline());
  85. // JProbe process always return 0 so we will not be
  86. // able to check for failure ! :-(
  87. int exitValue = exec.execute();
  88. if (Execute.isFailure(exitValue)) {
  89. throw new BuildException("JProbe Coverage Merging failed (" + exitValue + ")");
  90. }
  91. } catch (IOException e) {
  92. throw new BuildException("Failed to run JProbe Coverage Merge: " + e);
  93. } finally {
  94. //@todo should be removed once switched to JDK1.2
  95. paramfile.delete();
  96. }
  97. }
  98. /** check for mandatory options */
  99. protected void checkOptions() throws BuildException {
  100. if (tofile == null) {
  101. throw new BuildException("'tofile' attribute must be set.");
  102. }
  103. // check coverage home
  104. if (getHome() == null || !getHome().isDirectory()) {
  105. throw new BuildException("Invalid home directory. Must point to JProbe home directory");
  106. }
  107. File jar = findCoverageJar();
  108. if (!jar.exists()) {
  109. throw new BuildException("Cannot find Coverage directory: " + getHome());
  110. }
  111. }
  112. /** get the snapshots from the filesets */
  113. protected File[] getSnapshots() {
  114. Vector v = new Vector();
  115. final int size = filesets.size();
  116. for (int i = 0; i < size; i++) {
  117. FileSet fs = (FileSet) filesets.elementAt(i);
  118. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  119. ds.scan();
  120. String[] f = ds.getIncludedFiles();
  121. for (int j = 0; j < f.length; j++) {
  122. String pathname = f[j];
  123. File file = new File(ds.getBasedir(), pathname);
  124. file = getProject().resolveFile(file.getPath());
  125. v.addElement(file);
  126. }
  127. }
  128. File[] files = new File[v.size()];
  129. v.copyInto(files);
  130. return files;
  131. }
  132. /**
  133. * create the parameters file that contains all file to merge
  134. * and the output filename.
  135. */
  136. protected File createParamFile() throws BuildException {
  137. File[] snapshots = getSnapshots();
  138. File file = createTempFile("jpcovm");
  139. file.deleteOnExit();
  140. FileWriter fw = null;
  141. try {
  142. fw = new FileWriter(file);
  143. PrintWriter pw = new PrintWriter(fw);
  144. for (int i = 0; i < snapshots.length; i++) {
  145. pw.println(snapshots[i].getAbsolutePath());
  146. }
  147. if (!isJProbe4Plus()) {
  148. // last file is the output snapshot - JProbe 4.x doesn't
  149. // like it in the parameter file.
  150. pw.println(getProject().resolveFile(tofile.getPath()));
  151. }
  152. pw.flush();
  153. } catch (IOException e) {
  154. throw new BuildException("I/O error while writing to " + file, e);
  155. } finally {
  156. if (fw != null) {
  157. try {
  158. fw.close();
  159. } catch (IOException ignored) {
  160. }
  161. }
  162. }
  163. return file;
  164. }
  165. }