-
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.
#8442 Wrap Ruby PipelineIR Compiler in Java Call
- Loading branch information
1 parent
c0a355f
commit ba14481
Showing
7 changed files
with
101 additions
and
25 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
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
61 changes: 61 additions & 0 deletions
61
logstash-core/src/main/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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.logstash.config.ir; | ||
|
||
import java.nio.file.Paths; | ||
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 ConfigCompiler() { | ||
// Utility Class | ||
} | ||
|
||
/** | ||
* @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 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( | ||
Paths.get(System.getProperty("logstash.core.root.dir", ""), "lib").toFile() | ||
.getAbsolutePath() | ||
); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.logstash.config.ir; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class ConfigCompilerTest { | ||
|
||
@Test | ||
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)); | ||
} | ||
} |