From 9c405e8595aa15045f12031127ef9230f33e5a08 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 20 Feb 2018 08:09:24 +0100 Subject: [PATCH] made load method private and add another static getter that users of Grok can use to get the builtin patterns. --- .../java/org/elasticsearch/grok/Grok.java | 33 ++++++++++++------- .../org/elasticsearch/grok/GrokTests.java | 4 +-- .../ingest/common/IngestCommonPlugin.java | 9 +---- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java b/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java index 770f249b5a8..36a06777c4f 100644 --- a/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java +++ b/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java @@ -28,10 +28,7 @@ import org.joni.Region; import org.joni.Syntax; import org.joni.exception.ValueException; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Iterator; @@ -59,6 +56,17 @@ public final class Grok { ")?" + "\\}"; private static final Regex GROK_PATTERN_REGEX = new Regex(GROK_PATTERN.getBytes(StandardCharsets.UTF_8), 0, GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, Option.NONE, UTF8Encoding.INSTANCE, Syntax.DEFAULT); + + private static final Map builtinPatterns; + + static { + try { + builtinPatterns = loadBuiltinPatterns(); + } catch (IOException e) { + throw new UncheckedIOException("unable to load built-in grok patterns", e); + } + } + private final Map patternBank; private final boolean namedCaptures; private final Regex compiledExpression; @@ -182,14 +190,17 @@ public final class Grok { return null; } - // Code for loading built-in grok patterns packaged with the jar file: - private static final String[] PATTERN_NAMES = new String[] { - "aws", "bacula", "bro", "exim", "firewalls", "grok-patterns", "haproxy", - "java", "junos", "linux-syslog", "mcollective-patterns", "mongodb", "nagios", - "postgresql", "rails", "redis", "ruby" - }; + public static Map getBuiltinPatterns() { + return builtinPatterns; + } - public static Map loadBuiltinPatterns() throws IOException { + private static Map loadBuiltinPatterns() throws IOException { + // Code for loading built-in grok patterns packaged with the jar file: + String[] PATTERN_NAMES = new String[] { + "aws", "bacula", "bro", "exim", "firewalls", "grok-patterns", "haproxy", + "java", "junos", "linux-syslog", "mcollective-patterns", "mongodb", "nagios", + "postgresql", "rails", "redis", "ruby" + }; Map builtinPatterns = new HashMap<>(); for (String pattern : PATTERN_NAMES) { try(InputStream is = Grok.class.getResourceAsStream("/patterns/" + pattern)) { diff --git a/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java b/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java index 6504810713b..931842d9f24 100644 --- a/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java +++ b/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java @@ -38,8 +38,8 @@ public class GrokTests extends ESTestCase { private Map basePatterns; @Before - public void setup() throws IOException { - basePatterns = Grok.loadBuiltinPatterns(); + public void setup() { + basePatterns = Grok.getBuiltinPatterns(); } public void testMatchWithoutCaptures() { diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java index 8c7a1e76c3b..79c6dadb4fc 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java @@ -50,14 +50,7 @@ import org.elasticsearch.rest.RestHandler; public class IngestCommonPlugin extends Plugin implements ActionPlugin, IngestPlugin { - static final Map GROK_PATTERNS; - static { - try { - GROK_PATTERNS = Grok.loadBuiltinPatterns(); - } catch (IOException e) { - throw new UncheckedIOException("unable to load built-in grok patterns", e); - } - } + static final Map GROK_PATTERNS = Grok.getBuiltinPatterns(); public IngestCommonPlugin() { }