Skip to content

Commit 8722039

Browse files
committed
Manage the event lifecycle a little more tightly
This prevents us from needing to return a copy or from needing to put the maps in an unmodifiable wrapper. The object is single use: you build it, get the maps, close it, and then you're done.
1 parent 7578da0 commit 8722039

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CefParser.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.time.temporal.TemporalAccessor;
2727
import java.time.temporal.WeekFields;
2828
import java.util.ArrayList;
29-
import java.util.Collections;
3029
import java.util.HashMap;
3130
import java.util.HashSet;
3231
import java.util.List;
@@ -533,9 +532,9 @@ private static String desanitizeExtensionVal(String value) {
533532
return desanitized;
534533
}
535534

536-
public static class CEFEvent {
537-
private final Map<String, Object> rootMappings = new HashMap<>();
538-
private final Map<String, Object> cefMappings = new HashMap<>();
535+
public static class CEFEvent implements AutoCloseable {
536+
private Map<String, Object> rootMappings = new HashMap<>();
537+
private Map<String, Object> cefMappings = new HashMap<>();
539538

540539
public void addRootMapping(String key, Object value) {
541540
this.rootMappings.put(key, value);
@@ -546,11 +545,21 @@ public void addCefMapping(String key, Object value) {
546545
}
547546

548547
public Map<String, Object> getRootMappings() {
549-
return Collections.unmodifiableMap(rootMappings);
548+
return Objects.requireNonNull(rootMappings);
550549
}
551550

552551
public Map<String, Object> getCefMappings() {
553-
return Collections.unmodifiableMap(cefMappings);
552+
return Objects.requireNonNull(cefMappings);
553+
}
554+
555+
/**
556+
* Nulls out the maps of the event so that future calls to methods of this class will fail with a
557+
* {@link NullPointerException}.
558+
*/
559+
@Override
560+
public void close() {
561+
this.rootMappings = null;
562+
this.cefMappings = null;
554563
}
555564
}
556565

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CefProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public IngestDocument execute(IngestDocument document) {
5959
throw new IllegalArgumentException("field [" + field + "] is null, cannot process it.");
6060
}
6161
ZoneId timezone = getTimezone(document);
62-
CEFEvent event = new CefParser(timezone, removeEmptyValue).process(line);
63-
// Update ingestDocument with the CEF mappings
64-
document.setFieldValue(targetField, event.getCefMappings());
65-
event.getRootMappings().forEach(document::setFieldValue);
62+
try (CEFEvent event = new CefParser(timezone, removeEmptyValue).process(line)) {
63+
document.setFieldValue(targetField, event.getCefMappings());
64+
event.getRootMappings().forEach(document::setFieldValue);
65+
}
6666
return document;
6767
}
6868

0 commit comments

Comments
 (0)