diff --git a/lib/logstash/environment.rb b/lib/logstash/environment.rb index bf2f2ccf8..cced4ed57 100644 --- a/lib/logstash/environment.rb +++ b/lib/logstash/environment.rb @@ -7,6 +7,8 @@ module LogStash LOGSTASH_HOME = ::File.expand_path(::File.join(::File.dirname(__FILE__), "/../..")) JAR_DIR = ::File.join(LOGSTASH_HOME, "/vendor/jar") + # loads currenly embedded elasticsearch jars + # @raise LogStash::EnvironmentError if not runnig under JRuby or if no jar files found def load_elasticsearch_jars! assess_jruby! @@ -22,8 +24,17 @@ module LogStash end end - def assess_jruby!(exception_class = nil, message = nil) - raise(exception_class || LogStash::EnvironmentError, message || "JRuby is required") unless jruby? + # @yield execute optional block if not currently running under JRuby + # @yieldreturn [Exception] exception to raise if Exception class returned otherwise raise default exception + # @raise [Exception] yielded exception or default if not runnig under JRuby + def assess_jruby! + unless jruby? + # grab return value from block if present, use default exception if not an exception class + exception = block_given? ? yield : nil + exception = LogStash::EnvironmentError.new("JRuby is required") unless exception.is_a?(Exception) + + raise(exception) + end end def jruby? diff --git a/lib/logstash/outputs/elasticsearch.rb b/lib/logstash/outputs/elasticsearch.rb index 54e171780..5bd74d26f 100644 --- a/lib/logstash/outputs/elasticsearch.rb +++ b/lib/logstash/outputs/elasticsearch.rb @@ -197,7 +197,7 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base if ["node", "transport"].include?(@protocol) # Node or TransportClient; requires JRuby - LogStash::Environment.assess_jruby!(LogStash::PluginLoadingError, "This configuration requires JRuby. If you are not using JRuby, you must set 'protocol' to 'http'. For example: output { elasticsearch { protocol => \"http\" } }") + LogStash::Environment.assess_jruby!{LogStash::PluginLoadingError.new("This configuration requires JRuby. If you are not using JRuby, you must set 'protocol' to 'http'. For example: output { elasticsearch { protocol => \"http\" } }")} LogStash::Environment.load_elasticsearch_jars! # setup log4j properties for Elasticsearch @@ -241,7 +241,7 @@ class LogStash::Outputs::ElasticSearch < LogStash::Outputs::Base :protocol => @protocol) if @embedded - LogStash::Environment.assess_jruby!(LogStash::ConfigurationError, "The 'embedded => true' setting is only valid for the elasticsearch output under JRuby. You are running #{RUBY_DESCRIPTION}") + LogStash::Environment.assess_jruby!{LogStash::ConfigurationError.new("The 'embedded => true' setting is only valid for the elasticsearch output under JRuby. You are running #{RUBY_DESCRIPTION}")} LogStash::Environment.load_elasticsearch_jars! # Default @host with embedded to localhost. This should help avoid diff --git a/spec/environmnet.rb b/spec/environmnet.rb index 48a8bc668..c9e8f1772 100644 --- a/spec/environmnet.rb +++ b/spec/environmnet.rb @@ -2,12 +2,39 @@ require "logstash/environment" describe LogStash::Environment do - it "should load elasticsarch jars" do - expect {LogStash::Environment.load_elasticsearch_jars!}.to_not raise_error + describe "load_elasticsearch_jars!" do + + it "should load elasticsarch jars" do + expect{LogStash::Environment.load_elasticsearch_jars!}.to_not raise_error + end + + it "should raise when cannot find elasticsarch jars" do + stub_const("LogStash::Environment::JAR_DIR", "/some/invalid/path") + expect{LogStash::Environment.load_elasticsearch_jars!}.to raise_error(LogStash::EnvironmentError) + end end - it "should raise when cannot find elasticsarch jars" do - stub_const("LogStash::Environment::JAR_DIR", "/some/invalid/path") - expect {LogStash::Environment.load_elasticsearch_jars!}.to raise_error(LogStash::EnvironmentError) + describe "assess_jruby!" do + + it "should not raise when jruby" do + expect(LogStash::Environment).to receive(:jruby?).twice.and_return(true) + expect{LogStash::Environment.assess_jruby!}.to_not raise_error + expect{LogStash::Environment.assess_jruby!{StandardError.new}}.to_not raise_error + end + + it "should raise default exception" do + expect(LogStash::Environment).to receive(:jruby?).once.and_return(false) + expect{LogStash::Environment.assess_jruby!}.to raise_error(LogStash::EnvironmentError) + end + + it "should yield to block and raise returned exception" do + expect(LogStash::Environment).to receive(:jruby?).once.and_return(false) + expect{LogStash::Environment.assess_jruby!{StandardError.new}}.to raise_error(StandardError) + end + + it "should yield to block and raise default exception if exception not returned" do + expect(LogStash::Environment).to receive(:jruby?).once.and_return(false) + expect{LogStash::Environment.assess_jruby!{nil}}.to raise_error(LogStash::EnvironmentError) + end end end