Skip to content

Commit

Permalink
avoid passing null and better classloader
Browse files Browse the repository at this point in the history
  • Loading branch information
akostadinov committed Jun 22, 2018
1 parent 20f4a6a commit 2980228
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public LogstashScriptProcessor(SecureGroovyScript script, OutputStreamWriter con
binding = new Binding();
binding.setVariable("console", new BuildConsoleWrapper());

// not sure what the diff is compared to getClass().getClassLoader();
final Jenkins jenkins = Jenkins.getInstance();
classLoader = jenkins.getPluginManager().uberClassLoader;
// if users need access to types defined by any plugin we may decide to
// switch to Jenkins.getInstance().getPluginManager().uberClassLoader
classLoader = LogstashScriptProcessor.class.getClassLoader();
}

/**
Expand All @@ -102,7 +102,7 @@ public JSONObject process(JSONObject payload) throws Exception {
@Override
public JSONObject finish() throws Exception {
buildLogPrintln("Tearing down Script Log Processor..");
return process(null);
return process(new JSONObject());
}

/**
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/jenkins/plugins/logstash/LogstashWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jenkins.model.Jenkins;
import jenkins.plugins.logstash.persistence.BuildData;
import jenkins.plugins.logstash.persistence.LogstashIndexerDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
Expand Down Expand Up @@ -199,7 +200,7 @@ private void write(JSONObject payload) {

logErrorMessage(msg);
}
if (processedPayload != null) { writeRaw(processedPayload); }
if (payloadNeedsPersistence(processedPayload)) writeRaw(processedPayload);
} else {
writeRaw(payload);
}
Expand Down Expand Up @@ -257,6 +258,16 @@ private void logErrorMessage(String msg) {
}
}

/**
* Check whether payload is something we are expected to persist
*/
private Boolean payloadNeedsPersistence(JSONObject payload) {
if (payload != null) {
Object message = payload.opt("message");
return message != null && (message instanceof JSONArray) && !((JSONArray) message).isEmpty();
}
return false;
}

/**
* Signal payload processor that there will be no more lines
Expand All @@ -273,7 +284,7 @@ public void close() {

logErrorMessage(msg);
}
if (payload != null) writeRaw(payload);
if (payloadNeedsPersistence(payload)) writeRaw(payload);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jenkins.plugins.logstash;

import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.object.HasToString.hasToString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -134,6 +135,6 @@ public void writerClosedBeforeDelegate() throws Exception {
inOrder.verify(mockBuffer).close();

// Verify results
assertEquals("Results don't match", "", buffer.toString());
assertThat(buffer, hasToString(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ public void writeProcessedSuccess() throws Exception {
String goodMsg = "test message";
String ignoredMsg = "ignored input";
String scriptString =
"if (payload) {\n" +
"if (payload['message']) {\n" +
" if (payload['message'][0] =~ /" + ignoredMsg + "/) {\n" +
" payload = null\n" +
" payload.clear()\n" +
" } else {\n" +
" console.println('l');\n" +
" }\n" +
Expand Down

0 comments on commit 2980228

Please sign in to comment.