Hi,
Serialization problem is now solved. Thanks for the
help.
However ( is it not always something...) now I get the
following in my DataCollector (
still a master-slave setup):
This
is the code I am using:
/**
*
*/
package
jenkins.plugins.qrank;
import
hudson.FilePath;
import
hudson.FilePath.FileCallable;
import
hudson.model.AbstractBuild;
import
hudson.remoting.VirtualChannel;
import
jenkins.plugins.qrank.parse.LogParser;
import
jenkins.plugins.qrank.parse.PullParserFactory;
import
jenkins.plugins.qrank.parse.TestResults;
import
org.apache.tools.ant.types.FileSet;
import
java.io.File;
import java.io.IOException;
import
java.io.InputStream;
import java.io.PrintStream;
import
java.util.HashMap;
import java.util.Map;
import
java.util.regex.Pattern;
/**
* This class collects all data and returns it to the
caller.
*
* @author mikael petterson
*
*/
public class DataCollector implements
FileCallable<TestResults> {
/**
*
*/
private static final long serialVersionUID =
5174278843704513371L;
private static final String[] NO_FILE = new String[]
{};
private static final String[] TARGET = new String[]
{
PluginImpl.BUILD_ON_TARGET_NO_ERR,
PluginImpl.BUILD_ON_TARGET_NO_WARN }; // Order is important!
private static final String[] TEST = new String[]
{
PluginImpl.BUILD_FOR_TEST_NO_ERR, PluginImpl.BUILD_FOR_TEST_NO_WARN }; //
Order
//
is
// important!
private final Map<String, String>
pattern;
private final transient PrintStream
logger;
private transient File workspace;
private final transient AbstractBuild<?, ?>
build;
public DataCollector(final Map<String, String>
pattern,
final PrintStream logger, final AbstractBuild<?, ?> build)
{
this.pattern =
pattern;
this.logger =
logger;
this.build =
build;
}
/**
* Collects data and
returns the result to the caller.
*/
public TestResults invoke(final File workspace, final
VirtualChannel
channel)
throws IOException, InterruptedException
{
this.workspace =
workspace;
final TestResults report = new
TestResults();
for (Map.Entry<String,
String> entry : pattern.entrySet())
{
final
String pattern = entry.getValue();
if
(pattern == null)
{
logger.println("Pattern for type " +
entry.getKey()
+ " is
null");
continue;
}
doExtract(entry.getKey(), pattern,
report);
}
report.calcQrank();
return
report;
}
/**
* Finds the files
using patterns ( exception cobertura) and calls
diff-
* erent parsers.
*
* @param type
* @param
pattern
* @param report
* @throws IOException
*/
private void doExtract(final String type, final String
pattern,
final TestResults report) throws IOException
{
String[]
fileNames;
if
(type.equals(PluginImpl.COBERTURA))
{
fileNames =
getCoberturaXml(pattern);
} else
{
// Files
in
workspace.
if (pattern.length() != 0 && workspace != null)
{
115:
fileNames = findFiles(workspace,
pattern);
} else
{
fileNames = new String[]
{};
}
}
if (fileNames.length == 0)
{
logger.println("No report/log files of type " +
type
+ " with pattern " + pattern + " were
found!");
return;
}
for (int i = 0; i <
fileNames.length; i++)
{
logger.println("File to be parsed: " +
fileNames[i]);
}
for (String fileName :
fileNames)
{
final
File file;
Map<String, Pattern> patternsMap;
if
(type.equals(PluginImpl.TARGET_PARSER))
{
patternsMap =
preparePatterns(TARGET);
file = new File(workspace,
fileName);
parseAntLog(file, patternsMap,
report);
}
else if (type.equals(PluginImpl.TEST_PARSER))
{
patternsMap =
preparePatterns(TEST);
file = new File(workspace,
fileName);
parseAntLog(file, patternsMap,
report);
}
else if (type.equals(PluginImpl.COBERTURA))
{
file = new
File(fileName);
PullParserFactory.instance().createParser(type)
.parse(file, type,
report);
}
else
{
file = new File(workspace,
fileName);
PullParserFactory.instance().createParser(type)
.parse(file, type,
report);
}
}
}
// TODO: add handking when cobertura file is not
found.
private String[] getCoberturaXml(String pattern)
{
logger.println("Retrieving
Cobertura coverage report...");
final FilePath[] moduleRoots =
build.getModuleRoots();
final
boolean multipleModuleRoots = (moduleRoots !=
null)
&& (moduleRoots.length >
1);
final FilePath moduleRoot =
multipleModuleRoots ?
build.getWorkspace()
: build.getModuleRoot();
final
File buildCoberturaDir =
build.getRootDir();
FilePath
buildTarget = new FilePath(buildCoberturaDir);
FilePath[] reports = new
FilePath[0];
try
{
reports
= moduleRoot.list(pattern);
}
catch (IOException e)
{
logger.println("Unable to find file " +
pattern);
} catch
(InterruptedException e)
{
// TODO
Auto-generated catch
block
e.printStackTrace();
}
String[] filesFound = new
String[reports.length];
for (int i = 0; i <
reports.length; i++)
{
filesFound[i] =
reports[i].getRemote();
}
return
filesFound;
}
/**
* Sets the patterns to
the ant log parser.
*
*
@param types
* @return patternsMap a map with type
and accompanying pattern.
*/
private Map<String, Pattern> preparePatterns(final String[] types)
{
final Map<String, Pattern>
patternsMap = new HashMap<String, Pattern>();
for (int i = 0; i <
types.length; i++)
{
patternsMap.put(types[i],
Pattern.compile(PATTERNS[i]));
}
return
patternsMap;
}
/**
* Calls the LogParser
with regular expression used for parsing ant build
*
file output.
*
* @param
file
* log
file
* @param
patternsMap
* map
containing type and regexp.
* @param
report
* test
result.
* @throws
IOException
*/
private void
parseAntLog(final File
file,
final Map<String, Pattern> patternsMap, final TestResults
report)
throws IOException {
final
InputStream is = new
java.io.FileInputStream(file);
final LogParser logParser = new LogParser();
logParser.parse(is, report,
patternsMap);
}
/**
* Returns an array
with the filenames of the files that match an Ant
*
pattern using the workspace as the base directory.
*
* @param workspaceRoot
* root
directory of the workspace
*
* @return the filenames
found.
*/
private String[]
findFiles(final File workspaceRoot, final String pattern)
{
logger.println("find files in "
+ workspaceRoot + " pattern " + pattern);
String[]
files;
if (isBlank(pattern))
{
files =
new String[] {};
} else
{
final
FileSet fileSet = new
FileSet();
final org.apache.tools.ant.Project project = new
org.apache.tools.ant.Project();
fileSet.setProject(project);
fileSet.setDir(workspaceRoot);
fileSet.setIncludes(pattern);
files
=
fileSet.getDirectoryScanner(project).getIncludedFiles();
}
return
files;
}
/**
* Check if the string
is null or only spaces.
*
* @param str
* the string
to test.
* @return true if string is null or all
spaces.
*/
public static
boolean isBlank(final String str)
{
return (str == null) ? true :
"".equals(str.trim());
}
}