All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hudson.plugins.analysis.collector.AnalysisResult Maven / Gradle / Ivy

Go to download

This plug-in is an add-on for the plug-ins Checkstyle, Dry, FindBugs, PMD, Tasks, and Warnings: the plug-in collects the different analysis results and shows the results in a combined trend graph. Additionally, the plug-in provides health reporting and build stability based on these combined results.

The newest version!
package hudson.plugins.analysis.collector; // NOPMD

import hudson.model.AbstractBuild;
import hudson.plugins.analysis.core.BuildHistory;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.ResultAction;
import hudson.plugins.analysis.util.model.FileAnnotation;

import java.lang.ref.WeakReference;
import java.util.Map;

import com.google.common.collect.Maps;

/**
 * Stores the results of the analysis plug-ins. One instance of this class is
 * persisted for each build via an XML file.
 *
 * @author Ulli Hafner
 */
public class AnalysisResult extends BuildResult {
    /** Unique identifier of this class. */
    private static final long serialVersionUID = 847650789493429154L;

    /** Number of annotations by origin mapping. */
    private transient WeakReference> annotationsByOrigin;

    private transient Object mappingLock = new Object();

    /**
     * Creates a new instance of {@link AnalysisResult}.
     *
     * @param build
     *            the current build as owner of this action
     * @param defaultEncoding
     *            the default encoding to be used when reading and parsing files
     * @param result
     *            the parsed result with all annotations
     * @param history
     *            the plug-in history
     */
    public AnalysisResult(final AbstractBuild build, final String defaultEncoding,
            final ParserResult result, final BuildHistory history) {
        super(build, defaultEncoding, result, history);

        annotationsByOrigin = newReference(countAnnotations());
    }

    /**
     * Creates a new instance of {@link AnalysisResult}.
     *
     * @param build
     *            the current build as owner of this action
     * @param defaultEncoding
     *            the default encoding to be used when reading and parsing files
     * @param result
     *            the parsed result with all annotations
     */
    public AnalysisResult(final AbstractBuild build, final String defaultEncoding,
            final ParserResult result) {
        super(build, defaultEncoding, result);

        annotationsByOrigin = newReference(countAnnotations());
    }

    /** {@inheritDoc} */
    @Override
    protected Object readResolve() {
        super.readResolve();

        mappingLock = new Object();

        return this;
    }

    /**
     * Count the annotations by origin.
     *
     * @return the mapping
     */
    private Map countAnnotations() {
        Map mapping = Maps.newHashMap();
        for (FileAnnotation annotation : getAnnotations()) {
            if (!mapping.containsKey(annotation.getOrigin())) {
                mapping.put(annotation.getOrigin(), 0);
            }
            mapping.put(annotation.getOrigin(),
                    mapping.get(annotation.getOrigin()) + 1);
        }
        return mapping;
    }

    private WeakReference> newReference(final Map mapping) {
        return new WeakReference>(mapping);
    }

    /**
     * Returns a summary message for the summary.jelly file.
     *
     * @return the summary message
     */
    public String getSummary() {
        return AnalysisResultSummary.createSummary(this);
    }

    /** {@inheritDoc} */
    @Override
    protected String createDeltaMessage() {
        return AnalysisResultSummary.createDeltaMessage(this);
    }

    /** {@inheritDoc} */
    @Override
    protected String getSerializationFileName() {
        return "analysis.xml";
    }

    /** {@inheritDoc} */
    public String getDisplayName() {
        return Messages.Analysis_ProjectAction_Name();
    }

    /** {@inheritDoc} */
    @Override
    protected Class> getResultActionType() {
        return AnalysisResultAction.class;
    }

    /**
     * Returns the number of annotations from the specified origin. If there are no annotations
     *
     * @param origin
     *            the origin
     * @return the number of annotations from the specified origin
     */
    public int getNumberOfAnnotationsByOrigin(final String origin) {
        Map mapping = getMapping();
        if (mapping.containsKey(origin)) {
            return mapping.get(origin);
        }
        return 0;
    }

    private Map getMapping() {
        synchronized (mappingLock) {
            if (annotationsByOrigin == null || annotationsByOrigin.get() == null) {
                Map mapping = countAnnotations();
                annotationsByOrigin = newReference(mapping);
                return mapping;
            }
            return annotationsByOrigin.get();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy