Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-4912] Created new action base class for maven aggregation.
  • Loading branch information
uhafner committed May 11, 2011
1 parent 78695e2 commit 6d5dbdb
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 43 deletions.
Expand Up @@ -7,7 +7,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerProxy;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
Expand All @@ -21,7 +20,6 @@
import hudson.model.AbstractBuild;

import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.analysis.util.StringPluginLogger;
import hudson.plugins.analysis.util.ToolTipProvider;
import hudson.plugins.analysis.util.model.AbstractAnnotation;

Expand All @@ -46,12 +44,6 @@ public abstract class AbstractResultAction<T extends BuildResult> implements Sta
/** The actual result of this action. */
private T result;

private transient StringPluginLogger logger = createLogger();

private StringPluginLogger createLogger() {
return new StringPluginLogger("[" + StringUtils.upperCase(getDisplayName()) + "] "); //NOCHECKSTYLE
}

/**
* Creates a new instance of <code>AbstractResultAction</code>.
*
Expand Down Expand Up @@ -264,38 +256,6 @@ logger, getHealthDescriptor().getThresholds(),
}
}

/**
* Returns the logger.
*
* @return the logger
*/
protected PluginLogger getLogger() {
if (logger == null) {
logger = createLogger();
}
return logger;
}

/**
* Returns all logging statements of this action that couldn't be printed so far.
*
* @return the logging statements
*/
public String getLog() {
String message = getLogger().toString();
logger = createLogger();
return message;
}

/**
* Logs the specified message.
*
* @param message the message
*/
protected void log(final String message) {
getLogger().log(message);
}

/** Backward compatibility. @deprecated */
@Deprecated
@java.lang.SuppressWarnings("unused")
Expand Down
Expand Up @@ -36,7 +36,7 @@
import hudson.tasks.BuildStep;

/**
* A base class for maven reporters with the following two characteristics:
* A base class for Maven reporters with the following two characteristics:
* <ul>
* <li>It provides a unstable threshold, that could be enabled and set in the
* configuration screen. If the number of annotations in a build exceeds this
Expand Down Expand Up @@ -276,7 +276,7 @@ public String call(final MavenBuild mavenBuild) throws IOException, InterruptedE
@Override
public boolean end(final MavenBuild build, final Launcher launcher, final BuildListener listener) {
MavenModuleSetBuild moduleSetBuild = build.getParentBuild();
AbstractResultAction<T> action = moduleSetBuild.getAction(getResultActionClass());
MavenResultAction<T> action = moduleSetBuild.getAction(getResultActionClass());
if (action != null) {
listener.getLogger().append(action.getLog());
}
Expand Down Expand Up @@ -460,7 +460,7 @@ public Boolean call(final MavenBuild mavenBuild) throws IOException, Interrupted
*
* @return the type of the result action
*/
protected abstract Class<? extends AbstractResultAction<T>> getResultActionClass();
protected abstract Class<? extends MavenResultAction<T>> getResultActionClass();

/**
* Returns the path to the target folder.
Expand Down
208 changes: 208 additions & 0 deletions src/main/java/hudson/plugins/analysis/core/MavenResultAction.java
@@ -0,0 +1,208 @@
package hudson.plugins.analysis.core;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Sets;

import hudson.maven.AggregatableAction;
import hudson.maven.MavenAggregatedReport;
import hudson.maven.MavenBuild;
import hudson.maven.MavenModule;

import hudson.model.HealthReport;
import hudson.model.AbstractBuild;

import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.analysis.util.StringPluginLogger;
import hudson.plugins.analysis.util.ToolTipProvider;

/**
* Base class for Maven aggregated build reports.
*
* @author Ulli Hafner
* @since 1.20
* @param <T> type of the build result
*/
public abstract class MavenResultAction<T extends BuildResult> implements AggregatableAction, MavenAggregatedReport {
/** The default encoding to be used when reading and parsing files. */
private final String defaultEncoding;
/** Reuse all the functionality of the action for freestyle jobs. */
private final AbstractResultAction<T> delegate;

private transient StringPluginLogger logger = createLogger();
private transient Set<MavenModule> modules = Sets.newHashSet();

private StringPluginLogger createLogger() {
return new StringPluginLogger("[" + StringUtils.upperCase(getDisplayName()) + "] "); //NOCHECKSTYLE
}

/**
* Creates a new instance of {@link MavenResultAction}.
*
* @param delegate
* result action for freestyle jobs that will do the main of the
* work
* @param defaultEncoding
* the default encoding to be used when reading and parsing files
*/
public MavenResultAction(final AbstractResultAction<T> delegate, final String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
this.delegate = delegate;
}

/** {@inheritDoc} */
public abstract Class<? extends MavenResultAction<T>> getIndividualActionType();

/**
* Creates a new build result that contains the aggregated annotations.
*
* @param existingResult
* the existing result
* @param aggregatedAnnotations
* the aggregated annotations
* @return the created result
*/
protected abstract T createResult(final T existingResult, ParserResult aggregatedAnnotations);

/**
* Called whenever a new module build is completed, to update the aggregated
* report. When multiple builds complete simultaneously, Jenkins serializes
* the execution of this method, so this method needs not be
* concurrency-safe.
*
* @param moduleBuilds
* Same as <tt>MavenModuleSet.getModuleBuilds()</tt> but provided
* for convenience and efficiency.
* @param newBuild
* Newly completed build.
*/
public void update(final Map<MavenModule, List<MavenBuild>> moduleBuilds, final MavenBuild newBuild) {
MavenResultAction<T> additionalAction = newBuild.getAction(getIndividualActionType());
MavenModule project = newBuild.getProject();
if (additionalAction != null && !getModules().contains(project)) {
getModules().add(project);
getLogger().log("Aggregating results of " + project.getDisplayName());

T existingResult = delegate.getResult();
T additionalResult = additionalAction.getResult();

if (existingResult == null) {
setResult(additionalResult);
getOwner().setResult(additionalResult.getPluginResult());
}
else {
setResult(aggregate(existingResult, additionalResult));
}
}
}

private T aggregate(final T existingResult, final T additionalResult) {
ParserResult aggregatedAnnotations = new ParserResult();
aggregatedAnnotations.addAnnotations(existingResult.getAnnotations());
aggregatedAnnotations.addAnnotations(additionalResult.getAnnotations());

T createdResult = createResult(existingResult, aggregatedAnnotations);
createdResult.evaluateStatus(existingResult.getThresholds(), existingResult.canUseDeltaValues(), getLogger());
return createdResult;
}

private PluginLogger getLogger() {
if (logger == null) {
logger = createLogger();
}
return logger;
}

/**
* Returns the modules.
*
* @return the modules
*/
private Set<MavenModule> getModules() {
if (modules == null) {
modules = Sets.newHashSet();
}
return modules;
}

/**
* Returns all logging statements of this action that couldn't be printed so far.
*
* @return the logging statements
*/
public String getLog() {
String message = getLogger().toString();
logger = createLogger();
return message;
}

/**
* Returns the default encoding.
*
* @return the default encoding
*/
public String getDefaultEncoding() {
return defaultEncoding;
}

/** {@inheritDoc} */
public String getIconFileName() {
return delegate.getIconFileName();
}

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

/** {@inheritDoc} */
public String getUrlName() {
return delegate.getUrlName();
}

// CHECKSTYLE:OFF
private void setResult(final T additionalResult) {
delegate.setResult(additionalResult);
}

public T getResult() {
return delegate.getResult();
}

public AbstractBuild<?, ?> getOwner() {
return delegate.getOwner();
}

public final HealthReport getBuildHealth() {
return delegate.getBuildHealth();
}

public ToolTipProvider getToolTipProvider() {
return delegate.getToolTipProvider();
}

public final AbstractBuild<?, ?> getBuild() {
return delegate.getBuild();
}

public final Object getTarget() {
return delegate.getTarget();
}

public String getTooltip(final int numberOfItems) {
return delegate.getTooltip(numberOfItems);
}

public boolean isSuccessful() {
return delegate.isSuccessful();
}

public HealthDescriptor getHealthDescriptor() {
return delegate.getHealthDescriptor();
}
// CHECKSTYLE:ON
}

0 comments on commit 6d5dbdb

Please sign in to comment.