made load method private and add another static getter that users of Grok can use to get the builtin patterns.

This commit is contained in:
Martijn van Groningen 2018-02-20 08:09:24 +01:00
parent 3fad16e76c
commit 9c405e8595
3 changed files with 25 additions and 21 deletions

View File

@ -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<String, String> builtinPatterns;
static {
try {
builtinPatterns = loadBuiltinPatterns();
} catch (IOException e) {
throw new UncheckedIOException("unable to load built-in grok patterns", e);
}
}
private final Map<String, String> 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<String, String> getBuiltinPatterns() {
return builtinPatterns;
}
public static Map<String, String> loadBuiltinPatterns() throws IOException {
private static Map<String, String> 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<String, String> builtinPatterns = new HashMap<>();
for (String pattern : PATTERN_NAMES) {
try(InputStream is = Grok.class.getResourceAsStream("/patterns/" + pattern)) {

View File

@ -38,8 +38,8 @@ public class GrokTests extends ESTestCase {
private Map<String, String> basePatterns;
@Before
public void setup() throws IOException {
basePatterns = Grok.loadBuiltinPatterns();
public void setup() {
basePatterns = Grok.getBuiltinPatterns();
}
public void testMatchWithoutCaptures() {

View File

@ -50,14 +50,7 @@ import org.elasticsearch.rest.RestHandler;
public class IngestCommonPlugin extends Plugin implements ActionPlugin, IngestPlugin {
static final Map<String, String> 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<String, String> GROK_PATTERNS = Grok.getBuiltinPatterns();
public IngestCommonPlugin() {
}