#8154 break cyclic dependency () between Java and Ruby

Fixes #8594
This commit is contained in:
Armin 2017-11-06 15:01:12 +01:00 committed by Armin Braun
parent 87dc146e4b
commit 8cbc398282
3 changed files with 22 additions and 8 deletions

View file

@ -2,8 +2,11 @@
require "logstash/namespace" require "logstash/namespace"
# Force loading the RubyUtil to ensure its loaded before the Timestamp class is set up in Ruby since
# Timestamp depends on Ruby classes that are dynamically set up by Java code.
java_import org.logstash.RubyUtil
module LogStash module LogStash
class TimestampParserError < StandardError; end
class Timestamp class Timestamp
include Comparable include Comparable

View file

@ -42,6 +42,8 @@ public final class RubyUtil {
public static final RubyClass LOGSTASH_ERROR; public static final RubyClass LOGSTASH_ERROR;
public static final RubyClass TIMESTAMP_PARSER_ERROR;
static { static {
RUBY = Ruby.getGlobalRuntime(); RUBY = Ruby.getGlobalRuntime();
LOGSTASH_MODULE = RUBY.getOrCreateModule("LogStash"); LOGSTASH_MODULE = RUBY.getOrCreateModule("LogStash");
@ -56,12 +58,16 @@ public final class RubyUtil {
"Event", JrubyEventExtLibrary.RubyEvent::new, JrubyEventExtLibrary.RubyEvent.class "Event", JrubyEventExtLibrary.RubyEvent::new, JrubyEventExtLibrary.RubyEvent.class
); );
final RubyModule json = LOGSTASH_MODULE.defineOrGetModuleUnder("Json"); final RubyModule json = LOGSTASH_MODULE.defineOrGetModuleUnder("Json");
final RubyClass stdErr = RUBY.getStandardError();
LOGSTASH_ERROR = LOGSTASH_MODULE.defineClassUnder( LOGSTASH_ERROR = LOGSTASH_MODULE.defineClassUnder(
"Error", RUBY.getStandardError(), RubyUtil.LogstashRubyError::new "Error", stdErr, RubyUtil.LogstashRubyError::new
); );
PARSER_ERROR = json.defineClassUnder( PARSER_ERROR = json.defineClassUnder(
"ParserError", LOGSTASH_ERROR, RubyUtil.LogstashRubyParserError::new "ParserError", LOGSTASH_ERROR, RubyUtil.LogstashRubyParserError::new
); );
TIMESTAMP_PARSER_ERROR = LOGSTASH_MODULE.defineClassUnder(
"TimestampParserError", stdErr, RubyUtil.LogstashTimestampParserError::new
);
GENERATOR_ERROR = json.defineClassUnder("GeneratorError", LOGSTASH_ERROR, GENERATOR_ERROR = json.defineClassUnder("GeneratorError", LOGSTASH_ERROR,
RubyUtil.LogstashRubyGeneratorError::new RubyUtil.LogstashRubyGeneratorError::new
); );
@ -150,4 +156,12 @@ public final class RubyUtil {
super(runtime, metaClass); super(runtime, metaClass);
} }
} }
@JRubyClass(name = "TimestampParserError")
public static final class LogstashTimestampParserError extends RubyException {
public LogstashTimestampParserError(final Ruby runtime, final RubyClass metaClass) {
super(runtime, metaClass);
}
}
} }

View file

@ -78,8 +78,7 @@ public final class JrubyTimestampExtLibrary {
this.timestamp = new Timestamp(time.toString()); this.timestamp = new Timestamp(time.toString());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new RaiseException( throw new RaiseException(
getRuntime(), getRuntime(), RubyUtil.TIMESTAMP_PARSER_ERROR,
RubyUtil.LOGSTASH_MODULE.getClass("TimestampParserError"),
"invalid timestamp string format " + time, "invalid timestamp string format " + time,
true true
); );
@ -157,8 +156,7 @@ public final class JrubyTimestampExtLibrary {
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new RaiseException( throw new RaiseException(
context.runtime, context.runtime, RubyUtil.TIMESTAMP_PARSER_ERROR,
RubyUtil.LOGSTASH_MODULE.getClass("TimestampParserError"),
"invalid timestamp format " + e.getMessage(), "invalid timestamp format " + e.getMessage(),
true true
); );
@ -174,8 +172,7 @@ public final class JrubyTimestampExtLibrary {
return fromRString(context.runtime, (RubyString) time); return fromRString(context.runtime, (RubyString) time);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new RaiseException( throw new RaiseException(
context.runtime, context.runtime, RubyUtil.TIMESTAMP_PARSER_ERROR,
RubyUtil.LOGSTASH_MODULE.getClass("TimestampParserError"),
"invalid timestamp format " + e.getMessage(), "invalid timestamp format " + e.getMessage(),
true true
); );