Skip to content

Commit

Permalink
explicitly pass rack context into ServletLog (a "better" fix for jrub…
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Mar 21, 2012
1 parent 8d37bfb commit 3c8ce8d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jruby/rack/embed/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void initialize() {
if (context instanceof Context) {
((Context) context).getConfig().doInitialize(runtime);
}
// set servlet context as a global variable :
// TODO seems confusing as we're not really exposing a servlet context:
IRubyObject rubyContext = JavaUtil.convertJavaToRuby(runtime, context);
runtime.getGlobalVariables().set("$servlet_context", rubyContext);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/ruby/jruby/rack/servlet_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class JRuby::Rack::ServletLog
def initialize(context = $servlet_context)
raise ArgumentError, "no context" unless context
@context = context
end

Expand Down
26 changes: 24 additions & 2 deletions src/main/ruby/rack/handler/servlet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ def load_builtin(env, key)
when 'rack.multiprocess' then env[key] = false
when 'rack.run_once' then env[key] = false
when 'rack.input' then env[key] = @servlet_env.to_io
when 'rack.errors' then env[key] = JRuby::Rack::ServletLog.new
when 'rack.errors' then env[key] = JRuby::Rack::ServletLog.new(rack_context)
when 'rack.url_scheme'
scheme = env[key] = @servlet_env.getScheme
env['HTTPS'] = 'on' if scheme == 'https'
when 'java.servlet_request' then env[key] = @servlet_env.getRequest rescue @servlet_env
when 'java.servlet_response' then env[key] = @servlet_env.getResponse rescue @servlet_env
when 'java.servlet_context' then env[key] = $servlet_context
when 'java.servlet_context' then env[key] = servlet_context # nil for embed ?
when 'jruby.rack.context' then env[key] = rack_context # always present
when 'jruby.rack.version' then env[key] = JRuby::Rack::VERSION
when 'jruby.rack.jruby.version' then env[key] = JRUBY_VERSION
when 'jruby.rack.rack.release' then env[key] = ::Rack.release
Expand Down Expand Up @@ -185,6 +186,27 @@ def load__SERVER_PORT(env)
def load__SERVER_SOFTWARE(env)
env["SERVER_SOFTWARE"] = @servlet_env.context.getServerInfo
end

private

def rack_context
if @servlet_env.respond_to?(:context)
@servlet_env.context # RackEnvironment#getContext
else
$servlet_context || raise("missing rack context")
end
end

def servlet_context
return $servlet_context if $servlet_context
if @servlet_env.respond_to?(:context) &&
@servlet_env.context.is_a?(javax.servlet.ServletContext)
@servlet_env.context
else
nil
end
end

end
end
end
18 changes: 15 additions & 3 deletions src/spec/ruby/rack/handler/servlet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,29 @@ def enumeration(arr)
env["CONTENT_LENGTH"].should == "10"
end

it "should set the input and error keys" do
it "sets the rack.input and rack.errors keys" do
stub_env :getScheme => "http", :getContextPath => "/foo"
@env.stub!(:to_io).and_return StringIO.new
env = @servlet.create_lazy_env @env
(input = env['rack.input']).should_not be_nil

(input = env['rack.input']).should_not be nil
[:gets, :read, :each].each {|sym| input.respond_to?(sym).should == true }
(errors = env['rack.errors']).should_not be_nil
(errors = env['rack.errors']).should_not be nil
[:puts, :write, :flush].each {|sym| errors.respond_to?(sym).should == true }
env['java.servlet_request'].should == @servlet_env
end

it "sets the rack.errors to log via rack context" do
env = @servlet.create_lazy_env @env
env['rack.errors'].should be_a JRuby::Rack::ServletLog

@rack_context.should_receive(:log).with("bar").ordered
@rack_context.should_receive(:log).with("huu").ordered

env['rack.errors'].puts "bar"
env['rack.errors'].write "huu"
end

it "should set env['HTTPS'] = 'on' if scheme is https" do
stub_env :getScheme => "https"
env = @servlet.create_lazy_env @env
Expand Down

0 comments on commit 3c8ce8d

Please sign in to comment.