lib/trinidad/web_app.rb in trinidad-1.4.3 vs lib/trinidad/web_app.rb in trinidad-1.4.4
- old
+ new
@@ -28,16 +28,17 @@
key = key.to_sym
return true if config.has_key?(key)
use_default ? default_config.key?(key) : false
end
- %w{ root_dir jruby_compat_version
- rackup log async_supported reload_strategy }.each do |method|
+ %w{ root_dir rackup log async_supported reload_strategy }.each do |method|
class_eval "def #{method}; self[:'#{method}']; end"
end
alias_method :web_app_dir, :root_dir # is getting deprecated soon
+ def app_root; root_dir; end
+
def context_path
path = self[:context_path] || self[:path]
path ? path.to_s : path
end
@@ -52,25 +53,38 @@
def jruby_min_runtimes
if min = config[:jruby_min_runtimes]
return min.to_i # min specified overrides :threadsafe
else # but :threadsafe takes precendence over default :
- self[:threadsafe] ? 1 : default_config[:jruby_min_runtimes]
+ self[:threadsafe] ? 1 : fetch_default_config_value(:jruby_min_runtimes)
end
end
def jruby_max_runtimes
if max = config[:jruby_max_runtimes]
return max.to_i # max specified overrides :threadsafe
else # but :threadsafe takes precendence over default :
- self[:threadsafe] ? 1 : default_config[:jruby_max_runtimes]
+ self[:threadsafe] ? 1 : fetch_default_config_value(:jruby_max_runtimes)
end
end
+ def jruby_initial_runtimes
+ if ini = config[:jruby_initial_runtimes]
+ return ini.to_i # min specified overrides :threadsafe
+ else # but :threadsafe takes precendence over default :
+ self[:threadsafe] ? 1 :
+ fetch_default_config_value(:jruby_initial_runtimes, jruby_min_runtimes)
+ end
+ end
+
def jruby_runtime_acquire_timeout
- self[:jruby_runtime_acquire_timeout] || 5.0 # default 10s seems too high
+ fetch_config_value(:jruby_runtime_acquire_timeout, 5.0) # default 10s seems too high
end
+
+ def jruby_compat_version
+ fetch_config_value(:jruby_compat_version, RUBY_VERSION)
+ end
def environment; self[:environment] || @@defaults[:environment]; end # TODO check web.xml
def public_dir
@public_dir ||= expand_path(public_root)
@@ -123,14 +137,15 @@
def context_params
@context_params ||= {}
add_context_param 'jruby.min.runtimes', jruby_min_runtimes
add_context_param 'jruby.max.runtimes', jruby_max_runtimes
- add_context_param 'jruby.initial.runtimes', jruby_min_runtimes
+ add_context_param 'jruby.initial.runtimes', jruby_initial_runtimes
add_context_param 'jruby.runtime.acquire.timeout', jruby_runtime_acquire_timeout
- add_context_param 'jruby.compat.version', jruby_compat_version || RUBY_VERSION
- add_context_param 'public.root', File.join('/', public_root)
+ add_context_param 'jruby.compat.version', jruby_compat_version
+ add_context_param 'public.root', public_root
+ add_context_param 'jruby.rack.layout_class', layout_class
@context_params
end
# @deprecated replaced with {#context_params}
def init_params; context_params; end
@@ -139,10 +154,26 @@
if ! param_value.nil? && ! web_xml_context_param(param_name)
@context_params[param_name] = param_value.to_s
end
end
+ def logging
+ @logging ||= begin
+ defaults = {
+ :level => log, # backwards compatibility
+ :use_parent_handlers => environment == 'development',
+ :file => {
+ :dir => log_dir,
+ :prefix => environment,
+ :suffix => '.log',
+ :rotate => true
+ }
+ }
+ Trinidad::Configuration.merge_options(defaults, self[:logging])
+ end
+ end
+
def deployment_descriptor
return nil if @deployment_descriptor == false
@deployment_descriptor ||= expand_path(web_xml) || false
end
@@ -355,12 +386,29 @@
def context_listener
raise NotImplementedError.new "context_listener expected to be redefined"
end
+ if Gem::Version.create(JRuby::Rack::VERSION) > Gem::Version.create('1.1.10')
+ def layout_class
+ 'JRuby::Rack::FileSystemLayout' # handles Rails as well as Rack
+ end
+ else
+ def layout_class
+ # NOTE: we'll also need to do a GEM_PATH hack to avoid a bug :
+ if gem_path = ENV['GEM_PATH']
+ add_context_param 'gem.path', gem_path
+ # ENV['GEM_PATH'] will contain twice the same entry(ies) ...
+ end
+ add_context_param 'rails.root', app_root # still boots a rack app
+ 'JRuby::Rack::RailsFilesystemLayout' # no plain FS layout defined !
+ end
+ end
+
def complete_config!
config[:root_dir] ||= self.class.root_dir(config, default_config)
+ config[:root_dir] = File.expand_path(config[:root_dir])
config[:context_path] = self.class.context_path(config, default_config)
end
public
@@ -437,11 +485,26 @@
else
File.expand_path(path, root_dir)
end
end
end
+
+ def fetch_config_value(name, default = nil)
+ value = config[name]
+ value.nil? ? fetch_default_config_value(name, default) : value
+ end
+ def fetch_default_config_value(name, default = nil)
+ value = default_config[name]
+ if value.nil?
+ # JRuby-Rack names: jruby_min_runtimes -> jruby.min.runtimes :
+ value = java.lang.System.getProperty(name.to_s.gsub('_', '.'))
+ value ||= default
+ end
+ value
+ end
+
def logger
@logger ||= Trinidad::Logging::LogFactory.getLog('')
end
protected
@@ -568,10 +631,11 @@
# Rack web application (looks for a rackup config.ru file).
class RackupWebApp < WebApp
def context_params
+ add_context_param 'app.root', app_root
add_context_param 'rack.env', environment
if rackup = self.rackup
rackup = File.join(rackup, 'config.ru') if File.directory?(rackup)
add_context_param 'rackup.path', rackup
end
@@ -584,12 +648,12 @@
# Rails web app specifics. Supports the same versions as jruby-rack !
class RailsWebApp < WebApp
def context_params
+ add_context_param 'rails.root', app_root
add_context_param 'rails.env', environment
- add_context_param 'rails.root', '/'
super
end
def context_listener; 'org.jruby.rack.rails.RailsServletContextListener'; end
@@ -602,10 +666,14 @@
config[:jruby_min_runtimes] = 1 unless key?(:jruby_min_runtimes, false)
config[:jruby_max_runtimes] = 1 unless key?(:jruby_max_runtimes, false)
end
end
+ #def layout_class
+ #'JRuby::Rack::RailsFileSystemLayout'
+ #end
+
private
def self.threadsafe?(app_base, environment)
threadsafe_match?("#{app_base}/config/environments/#{environment}.rb") ||
threadsafe_match?("#{app_base}/config/environment.rb")
@@ -621,11 +689,11 @@
class WarWebApp < WebApp
def context_path
super.gsub(/\.war$/, '')
end
-
+
def log_dir
@log_dir ||= self[:log_dir] || File.join(work_dir, 'log')
end
def work_dir
@@ -636,9 +704,13 @@
File.expand_path(root_dir)
end
def define_lifecycle
Trinidad::Lifecycle::WebApp::War.new(self)
+ end
+
+ def layout_class
+ 'JRuby::Rack::WebInfLayout'
end
end
end