-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap PipelineIR Compilation in Java Call
- Loading branch information
1 parent
f7ca6d9
commit 761b551
Showing
6 changed files
with
71 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 46 additions & 12 deletions
58
logstash-core/src/test/java/org/logstash/config/ir/ConfigCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,57 @@ | ||
package org.logstash.config.ir; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.javasupport.JavaUtil; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.runtime.load.LoadService; | ||
import org.logstash.RubyUtil; | ||
import org.logstash.common.IncompleteSourceWithMetadataException; | ||
import org.logstash.common.SourceWithMetadata; | ||
|
||
/** | ||
* Java Implementation of the config compiler that is implemented by wrapping the Ruby | ||
* {@code LogStash::Compiler}. | ||
*/ | ||
public final class ConfigCompiler { | ||
|
||
private final Ruby ruby; | ||
|
||
public ConfigCompiler(final Ruby ruby) { | ||
this.ruby = ruby; | ||
private ConfigCompiler() { | ||
// Utility Class | ||
} | ||
|
||
public String configToRuby(final String config) { | ||
final IRubyObject grammar = ruby.executeScript( | ||
"require 'logstash/config/grammar'\ngrammar = LogStashConfigParser.new", "" | ||
/** | ||
* @param config Logstash Config String | ||
* @param supportEscapes The value of the setting {@code config.support_escapes} | ||
* @return Compiled {@link PipelineIR} | ||
* @throws IncompleteSourceWithMetadataException On Broken Configuration | ||
*/ | ||
public static PipelineIR configToPipelineIR(final String config, final boolean supportEscapes) | ||
throws IncompleteSourceWithMetadataException { | ||
ensureLoadpath(); | ||
final IRubyObject compiler = RubyUtil.RUBY.executeScript( | ||
"require 'logstash/compiler'\nLogStash::Compiler", | ||
"" | ||
); | ||
final IRubyObject parsed = | ||
grammar.callMethod(ruby.getCurrentContext(), "parse", ruby.newString(config)); | ||
final IRubyObject code = parsed.callMethod(ruby.getCurrentContext(), "compile"); | ||
return code.toString(); | ||
final IRubyObject code = | ||
compiler.callMethod(RubyUtil.RUBY.getCurrentContext(), "compile_sources", | ||
new IRubyObject[]{ | ||
RubyUtil.RUBY.newArray( | ||
JavaUtil.convertJavaToRuby( | ||
RubyUtil.RUBY, | ||
new SourceWithMetadata("str", "pipeline", 0, 0, config) | ||
) | ||
), | ||
RubyUtil.RUBY.newBoolean(supportEscapes) | ||
} | ||
); | ||
return (PipelineIR) code.toJava(PipelineIR.class); | ||
} | ||
|
||
/** | ||
* Loads the logstash-core/lib path if the load service can't find {@code logstash/compiler}. | ||
*/ | ||
private static void ensureLoadpath() { | ||
final LoadService loader = RubyUtil.RUBY.getLoadService(); | ||
if (loader.findFileForLoad("logstash/compiler").library == null) { | ||
loader.addPaths("lib"); | ||
} | ||
} | ||
} |
12 changes: 8 additions & 4 deletions
12
logstash-core/src/test/java/org/logstash/config/ir/ConfigCompilerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package org.logstash.config.ir; | ||
|
||
import org.junit.Test; | ||
import org.logstash.RubyUtil; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class ConfigCompilerTest { | ||
|
||
@Test | ||
public void testConfigToRuby() { | ||
new ConfigCompiler(RubyUtil.RUBY) | ||
.configToRuby("input {stdin{}} output{stdout{}}"); | ||
public void testConfigToPipelineIR() throws Exception { | ||
final PipelineIR pipelineIR = | ||
ConfigCompiler.configToPipelineIR("input {stdin{}} output{stdout{}}", false); | ||
assertThat(pipelineIR.getOutputPluginVertices().size(), is(1)); | ||
assertThat(pipelineIR.getFilterPluginVertices().size(), is(0)); | ||
} | ||
} |