Skip to content

Commit

Permalink
[JENKINS-43529] Don’t create an invalid build xml logs file when the …
Browse files Browse the repository at this point in the history
…Maven Event Spy is disabled (#39)
  • Loading branch information
Cyrille Le Clerc committed Apr 12, 2017
1 parent eaf719e commit ebf74cb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Expand Up @@ -70,7 +70,7 @@ public class JenkinsMavenEventSpy extends AbstractEventSpy {

public final static String DISABLE_MAVEN_EVENT_SPY_ENVIRONMENT_VARIABLE_NAME = "JENKINS_MAVEN_AGENT_DISABLED";

private final MavenEventReporter reporter;
private MavenEventReporter reporter;

/*
* visible for testing
Expand All @@ -86,10 +86,28 @@ public class JenkinsMavenEventSpy extends AbstractEventSpy {
private List<MavenEventHandler> handlers = new ArrayList();

public JenkinsMavenEventSpy() throws IOException {
this(new FileMavenEventReporter());
this.disabled = isEventSpyDisabled();
if (disabled) {
System.out.println("[jenkins-maven-event-spy] INFO Jenkins Maven Event Spy is disabled");
}
}

public JenkinsMavenEventSpy(MavenEventReporter reporter) throws IOException {
this();
this.reporter = reporter;
}

@Override
public void init(EventSpy.Context context) throws Exception {
if (disabled) {
this.reporter = new DevNullMavenEventReporter();
return;
}

if (reporter == null) {
this.reporter = new FileMavenEventReporter();
}
// Initialize handlers
handlers.add(new ProjectSucceededExecutionHandler(reporter));
handlers.add(new ProjectFailedExecutionHandler(reporter));
handlers.add(new ProjectStartedExecutionHandler(reporter));
Expand All @@ -104,20 +122,7 @@ public JenkinsMavenEventSpy(MavenEventReporter reporter) throws IOException {

handlers.add(new CatchAllExecutionHandler(reporter));

if (isEventSpyDisabled()) {
this.disabled = true;
this.reporter = new DevNullMavenEventReporter();
} else {
this.disabled = false;
this.reporter = reporter;
}
}

@Override
public void init(EventSpy.Context context) throws Exception {
if (disabled)
return;

// Print context
Xpp3Dom element = new Xpp3Dom("context");
for (Map.Entry<String, Object> entry : context.getData().entrySet()) {
Xpp3Dom entryElt = new Xpp3Dom(entry.getKey());
Expand Down Expand Up @@ -150,7 +155,7 @@ public void onEvent(Object event) throws Exception {

} catch (Throwable t) {
blackList.add(event.getClass());
System.err.println("Exception processing " + event);
System.err.println("[jenkins-maven-event-spy] WARNING Exception processing " + event);
reporter.print(getClass().getName() + ": Exception processing " + event);
t.printStackTrace();
}
Expand All @@ -166,7 +171,9 @@ public void close() {
reporter.close();
}


/**
* Visible for testing
*/
protected boolean isEventSpyDisabled(){
return "true".equalsIgnoreCase(System.getProperty(DISABLE_MAVEN_EVENT_SPY_PROPERTY_NAME)) ||
"true".equalsIgnoreCase(System.getenv(DISABLE_MAVEN_EVENT_SPY_ENVIRONMENT_VARIABLE_NAME));
Expand Down
Expand Up @@ -81,7 +81,7 @@ public FileMavenEventReporter() throws IOException {
xmlWriter.startElement("mavenExecution");
xmlWriter.addAttribute("_time", new Timestamp(System.currentTimeMillis()).toString());

System.out.println("[jenkins-maven-event-spy] generate " + outFile.getAbsolutePath() + " ...");
System.out.println("[jenkins-maven-event-spy] INFO generate " + outFile.getAbsolutePath() + " ...");
}

@Override
Expand All @@ -102,6 +102,6 @@ public synchronized void close() {
xmlWriter.endElement();

out.close();
System.out.println("[jenkins-maven-event-spy] generated " + outFile.getAbsolutePath());
System.out.println("[jenkins-maven-event-spy] INFO generated " + outFile.getAbsolutePath());
}
}
Expand Up @@ -53,6 +53,14 @@ protected boolean isEventSpyDisabled() {
return true;
}
};
Assert.assertThat(spy.getReporter(), CoreMatchers.nullValue());
spy.init(new EventSpy.Context() {
@Override
public Map<String, Object> getData() {
return new HashMap<String, Object>();
}
});

Assert.assertThat(spy.getReporter(), CoreMatchers.instanceOf(DevNullMavenEventReporter.class));
Assert.assertThat(spy.disabled, CoreMatchers.is(true));
}
Expand Down

0 comments on commit ebf74cb

Please sign in to comment.