Refactor common code to a util function

Fixes #7331
This commit is contained in:
Suyog Rao 2017-06-05 23:25:38 -07:00
parent d0b5f3a5e4
commit 6a1cfcf4b9
10 changed files with 33 additions and 137 deletions

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
/**
@ -18,16 +12,6 @@ public final class Append {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_append_to_logstash",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_append_to_logstash");
}
}

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
/**
@ -18,16 +12,6 @@ public final class Convert {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_convert_to_logstash",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_convert_to_logstash");
}
}

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
/**
@ -18,16 +12,6 @@ public final class Date {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_to_logstash_date",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_to_logstash_date");
}
}

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public final class GeoIp {
@ -15,16 +9,6 @@ public final class GeoIp {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_to_logstash_geoip",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_to_logstash_geoip");
}
}

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
/**
@ -18,16 +12,6 @@ public final class Grok {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_to_logstash_grok",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_to_logstash_grok");
}
}

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public final class Gsub {
@ -15,16 +9,6 @@ public final class Gsub {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_to_logstash_gsub",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_to_logstash_gsub");
}
}

View file

@ -3,6 +3,10 @@ package org.logstash.ingest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
@ -37,6 +41,26 @@ final class JsUtil {
return engine;
}
/**
* Converts the given files from ingest to LS conf using the javascript function
* @param args
* @param jsFunc
* @throws ScriptException
* @throws NoSuchMethodException
*/
public static void convert(final String[] args, final String jsFunc) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
jsFunc, new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}
private static void add(final ScriptEngine engine, final String file)
throws IOException, ScriptException {
try (final Reader reader =

View file

@ -1,12 +1,6 @@
package org.logstash.ingest;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Ingest JSON processor DSL to Logstash json Transpiler.
@ -17,16 +11,6 @@ public class Json {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_json_to_logstash",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_json_to_logstash");
}
}

View file

@ -1,11 +1,5 @@
package org.logstash.ingest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
/**
@ -18,16 +12,6 @@ public final class Pipeline {
}
public static void main(final String... args) throws ScriptException, NoSuchMethodException {
try {
final ScriptEngine engine = JsUtil.engine();
Files.write(Paths.get(args[1]), ((String) ((Invocable) engine).invokeFunction(
"ingest_pipeline_to_logstash",
new String(
Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8
)
)).getBytes(StandardCharsets.UTF_8));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
JsUtil.convert(args, "ingest_pipeline_to_logstash");
}
}

View file

@ -15,7 +15,7 @@ public final class AppendTest extends IngestTest {
}
@Test
public void convertsConvertProcessorCorrectly() throws Exception {
public void convertsAppendProcessorCorrectly() throws Exception {
final String append = getResultPath(temp);
Append.main(resourcePath(String.format("ingest%s.json", testCase)), append);
assertThat(