Skip to content

Commit 3c99aa0

Browse files
committed
using out "servlet-specific" path methods in booter
- we're avoiding File.exist? and expanding path specifically for the application layout this helps for a better boot in non-expanded .war scenarios with current JRuby (1.7.11)
1 parent 683e58a commit 3c99aa0

File tree

4 files changed

+110
-100
lines changed

4 files changed

+110
-100
lines changed

src/main/ruby/jruby/rack/rails/environment2.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# Rails 2.x (JRuby-Rack >= 1.1.0 only supports 2.3) specific booter behavior.
1111
module JRuby::Rack::RailsBooter::Rails2Environment
12-
12+
1313
# @return [::Rack::Adapter::Rails] a Rack compatible Rails application wrapper
1414
def to_app
1515
# backward "compatibility" calling #to_app without a #load_environment
@@ -22,7 +22,7 @@ def to_app
2222
# Loads the Rails 2.x environment.
2323
def load_environment
2424
require 'jruby/rack/rails/boot_hook'
25-
load File.join(app_path, 'config', 'environment.rb')
25+
load expand_path('config/environment.rb') # load File.join(app_path, 'config', 'environment.rb')
2626
require 'dispatcher'
2727
setup_sessions
2828
setup_logger
@@ -35,9 +35,9 @@ def load_environment
3535
def set_public_root
3636
silence_warnings { Object.const_set(:PUBLIC_ROOT, public_path) }
3737
end
38-
38+
3939
public
40-
40+
4141
# This hook method is called back from within the mechanism installed
4242
# by rails_boot above. We're setting appropriate defaults for the
4343
# servlet environment here that can still be overridden (if desired) in
@@ -77,7 +77,7 @@ def setup_relative_url_root
7777
ActionController::Base.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
7878
end
7979
end
80-
80+
8181
def rack_based_sessions?
8282
defined?(ActionController::Session::AbstractStore)
8383
end
@@ -100,7 +100,7 @@ def setup_logger
100100
if defined?(ActiveSupport::BufferedLogger) && # we only support Rails 2.3
101101
logger.is_a?(ActiveSupport::BufferedLogger)
102102
# since there's no way to detect whether this is a custom config.logger
103-
# declaration or the (default) Rails configured logger we assume all
103+
# declaration or the (default) Rails configured logger we assume all
104104
# ActiveSupport::BufferedLogger instances to be the default and patch :
105105
old_dev = logger.send :instance_variable_get, "@log"
106106
old_dev.close rescue nil
@@ -122,9 +122,9 @@ def java_sessions?
122122
def pstore_sessions?
123123
session_options[:database_manager] == (defined?(::CGI::Session::PStore) && ::CGI::Session::PStore)
124124
end
125-
125+
126126
class RailsRequestSetup # :nodoc only used for (non-rack based) CGI sessions
127-
127+
128128
def initialize(app, session_options)
129129
@app, @session_options = app, session_options
130130
end
@@ -135,7 +135,7 @@ def call(env)
135135
env['rails.session_options'] = options
136136
@app.call(env)
137137
end
138-
138+
139139
end
140140

141141
end

src/main/ruby/jruby/rack/rails/environment3.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ def to_app
1717
load_environment
1818
::Rails.application
1919
end
20-
20+
2121
# Loads the Rails environment (*config/environment.rb*).
2222
def load_environment
23-
require File.join(app_path, 'config', 'boot')
23+
require expand_path('config/boot.rb')
2424
require 'jruby/rack/rails/railtie'
25-
require File.join(app_path, 'config', 'environment')
25+
require expand_path('config/environment.rb')
2626
require 'jruby/rack/rails/extensions3'
2727
end
2828

2929
protected
30-
30+
3131
# The public root is set in {JRuby::Rack::Railtie}.
3232
def set_public_root
3333
# no-op here
3434
end
35-
35+
3636
end

src/main/ruby/jruby/rack/rails_booter.rb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def boot!
2626
super
2727
ENV['RAILS_ROOT'] = app_path
2828
ENV['RAILS_ENV'] = rails_env
29-
30-
if File.exist?(File.join(app_path, "config", "application.rb"))
31-
require 'jruby/rack/rails/environment3'
32-
extend Rails3Environment
33-
else
29+
30+
if rails2?
3431
require 'jruby/rack/rails/environment2'
3532
extend Rails2Environment
33+
else
34+
require 'jruby/rack/rails/environment3'
35+
extend Rails3Environment
3636
end
3737

3838
set_public_root
@@ -69,24 +69,32 @@ def run_boot_hooks
6969
# no-op hooks run when 'jruby/rack/rails/extensions' gets loaded
7070
end
7171

72-
public
72+
private
7373

74-
# @see #RailsRackApplicationFactory
75-
def self.load_environment # :nodoc
76-
rails_booter.load_environment
74+
def rails2?
75+
# a File.exist? File.join(app_path, 'config/application.rb')
76+
! real_path File.join(layout.app_uri, 'config/application.rb')
7777
end
7878

79-
# @see #RailsRackApplicationFactory
80-
def self.to_app # :nodoc
81-
rails_booter.to_app
82-
end
79+
class << self
8380

84-
private
81+
# @see #RailsRackApplicationFactory
82+
# @private
83+
def self.load_environment; rails_booter.load_environment end
84+
85+
# @see #RailsRackApplicationFactory
86+
# @private
87+
def to_app; rails_booter.to_app end
88+
89+
private
90+
91+
# @private
92+
def rails_booter
93+
raise "no booter set" unless booter = JRuby::Rack.booter
94+
raise "not a rails booter" unless booter.is_a?(JRuby::Rack::RailsBooter)
95+
booter
96+
end
8597

86-
def self.rails_booter # :nodoc
87-
raise "no booter set" unless booter = JRuby::Rack.booter
88-
raise "not a rails booter" unless booter.is_a?(JRuby::Rack::RailsBooter)
89-
booter
9098
end
9199

92100
end

src/spec/ruby/jruby/rack/rails_booter_spec.rb

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class ::CGI::Session::PStore; end
3535
@@rack_env.nil? ? ENV.delete('RACK_ENV') : ENV['RACK_ENV'] = @@rack_env
3636
end
3737

38-
it "should default RAILS_ROOT to /WEB-INF" do
39-
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "./WEB-INF"
38+
it "should default rails path to /WEB-INF" do
39+
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/usr/apps/WEB-INF"
4040
booter.boot!
41-
booter.app_path.should == "./WEB-INF"
41+
booter.app_path.should == "/usr/apps/WEB-INF"
4242
end
4343

4444
it "leaves ENV['RAILS_ENV'] as is if it was already set" do
@@ -101,98 +101,100 @@ class ::CGI::Session::PStore; end
101101
booter.logger.instance_variable_get(:@logdev).write "hello"
102102
end
103103

104-
it "should setup java servlet-based sessions if the session store is the default",
105-
:lib => [ :stub ] do
104+
describe "Rails 2 environment", :lib => :stub do
106105

107-
booter.boot!
108-
booter.should_receive(:rack_based_sessions?).and_return false
106+
before do
107+
booter.stub(:rails2?).and_return true
108+
end
109109

110-
booter.session_options[:database_manager] = ::CGI::Session::PStore
111-
booter.setup_sessions
112-
booter.session_options[:database_manager].should == ::CGI::Session::JavaServletStore
113-
end
110+
it "sets up java servlet-based sessions if the session store is the default" do
114111

115-
it "should turn off Ruby CGI cookies if the java servlet store is used",
116-
:lib => [ :stub ] do
112+
booter.boot!
113+
booter.should_receive(:rack_based_sessions?).and_return false
117114

118-
booter.boot!
119-
booter.should_receive(:rack_based_sessions?).and_return false
115+
booter.session_options[:database_manager] = ::CGI::Session::PStore
116+
booter.setup_sessions
117+
booter.session_options[:database_manager].should == ::CGI::Session::JavaServletStore
118+
end
120119

121-
booter.session_options[:database_manager] = ::CGI::Session::JavaServletStore
122-
booter.setup_sessions
123-
booter.session_options[:no_cookies].should == true
124-
end
120+
it "turns off Ruby CGI cookies if the java servlet store is used" do
125121

126-
it "should provide the servlet request in the session options if the java servlet store is used",
127-
:lib => [ :stub ] do
122+
booter.boot!
123+
booter.should_receive(:rack_based_sessions?).and_return false
128124

129-
booter.boot!
130-
booter.should_receive(:rack_based_sessions?).twice.and_return false
125+
booter.session_options[:database_manager] = ::CGI::Session::JavaServletStore
126+
booter.setup_sessions
127+
booter.session_options[:no_cookies].should == true
128+
end
131129

132-
booter.session_options[:database_manager] = ::CGI::Session::JavaServletStore
133-
booter.setup_sessions
134-
booter.instance_variable_set :@load_environment, true
130+
it "provides the servlet request in the session options if the java servlet store is used" do
135131

136-
::Rack::Adapter::Rails.should_receive(:new).and_return app = double("rails adapter")
137-
app.should_receive(:call)
132+
booter.boot!
133+
booter.should_receive(:rack_based_sessions?).twice.and_return false
138134

139-
env = { "java.servlet_request" => double("servlet request") }
140-
booter.to_app.call(env)
141-
env['rails.session_options'].should have_key(:java_servlet_request)
142-
env['rails.session_options'][:java_servlet_request].should == env["java.servlet_request"]
143-
end
135+
booter.session_options[:database_manager] = ::CGI::Session::JavaServletStore
136+
booter.setup_sessions
137+
booter.instance_variable_set :@load_environment, true
144138

145-
it "should set the PUBLIC_ROOT constant to the location of the public root",
146-
:lib => [ :rails23, :stub ] do
139+
::Rack::Adapter::Rails.should_receive(:new).and_return app = double("rails adapter")
140+
app.should_receive(:call)
147141

148-
begin
149-
booter.app_path = File.expand_path("../../../rails", __FILE__)
150-
booter.boot!
151-
PUBLIC_ROOT.should == booter.public_path
152-
ensure
153-
Object.send :remove_const, :PUBLIC_ROOT
142+
env = { "java.servlet_request" => double("servlet request") }
143+
booter.to_app.call(env)
144+
env['rails.session_options'].should have_key(:java_servlet_request)
145+
env['rails.session_options'][:java_servlet_request].should == env["java.servlet_request"]
154146
end
155-
end
156147

157-
describe "Rails 2 environment", :lib => :stub do
148+
it "should set the PUBLIC_ROOT constant to the location of the public root",
149+
:lib => [ :rails23, :stub ] do
158150

159-
before :each do
160-
$servlet_context = @servlet_context
161-
@rack_context.should_receive(:getContextPath).and_return "/foo"
162151
booter.app_path = File.expand_path("../../../rails", __FILE__)
163152
booter.boot!
164-
silence_warnings { booter.load_environment }
153+
expect( PUBLIC_ROOT ).to eql booter.public_path
165154
end
166155

167-
after(:each) { Object.send :remove_const, :PUBLIC_ROOT }
168-
169-
after :all do
170-
$servlet_context = nil
156+
after(:each) do
157+
Object.send :remove_const, :PUBLIC_ROOT if Object.const_defined? :PUBLIC_ROOT
171158
end
172159

173-
it "should default the page cache directory to the public root" do
174-
ActionController::Base.page_cache_directory.should == booter.public_path
175-
end
160+
context 'booted' do
176161

177-
it "should default the session store to the java servlet session store" do
178-
ActionController::Base.session_store.should == CGI::Session::JavaServletStore
179-
end
162+
before :each do
163+
$servlet_context = @servlet_context
164+
@rack_context.should_receive(:getContextPath).and_return "/foo"
165+
booter.app_path = File.expand_path("../../../rails", __FILE__)
166+
booter.boot!
167+
silence_warnings { booter.load_environment }
168+
end
180169

181-
it "should set the ActionView ASSETS_DIR constant to the public root" do
182-
ActionView::Helpers::AssetTagHelper::ASSETS_DIR.should == booter.public_path
183-
end
170+
after(:all) { $servlet_context = nil }
184171

185-
it "should set the ActionView JAVASCRIPTS_DIR constant to the public root/javascripts" do
186-
ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR.should == booter.public_path + "/javascripts"
187-
end
172+
it "should default the page cache directory to the public root" do
173+
ActionController::Base.page_cache_directory.should == booter.public_path
174+
end
188175

189-
it "should set the ActionView STYLESHEETS_DIR constant to the public root/stylesheets" do
190-
ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR.should == booter.public_path + "/stylesheets"
191-
end
176+
it "should default the session store to the java servlet session store" do
177+
ActionController::Base.session_store.should == CGI::Session::JavaServletStore
178+
end
179+
180+
it "should set the ActionView ASSETS_DIR constant to the public root" do
181+
ActionView::Helpers::AssetTagHelper::ASSETS_DIR.should == booter.public_path
182+
end
183+
184+
it "should set the ActionView JAVASCRIPTS_DIR constant to the public root/javascripts" do
185+
ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR.should == booter.public_path + "/javascripts"
186+
end
187+
188+
it "should set the ActionView STYLESHEETS_DIR constant to the public root/stylesheets" do
189+
ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR.should == booter.public_path + "/stylesheets"
190+
end
191+
192+
it "should set the ActionController.relative_url_root to the servlet context path" do
193+
ActionController::Base.relative_url_root.should == "/foo"
194+
end
192195

193-
it "should set the ActionController.relative_url_root to the servlet context path" do
194-
ActionController::Base.relative_url_root.should == "/foo"
195196
end
197+
196198
end
197199

198200
# NOTE: specs currently only test with a stubbed Rails::Railtie

0 commit comments

Comments
 (0)