net.sf.sanity4j.workflow.QAConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
The newest version!
package net.sf.sanity4j.workflow;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.StringTokenizer;
import net.sf.sanity4j.maven.plugin.RunQAMojo;
import net.sf.sanity4j.util.FileUtil;
import net.sf.sanity4j.util.QAException;
import net.sf.sanity4j.util.QaLogger;
import net.sf.sanity4j.util.QaUtil;
import net.sf.sanity4j.util.StringUtil;
import net.sf.sanity4j.util.Tool;
/**
* This class holds all the configuration attributes for the QA process.
*
* @author Yiannis Paschalidis
* @since Sanity4J 1.0
*/
public final class QAConfig
{
/** The location of the "tools" configuration properties file. */
private static final String TOOL_PROPERTIES = "/net/sf/sanity4j/workflow/tool/tools.properties";
/** The property key for the tools to be run. */
private static final String TOOLS_TO_RUN_PROPERTY = "sanity4j.toolsToRun";
/** Prefix used within properties for QA tool properties. */
private static final String QA_TOOL_PREFIX = "sanity4j.tool.";
/**
* A list of source file paths.
*/
private final List sources = new ArrayList();
/**
* A list of class file paths.
*/
private final List classes = new ArrayList();
/**
* A list of library file paths.
*/
private final List libraries = new ArrayList();
/**
* The location of the directory containing the various tools.
*/
private String productsDir;
/**
* The RunQAMojo;
*/
private RunQAMojo runQAMojo;
/**
* A flag indicating whether diagnostics should be displayed first.
*/
private boolean diagnosticsFirst;
/**
* The directory in which to place report output.
*/
private String reportDir;
/**
* The file containing the merged jUnit coverage data.
*/
private String coverageMergeDataFile;
/**
* The list of files containing the jUnit coverage data.
*/
private final List coverageDataFiles = new ArrayList();
/**
* The summary data file, if used.
*/
private String summaryDataFile;
/**
* The temporary directory.
*/
private File tempDir = new File(System.getProperty("java.io.tmpdir"), "sanity4j-temp-" + System.currentTimeMillis());
/**
* The java runtime to use when running external tasks.
*/
private String javaRuntime = QAProcessor.DEFAULT_JAVA_RUNTIME;
/**
* The java arguments to use when running external tasks.
*/
private String javaArgs = QAProcessor.JAVA_RUNTIME_MAX_MEMORY;
/**
* A Map from a tool / version to a configuration file.
*/
private final Map toolConfig = new HashMap();
/**
* If true, the raw tool output is included in the report directory.
*/
private boolean includeToolOutput = false;
/**
* The number of threads to use to run the tools and produce the report output.
*/
private int numThreads = 1; // TODO: Add support for concurrent tasks (Note: some tasks can not be run in parallel).
/**
* The configuration properties. This is a combination of the internal defaults {@link #TOOL_PROPERTIES}
* and the {@link #externalPropertiesPath}.
*/
private Properties properties = QaUtil.getProperties(TOOL_PROPERTIES);
/**
* Adds a source path to the list of source paths to analyse.
*
* @param sourcePath the source path to add.
*/
public void addSourcePath(final String sourcePath)
{
if (!sources.contains(sourcePath))
{
if (new File(sourcePath).exists())
{
sources.add(sourcePath);
}
}
}
/**
* Adds a class path to the list of class paths to analyse.
*
* @param classPath the class path to add.
*/
public void addClassPath(final String classPath)
{
if (!classes.contains(classPath))
{
if (new File(classPath).exists())
{
classes.add(classPath);
}
}
}
/**
* Adds a library path to the list of library paths.
*
* @param libraryPath the library path to add.
*/
public void addLibraryPath(final String libraryPath)
{
if (!libraries.contains(libraryPath))
{
if (new File(libraryPath).exists())
{
libraries.add(libraryPath);
}
}
}
/**
* @return the list of source directories to analyse.
*/
public List getSourceDirs()
{
return sources;
}
/**
* @return the list of class directories to analyse.
*/
public List getClassDirs()
{
return classes;
}
/**
* @return the list of library directories that are necessary for analysis.
*/
public List getLibraryDirs()
{
return libraries;
}
/**
* @return Returns the java runtime.
*/
public String getJavaRuntime()
{
return javaRuntime;
}
/**
* @param javaRuntime The javaRuntime to set.
*/
public void setJavaRuntime(final String javaRuntime)
{
if (javaRuntime == null || javaRuntime.length() == 0)
{
this.javaRuntime = QAProcessor.DEFAULT_JAVA_RUNTIME;
}
else
{
this.javaRuntime = javaRuntime;
}
}
/**
* @return Returns the java arguments.
*/
public String getJavaArgs()
{
return javaArgs;
}
/**
* @param javaRuntime The javaRuntime to set.
*/
public void setJavaArgs(final String javaArgs)
{
if (javaArgs == null || javaArgs.length() == 0)
{
this.javaArgs = QAProcessor.JAVA_RUNTIME_MAX_MEMORY;
}
else
{
this.javaArgs = javaArgs;
}
}
/**
* @return Returns the productsDir.
*/
public String getProductsDir()
{
return productsDir;
}
/**
* @param productsDir The productsDir to set.
*/
public void setProductsDir(final String productsDir)
{
this.productsDir = productsDir;
}
/**
* @return Returns the RunQAMojo.
*/
public RunQAMojo getRunQAMojo()
{
return runQAMojo;
}
/**
* @param runQAMojo The RunQAMojo to set.
*/
public void setRunQAMojo(final RunQAMojo runQAMojo)
{
this.runQAMojo = runQAMojo;
}
/**
* @return Returns the reportDir.
*/
public String getReportDir()
{
return reportDir;
}
/**
* @param reportDir The reportDir to set.
*/
public void setReportDir(final String reportDir)
{
this.reportDir = reportDir;
}
/**
* @param coverageDataFile The coverage data file to set.
*/
public void setCoverageDataFile(final String coverageDataFile)
{
addCoverageDataFile(coverageDataFile);
}
/**
* @param coverageDataFile The coverage data file to add to the list.
*/
public void addCoverageDataFile(final String coverageDataFile)
{
if (coverageDataFile != null && coverageDataFile.length() > 0)
{
File file = new File(coverageDataFile);
if (file.exists() && file.isFile())
{
if (coverageDataFiles.contains(coverageDataFile))
{
QaLogger.getInstance().debug("Coverage data file is already in the list: " + coverageDataFile);
}
else
{
coverageDataFiles.add(coverageDataFile);
QaLogger.getInstance().debug("Added coverage data file to list: " + coverageDataFile);
}
}
else
{
QaLogger.getInstance().warn("Unable to locate coverage data file: " + coverageDataFile);
}
}
else
{
QaLogger.getInstance().debug("Invalid coverage data file name: " + coverageDataFile);
}
}
/**
* @return Returns the coverage data file.
*/
public String getCoverageDataFile()
{
if (coverageDataFiles.size() < 1)
{
return null;
}
else if (coverageDataFiles.size() > 1)
{
return getCoverageMergeDataFile();
}
else
{
return coverageDataFiles.get(0);
}
}
/**
* @return Returns the coverage data file.
*/
public String getCoverageMergeDataFiles()
{
if (coverageDataFiles.size() < 1)
{
return null;
}
else if (coverageDataFiles.size() > 1)
{
StringBuilder builder = new StringBuilder();
int index = 0;
for (String coverageDataFile : coverageDataFiles)
{
builder.append(coverageDataFile);
if (++index < coverageDataFiles.size())
{
builder.append(" ");
}
}
return builder.toString();
}
else
{
return coverageDataFiles.get(0);
}
}
/**
* @return The number of coverage data files.
*/
public int getCoverageDataFileCount()
{
return coverageDataFiles.size();
}
/**
* @param summaryDataFile The summaryDataFile to set.
*/
public void setSummaryDataFile(final String summaryDataFile)
{
this.summaryDataFile = summaryDataFile;
}
/**
* @return Returns the summaryDataFile.
*/
public String getSummaryDataFile()
{
return summaryDataFile;
}
/**
* @return Returns the includeToolOutput.
*/
public boolean isIncludeToolOutput()
{
return includeToolOutput;
}
/**
* @param includeToolOutput The includeToolOutput to set.
*/
public void setIncludeToolOutput(final boolean includeToolOutput)
{
this.includeToolOutput = includeToolOutput;
}
/**
* Sets the maximum number of WorkUnits which can be run concurrently.
*
* @param numThreads the number of simultaneous WorkUnits to allow
*/
public void setNumThreads(final int numThreads)
{
this.numThreads = numThreads;
}
/**
* @return the number of simultaneous WorkUnits to allow
*/
public int getNumThreads()
{
return numThreads;
}
/**
* @return the temporary directory used during analysis.
*/
public File getTempDir()
{
return tempDir;
}
/**
* Sets the temporary directory used during analysis.
*
* @param tempDir the temporary directory.
*/
public void setTempDir(final File tempDir)
{
if (tempDir == null)
{
this.tempDir = new File(System.getProperty("java.io.tmpdir"), "sanity4j-temp-" + System.currentTimeMillis());
}
this.tempDir = tempDir;
}
/**
* @return the location of the combined sources directory
*/
public File getCombinedSourceDir()
{
return new File(getTempDir(), "source");
}
/**
* @return the location of the combined classes directory
*/
public File getCombinedClassDir()
{
return new File(getTempDir(), "classes");
}
/**
* @return the location of the combined library directory
*/
public File getCombinedLibraryDir()
{
return new File(getTempDir(), "lib");
}
/**
* Returns a map view of this configuration to be used in e.g. performing variable substitution. Some additional
* parameters are added to the map for convenience.
*
* @return a view of this configuration as a map
*/
public Map asParameterMap()
{
Map params = new HashMap();
for (Map.Entry