Skip to content

Commit

Permalink
[JENKINS-43449] Ensure that the maven-event-spy is thread safe (#34)
Browse files Browse the repository at this point in the history
* [JENKINS-43449] Ensure that the maven-event-spy is thread safe
  • Loading branch information
Cyrille Le Clerc committed Apr 10, 2017
1 parent d2c7358 commit 3bb3b5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
13 changes: 13 additions & 0 deletions maven-spy/pom.xml
Expand Up @@ -94,6 +94,19 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>sisu-maven-plugin</artifactId>
Expand Down
Expand Up @@ -41,13 +41,18 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
@ThreadSafe
public class FileMavenEventReporter implements MavenEventReporter {
File outFile;
@GuardedBy("this")
PrintWriter out;

@GuardedBy("this")
XMLWriter xmlWriter;

public FileMavenEventReporter() throws IOException {
Expand Down Expand Up @@ -77,20 +82,20 @@ public FileMavenEventReporter() throws IOException {
}

@Override
public void print(Object message) {
public synchronized void print(Object message) {
XmlWriterUtil.writeComment(xmlWriter, new Timestamp(System.currentTimeMillis()) + " - " + message);
XmlWriterUtil.writeLineBreak(xmlWriter);
}

@Override
public void print(Xpp3Dom element) {
public synchronized void print(Xpp3Dom element) {
element.setAttribute("_time", new Timestamp(System.currentTimeMillis()).toString());
Xpp3DomWriter.write(xmlWriter, element);
XmlWriterUtil.writeLineBreak(xmlWriter);
}

@Override
public void close() {
public synchronized void close() {
xmlWriter.endElement();

out.close();
Expand Down
Expand Up @@ -24,15 +24,27 @@

package org.jenkinsci.plugins.pipeline.maven.eventspy.reporter;

import org.apache.maven.eventspy.EventSpy;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import javax.annotation.concurrent.ThreadSafe;

/**
* WARNING: implementations of {@link MavenEventReporter} MUST be thread safe.
*
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
@ThreadSafe
public interface MavenEventReporter {
void print(Object message);

void print(Xpp3Dom element);

/**
* Close the reporter at the end of the Maven execution.
* No call to {@link #print(Object)} or {@link #print(Xpp3Dom)} will be made after the invocation of this method.
*
* @see EventSpy#close()
*/
void close();
}
Expand Up @@ -37,12 +37,18 @@
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;

import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
@ThreadSafe
public class OutputStreamEventReporter implements MavenEventReporter {

@GuardedBy("this")
final PrintWriter out;
@GuardedBy("this")
final XMLWriter xmlWriter;

public OutputStreamEventReporter(OutputStream out) {
Expand All @@ -61,7 +67,7 @@ public OutputStreamEventReporter(Writer out) {
}

@Override
public void print(Object message) {
public synchronized void print(Object message) {
String comment = new Timestamp(System.currentTimeMillis()) + " - " + message;
XmlWriterUtil.writeComment(xmlWriter, comment);
XmlWriterUtil.writeLineBreak(xmlWriter);
Expand All @@ -70,15 +76,15 @@ public void print(Object message) {
}

@Override
public void print(Xpp3Dom element) {
public synchronized void print(Xpp3Dom element) {
Xpp3DomWriter.write(xmlWriter, element);
XmlWriterUtil.writeLineBreak(xmlWriter);

out.flush();
}

@Override
public void close() {
public synchronized void close() {
xmlWriter.endElement();
out.flush();
}
Expand Down

0 comments on commit 3bb3b5b

Please sign in to comment.