| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.report; |
|---|
| 34 | |
|---|
| 35 | import java.io.File; |
|---|
| 36 | import java.io.IOException; |
|---|
| 37 | import java.util.ArrayList; |
|---|
| 38 | import java.util.Iterator; |
|---|
| 39 | import java.util.List; |
|---|
| 40 | |
|---|
| 41 | import org.apache.tools.ant.BuildException; |
|---|
| 42 | import org.apache.tools.ant.Project; |
|---|
| 43 | import org.apache.tools.ant.taskdefs.Execute; |
|---|
| 44 | import org.apache.tools.ant.taskdefs.LogStreamHandler; |
|---|
| 45 | import org.apache.tools.ant.taskdefs.MatchingTask; |
|---|
| 46 | import org.apache.tools.ant.types.Commandline; |
|---|
| 47 | import org.apache.tools.ant.types.CommandlineJava; |
|---|
| 48 | import org.apache.tools.ant.types.Environment; |
|---|
| 49 | import org.apache.tools.ant.types.Path; |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Report Normalizer Ant Task. |
|---|
| 53 | * |
|---|
| 54 | * This Task takes none, one or more input reports such as Checkstyle or PMD |
|---|
| 55 | * and generates a normalized report file (XML) for any Java source under |
|---|
| 56 | * <code>srcdir</code>. |
|---|
| 57 | * This version of the class JcoderzReportAntTask just creates the combined XML |
|---|
| 58 | * report. No further processing into a HTML report is done at this time. |
|---|
| 59 | * |
|---|
| 60 | * @author Michael Griffel (Michael.Griffel@jcoderz.org) |
|---|
| 61 | * @author Michael Rumpf (Michael.Rumpf@jcoderz.org) |
|---|
| 62 | */ |
|---|
| 63 | public final class ReportNormalizerAntTask |
|---|
| 64 | extends MatchingTask |
|---|
| 65 | { |
|---|
| 66 | /** the current working directory when forking JVM */ |
|---|
| 67 | private File mWorkingDir = null; |
|---|
| 68 | |
|---|
| 69 | /** Output directory for XML/HTML report */ |
|---|
| 70 | private File mOut = new File("."); |
|---|
| 71 | /** The Filter Filename */ |
|---|
| 72 | private File mFilter = null; |
|---|
| 73 | /** The project's name */ |
|---|
| 74 | private String mName = "Not defined"; |
|---|
| 75 | /** Flag: exit build process if an error occurred */ |
|---|
| 76 | private boolean mFailOnError = false; |
|---|
| 77 | /** The level of the report */ |
|---|
| 78 | private ReportLevel mLevel = ReportLevel.PROD; |
|---|
| 79 | /** List of input report of type JcoderzReportAntTask.Report */ |
|---|
| 80 | private final List mReportFiles = new ArrayList(); |
|---|
| 81 | /** The Java Commandline */ |
|---|
| 82 | private final CommandlineJava mCommandline = new CommandlineJava(); |
|---|
| 83 | /** |
|---|
| 84 | * List of source directories of type JcoderzReportAntTask.SourceDirectory. |
|---|
| 85 | */ |
|---|
| 86 | private final List mSourceDirectories = new ArrayList(); |
|---|
| 87 | |
|---|
| 88 | /** Debug output flag */ |
|---|
| 89 | private boolean mDebug = false; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Sets the output directory to given <code>dir</code>. |
|---|
| 94 | * This directory is used to store the XML/HTML report file(s). |
|---|
| 95 | * @param dir The output directory to set. |
|---|
| 96 | */ |
|---|
| 97 | public void setOut (File dir) |
|---|
| 98 | { |
|---|
| 99 | mOut = dir; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Sets the filter file to <code>f</code>. |
|---|
| 104 | * This file is used to filter to raw jcoderz XML report. |
|---|
| 105 | * |
|---|
| 106 | * @param f The filter file. |
|---|
| 107 | */ |
|---|
| 108 | public void setFilter (File f) |
|---|
| 109 | { |
|---|
| 110 | mFilter = f; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Sets the projectName to given <code>projectName</code>. |
|---|
| 115 | * @param projectName The projectName to set. |
|---|
| 116 | */ |
|---|
| 117 | public void setName (String projectName) |
|---|
| 118 | { |
|---|
| 119 | mName = projectName; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Sets the level to given <code>level</code>. |
|---|
| 124 | * @param level The level to set. |
|---|
| 125 | */ |
|---|
| 126 | public void setLevel (String level) |
|---|
| 127 | { |
|---|
| 128 | mLevel = ReportLevel.fromString(level); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * The directory to invoke the VM in. Ignored if no JVM is forked. |
|---|
| 133 | * @param dir the directory to invoke the JVM from. |
|---|
| 134 | */ |
|---|
| 135 | public void setDir (File dir) |
|---|
| 136 | { |
|---|
| 137 | mWorkingDir = dir; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Set whether we should fail on an error. |
|---|
| 142 | * |
|---|
| 143 | * @param b Whether we should fail on an error. |
|---|
| 144 | */ |
|---|
| 145 | public void setFailonerror (boolean b) |
|---|
| 146 | { |
|---|
| 147 | mFailOnError = b; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | public void setDebug (boolean b) |
|---|
| 151 | { |
|---|
| 152 | mDebug = b; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Adds a classpath to the task. |
|---|
| 157 | * @return The newly created classpath. |
|---|
| 158 | */ |
|---|
| 159 | public Path createClasspath () |
|---|
| 160 | { |
|---|
| 161 | return mCommandline.createClasspath(getProject()).createPath(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * Adds a bootclasspath to the task. |
|---|
| 166 | * @return The newly created bootclasspath. |
|---|
| 167 | */ |
|---|
| 168 | public Path createBootclasspath () |
|---|
| 169 | { |
|---|
| 170 | return mCommandline.createBootclasspath(getProject()).createPath(); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | * Adds a system property. |
|---|
| 175 | * |
|---|
| 176 | * @param sysp system property |
|---|
| 177 | */ |
|---|
| 178 | public void addSysproperty (Environment.Variable sysp) |
|---|
| 179 | { |
|---|
| 180 | mCommandline.addSysproperty(sysp); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | * Adds a JVM argument. |
|---|
| 185 | * |
|---|
| 186 | * @return The newly created Command line argument. |
|---|
| 187 | */ |
|---|
| 188 | public Commandline.Argument createJvmarg () |
|---|
| 189 | { |
|---|
| 190 | return mCommandline.createVmArgument(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** |
|---|
| 194 | * Adds a Report file such as PMD report or checkstyle report. |
|---|
| 195 | * @return The newly create report file. |
|---|
| 196 | */ |
|---|
| 197 | public Report createReport () |
|---|
| 198 | { |
|---|
| 199 | final Report ret = new Report(); |
|---|
| 200 | mReportFiles.add(ret); |
|---|
| 201 | return ret; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | /** |
|---|
| 205 | * Adds a source folder file such as PMD report or checkstyle report. |
|---|
| 206 | * @return The newly create report file. |
|---|
| 207 | */ |
|---|
| 208 | public SourceDirectory createSrcDir () |
|---|
| 209 | { |
|---|
| 210 | final SourceDirectory ret = new SourceDirectory(); |
|---|
| 211 | mSourceDirectories.add(ret); |
|---|
| 212 | return ret; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /** |
|---|
| 216 | * Execute this task. |
|---|
| 217 | * |
|---|
| 218 | * @throws BuildException An building exception occurred. |
|---|
| 219 | */ |
|---|
| 220 | public void execute () |
|---|
| 221 | throws BuildException |
|---|
| 222 | { |
|---|
| 223 | checkParameters(); |
|---|
| 224 | |
|---|
| 225 | try |
|---|
| 226 | { |
|---|
| 227 | int exitValue; |
|---|
| 228 | |
|---|
| 229 | exitValue = executeAsForked(); |
|---|
| 230 | |
|---|
| 231 | if (exitValue != 0) |
|---|
| 232 | { |
|---|
| 233 | final String msg = "ReportNormalizer returned with exit code '" |
|---|
| 234 | + exitValue + "'"; |
|---|
| 235 | log(msg, Project.MSG_WARN); |
|---|
| 236 | throw new BuildException(msg, getLocation()); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | catch (BuildException e) |
|---|
| 240 | { |
|---|
| 241 | if (mFailOnError) |
|---|
| 242 | { |
|---|
| 243 | throw e; |
|---|
| 244 | } |
|---|
| 245 | else |
|---|
| 246 | { |
|---|
| 247 | log(e.getMessage(), Project.MSG_ERR); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | /** |
|---|
| 254 | * Create a Java command line for executing the ReportNormalizer. |
|---|
| 255 | * |
|---|
| 256 | * @param suite The suite to run |
|---|
| 257 | * @return The Java command line. |
|---|
| 258 | */ |
|---|
| 259 | private CommandlineJava createCommandline () |
|---|
| 260 | { |
|---|
| 261 | final CommandlineJava cmd; |
|---|
| 262 | try |
|---|
| 263 | { |
|---|
| 264 | cmd = (CommandlineJava) mCommandline.clone(); |
|---|
| 265 | } |
|---|
| 266 | catch (CloneNotSupportedException unexpected) |
|---|
| 267 | { |
|---|
| 268 | throw new RuntimeException( |
|---|
| 269 | "Ups, CommandLineJava doesn't support the method clone()", |
|---|
| 270 | unexpected); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | cmd.setClassname(ReportNormalizer.class.getName()); |
|---|
| 274 | |
|---|
| 275 | // WOW cmd.createVmArgument().setValue("-Xmx1500m"); |
|---|
| 276 | |
|---|
| 277 | cmd.createArgument().setValue("-out"); |
|---|
| 278 | cmd.createArgument().setFile(mOut); |
|---|
| 279 | |
|---|
| 280 | cmd.createArgument().setValue("-projectName"); |
|---|
| 281 | cmd.createArgument().setValue(mName); |
|---|
| 282 | |
|---|
| 283 | cmd.createArgument().setValue("-level"); |
|---|
| 284 | cmd.createArgument().setValue(mLevel.toString()); |
|---|
| 285 | |
|---|
| 286 | cmd.createArgument().setValue("-loglevel"); |
|---|
| 287 | if (mDebug) |
|---|
| 288 | { |
|---|
| 289 | cmd.createArgument().setValue("ALL"); |
|---|
| 290 | } |
|---|
| 291 | else |
|---|
| 292 | { |
|---|
| 293 | cmd.createArgument().setValue("INFO"); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | if (mFilter != null) |
|---|
| 297 | { |
|---|
| 298 | cmd.createArgument().setValue("-filter"); |
|---|
| 299 | cmd.createArgument().setFile(mFilter); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | for (final Iterator iterator = mSourceDirectories.iterator(); |
|---|
| 303 | iterator.hasNext();) |
|---|
| 304 | { |
|---|
| 305 | final SourceDirectory sourceDir = (SourceDirectory) iterator.next(); |
|---|
| 306 | cmd.createArgument().setValue("-srcDir"); |
|---|
| 307 | cmd.createArgument().setPath( |
|---|
| 308 | new Path(getProject(), sourceDir.getDir())); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | for (final Iterator iterator = mReportFiles.iterator(); |
|---|
| 312 | iterator.hasNext();) |
|---|
| 313 | { |
|---|
| 314 | final Report reportFile = (Report) iterator.next(); |
|---|
| 315 | if (reportFile.testIfCondition()) |
|---|
| 316 | { |
|---|
| 317 | cmd.createArgument().setValue("-" + reportFile.getFormat()); |
|---|
| 318 | cmd.createArgument().setFile(reportFile.getFile()); |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | return cmd; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | private int executeAsForked () |
|---|
| 326 | throws BuildException |
|---|
| 327 | { |
|---|
| 328 | final Execute execute |
|---|
| 329 | = new Execute(new LogStreamHandler( |
|---|
| 330 | this, Project.MSG_INFO, Project.MSG_WARN)); |
|---|
| 331 | |
|---|
| 332 | final CommandlineJava cmd = createCommandline(); |
|---|
| 333 | execute.setCommandline(cmd.getCommandline()); |
|---|
| 334 | |
|---|
| 335 | if (mWorkingDir != null) |
|---|
| 336 | { |
|---|
| 337 | log("Setting working directory to : " |
|---|
| 338 | + mWorkingDir, Project.MSG_VERBOSE); |
|---|
| 339 | execute.setWorkingDirectory(mWorkingDir); |
|---|
| 340 | execute.setAntRun(getProject()); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | final Path classpath = mCommandline.getClasspath(); |
|---|
| 344 | if (classpath != null) |
|---|
| 345 | { |
|---|
| 346 | log("Using CLASSPATH " + classpath, Project.MSG_VERBOSE); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | log("Executing: [fork] " + cmd.toString(), Project.MSG_VERBOSE); |
|---|
| 350 | |
|---|
| 351 | try |
|---|
| 352 | { |
|---|
| 353 | return execute.execute(); |
|---|
| 354 | } |
|---|
| 355 | catch (IOException e) |
|---|
| 356 | { |
|---|
| 357 | throw new BuildException("Process fork failed.", e, getLocation()); |
|---|
| 358 | } |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | private void checkParameters () |
|---|
| 362 | { |
|---|
| 363 | if (mSourceDirectories.size() == 0) |
|---|
| 364 | { |
|---|
| 365 | throw new BuildException( |
|---|
| 366 | "at least one srcdir element must be specified!", getLocation()); |
|---|
| 367 | } |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | /** This class represents an input report with a format and filename. */ |
|---|
| 371 | public final class Report |
|---|
| 372 | { |
|---|
| 373 | private ReportFormat mReportFormat; |
|---|
| 374 | private File mReportFilename; |
|---|
| 375 | private String mIfCondition = ""; |
|---|
| 376 | |
|---|
| 377 | /** |
|---|
| 378 | * Returns the reportFilename. |
|---|
| 379 | * @return the reportFilename. |
|---|
| 380 | */ |
|---|
| 381 | public File getFile () |
|---|
| 382 | { |
|---|
| 383 | return mReportFilename; |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | /** |
|---|
| 387 | * Sets the reportFilename to given <code>reportFilename</code>. |
|---|
| 388 | * @param reportFilename The reportFilename to set. |
|---|
| 389 | */ |
|---|
| 390 | public void setFile (File reportFilename) |
|---|
| 391 | { |
|---|
| 392 | mReportFilename = reportFilename; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | /** |
|---|
| 396 | * Returns the reportFormat. |
|---|
| 397 | * @return the reportFormat. |
|---|
| 398 | */ |
|---|
| 399 | public ReportFormat getFormat () |
|---|
| 400 | { |
|---|
| 401 | return mReportFormat; |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | /** |
|---|
| 405 | * Sets the reportFormat to given <code>reportFormat</code>. |
|---|
| 406 | * @param reportFormat The reportFormat to set. |
|---|
| 407 | */ |
|---|
| 408 | public void setFormat (String reportFormat) |
|---|
| 409 | { |
|---|
| 410 | mReportFormat = ReportFormat.fromString(reportFormat); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | /** |
|---|
| 414 | * Only use report file if the property is set to <code>true</code>. |
|---|
| 415 | * @param property the property name to check. |
|---|
| 416 | */ |
|---|
| 417 | public void setif (String property) |
|---|
| 418 | { |
|---|
| 419 | mIfCondition = (property == null) ? "" : property; |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | /** |
|---|
| 423 | * Tests whether or not the "if" condition is satisfied. |
|---|
| 424 | * |
|---|
| 425 | * @return whether or not the "if" condition is satisfied. If no |
|---|
| 426 | * condition (or an empty condition) has been set, |
|---|
| 427 | * <code>true</code> is returned. |
|---|
| 428 | */ |
|---|
| 429 | private boolean testIfCondition () |
|---|
| 430 | { |
|---|
| 431 | final boolean result; |
|---|
| 432 | if ("".equals(mIfCondition)) |
|---|
| 433 | { |
|---|
| 434 | result = true; |
|---|
| 435 | } |
|---|
| 436 | else |
|---|
| 437 | { |
|---|
| 438 | final String test = getProject().replaceProperties(mIfCondition); |
|---|
| 439 | result = getProject().getProperty(test) != null; |
|---|
| 440 | } |
|---|
| 441 | return result; |
|---|
| 442 | } |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | public static final class SourceDirectory |
|---|
| 446 | { |
|---|
| 447 | private String mSourceDir; |
|---|
| 448 | /** |
|---|
| 449 | * Returns the sourceDir. |
|---|
| 450 | * @return the sourceDir. |
|---|
| 451 | */ |
|---|
| 452 | public String getDir () |
|---|
| 453 | { |
|---|
| 454 | return mSourceDir; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | /** |
|---|
| 458 | * Sets the sourceDir to given <code>sourceDir</code>. |
|---|
| 459 | * @param sourceDir The sourceDir to set. |
|---|
| 460 | */ |
|---|
| 461 | public void setDir (String sourceDir) |
|---|
| 462 | { |
|---|
| 463 | mSourceDir = sourceDir; |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | } |
|---|