lib/trinidad/web_app.rb in trinidad-1.4.6 vs lib/trinidad/web_app.rb in trinidad-1.5.0.B1

- old
+ new

@@ -61,27 +61,29 @@ def allow_linking; key?(:allow_linking) ? self[:allow_linking] : true; end 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 : fetch_default_config_value(:jruby_min_runtimes) + else # but :threadsafe takes precedence over default + # (if no default value specified we'll end up thread-safe) : + self[:threadsafe] ? 1 : fetch_default_config_value(:jruby_min_runtimes, 1) 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 : fetch_default_config_value(:jruby_max_runtimes) + else # but :threadsafe takes precedence over default + # (if no default value specified we'll end up thread-safe) : + self[:threadsafe] ? 1 : fetch_default_config_value(:jruby_max_runtimes, 1) 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 : + else # but :threadsafe takes precedence over default : self[:threadsafe] ? 1 : fetch_default_config_value(:jruby_initial_runtimes, jruby_min_runtimes) end end @@ -129,28 +131,34 @@ def context_xml; self[:context_xml] || self[:default_context_xml]; end def web_xml; self[:web_xml] || self[:default_web_xml]; end def default_web_xml; self[:default_web_xml]; end def java_lib - # accepts #deprecated :libs_dir syntax - self[:java_lib] || self[:libs_dir] || @@defaults[:java_lib] + self[:java_lib] || begin + if libs_dir = self[:libs_dir] # @deprecated + Helpers.deprecate "please use :java_lib instead of :libs_dir" + end + libs_dir || @@defaults[:java_lib] + end end def java_classes - # accepts #deprecated :classes_dir syntax - self[:java_classes] || self[:classes_dir] || File.join(java_lib, 'classes') + self[:java_classes] || begin + if classes_dir = self[:classes_dir] # @deprecated + Helpers.deprecate "please use :java_classes instead of :classes_dir" + end + classes_dir || File.join(java_lib, 'classes') + end end def java_lib_dir @java_lib_dir ||= self[:java_lib_dir] || expand_path(java_lib) end - alias_method :libs_dir, :java_lib_dir # #deprecated def java_classes_dir @java_classes_dir ||= self[:java_classes_dir] || expand_path(java_classes) end - alias_method :classes_dir, :java_classes_dir # #deprecated def extensions @extensions ||= begin extensions = default_config[:extensions] || {} extensions.merge(config[:extensions] || {}) @@ -159,15 +167,18 @@ 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_initial_runtimes - add_context_param 'jruby.runtime.acquire.timeout', jruby_runtime_acquire_timeout + unless threadsafe? + add_context_param 'jruby.initial.runtimes', jruby_initial_runtimes + add_context_param 'jruby.runtime.acquire.timeout', jruby_runtime_acquire_timeout + end 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 + # JRuby::Rack::ErrorApp got a bit smarter so use it, TODO maybe override ? add_context_param 'jruby.rack.error', false # do not start error app on errors @context_params end # @deprecated replaced with {#context_params} def init_params; context_params; end @@ -218,14 +229,13 @@ # public: # root: /assets # cache: true # cache_ttl: 60000 def public_config - @public_config ||= - self[:public].is_a?(String) ? - { :root => self[:public] } : - ( self[:public] || {} ) + @public_config ||= begin; public = self[:public] + public.is_a?(String) ? { :root => public } : ( public || {} ) + end end def aliases # :public => { :aliases => ... } return nil unless aliases = ( self[:aliases] || public_config[:aliases] ) return aliases if aliases.is_a?(String) @@ -266,21 +276,10 @@ def cache_ttl # :public => { :cache_ttl => ... } # ((BaseDirContext) resources).setCacheTTL self[:cache_ttl] || public_config[:cache_ttl] end - def class_loader - @class_loader ||= - org.jruby.util.JRubyClassLoader.new(JRuby.runtime.jruby_class_loader) - end - - def class_loader! - ( @class_loader = nil ) || class_loader - end - # @deprecated replaced with {#class_loader!} - def generate_class_loader; class_loader!; end - def define_lifecycle Lifecycle::WebApp::Default.new(self) end # Reset the hold web application state so it gets re-initialized. @@ -418,12 +417,29 @@ 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) + + return if key?(:jruby_max_runtimes) || key?(:jruby_min_runtimes) + + if ( ! key?(:threadsafe) && ! detect_threadsafe? ) || self[:threadsafe] == false + max_runtimes = config[:jruby_max_runtimes] = guess_max_runtimes + if environment == 'development' || environment == 'test' + config[:jruby_min_runtimes] = 1 + else + config[:jruby_min_runtimes] = max_runtimes + end + else + config[:jruby_min_runtimes] = config[:jruby_max_runtimes] = 1 + end end + def detect_threadsafe?(environment = self.environment); true end + + def guess_max_runtimes; 5 end + public # Returns true if there's a servlet with the given servlet-class name # configured or if the optional name second argument is given it also # checks for a servlet with the given name. @@ -509,88 +525,90 @@ 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 = ENV_JAVA[ name.to_s.gsub('_', '.') ] value ||= default end value end def logger @logger ||= Logging::LogFactory.getLog('') end - protected + class << self - def self.rackup?(config, default_config = nil) - return true if config.has_key?(:rackup) - root_dir = root_dir(config, default_config) - config_ru = (default_config && default_config[:rackup]) || 'config.ru' - # check for rackup (but still use config/environment.rb for rails 3) - if File.exists?(File.join(root_dir, config_ru)) && - ! rails?(config, default_config) # do not :rackup a rails app - config[:rackup] = config_ru + def rackup?(config, default_config = nil) + return true if config.has_key?(:rackup) + root_dir = root_dir(config, default_config) + config_ru = (default_config && default_config[:rackup]) || 'config.ru' + # check for rackup (but still use config/environment.rb for rails 3) + if File.exists?(File.join(root_dir, config_ru)) && + ! rails?(config, default_config) # do not :rackup a rails app + config[:rackup] = config_ru + end + config[:rackup] || ! Dir[File.join(root_dir, 'WEB-INF/**/config.ru')].empty? end - config[:rackup] || ! Dir[File.join(root_dir, 'WEB-INF/**/config.ru')].empty? - end - def self.rails?(config, default_config = nil) - root_dir = root_dir(config, default_config) - # standart Rails 3.x `class Application < Rails::Application` - if File.exists?(application = File.join(root_dir, 'config/application.rb')) - return true if file_line_match?(application, /^[^#]*Rails::Application/) - end - if File.exists?(environment = File.join(root_dir, 'config/environment.rb')) - return true if file_line_match?(environment) do |line| - # customized Rails 3.x, expects a `Rails::Application` subclass - # or a plain-old Rails 2.3 with `RAILS_GEM_VERSION = '2.3.14'` - line =~ /^[^#]*Rails::Application/ || line =~ /^[^#]*RAILS_GEM_VERSION/ + def rails?(config, default_config = nil) + root_dir = root_dir(config, default_config) + # standard Rails 3.x/4.x `class Application < Rails::Application` + if File.exists?(application = File.join(root_dir, 'config/application.rb')) + return true if file_line_match?(application, /^[^#]*Rails::Application/) end + if File.exists?(environment = File.join(root_dir, 'config/environment.rb')) + return true if file_line_match?(environment) do |line| + # customized Rails >= 3.x, expects a `Rails::Application` subclass + # or a plain-old Rails 2.3 with `RAILS_GEM_VERSION = '2.3.14'` + line =~ /^[^#]*Rails::Application/ || line =~ /^[^#]*RAILS_GEM_VERSION/ + end + end + false end - false - end - def self.war?(config, default_config = nil) - root_dir = root_dir(config, default_config) - return true if root_dir && root_dir.to_s[-4..-1] == '.war' - context_path = config[:context_path] # backwards-compatibility : - context_path && context_path.to_s[-4..-1] == '.war' - end + def war?(config, default_config = nil) + root_dir = root_dir(config, default_config) + return true if root_dir && root_dir.to_s[-4..-1] == '.war' + context_path = config[:context_path] # backwards-compatibility : + context_path && context_path.to_s[-4..-1] == '.war' + end - private + def root_dir(config, default_config, default_dir = Dir.pwd) + # for backwards compatibility accepts the :web_app_dir "alias" + config[:root_dir] || config[:web_app_dir] || + ( default_config && + ( default_config[:root_dir] || default_config[:web_app_dir] ) ) || + default_dir + end - def self.root_dir(config, default_config, default_dir = Dir.pwd) - # for backwards compatibility accepts the :web_app_dir "alias" - config[:root_dir] || config[:web_app_dir] || - ( default_config && - ( default_config[:root_dir] || default_config[:web_app_dir] ) ) || - default_dir - end - - def self.context_path(config, default_config = nil) - path = config[:context_path] || - ( default_config && default_config[:context_path] ) - unless path - name = config[:context_name] || - ( default_config && default_config[:context_name] ) - path = name.to_s == 'default' ? '/' : "/#{name}" + def context_path(config, default_config = nil) + path = config[:context_path] || + ( default_config && default_config[:context_path] ) + unless path + name = config[:context_name] || + ( default_config && default_config[:context_name] ) + path = name.to_s == 'default' ? '/' : "/#{name}" + end + path = "/#{path}" if path.to_s[0, 1] != '/' + path.to_s end - path = "/#{path}" if path.to_s[0, 1] != '/' - path.to_s - end - def self.file_line_match?(path, pattern = nil) - File.open(path) do |file| - if block_given? - file.each_line { |line| return true if yield(line) } - else - file.each_line { |line| return true if line =~ pattern } + private + + def file_line_match?(path, pattern = nil) + File.open(path) do |file| + if block_given? + file.each_line { |line| return true if yield(line) } + else + file.each_line { |line| return true if line =~ pattern } + end end + false end - false + end class Holder def initialize(web_app, context) @@ -610,40 +628,10 @@ def locked?; !!@lock; end def lock; @lock = true; end def unlock; @lock = false; end - # #deprecated behaves Hash like for (<= 1.3.5) compatibility - def [](key) - case key.to_sym - when :app then - web_app - when :context then - context - when :lock then - @lock - when :monitor then - monitor - when :mtime then - monitor_mtime - else raise NoMethodError, key.to_s - end - end - - # #deprecated behaves Hash like for (<= 1.3.5) compatibility - def []=(key, val) - case key.to_sym - when :context then - self.context=(val) - when :lock then - @lock = val - when :mtime then - self.monitor_mtime=(val) - else raise NoMethodError, "#{key}=" - end - end - end end # Rack web application (looks for a "rackup" *config.ru* file). @@ -679,33 +667,54 @@ def web_xml_environment; web_xml_context_param('rails.env'); end protected - def complete_config! - super - # detect threadsafe! in config/environments/environment.rb : - if ! key?(:threadsafe) && self.class.threadsafe?(root_dir, environment) - config[:jruby_min_runtimes] = 1 unless key?(:jruby_min_runtimes, false) - config[:jruby_max_runtimes] = 1 unless key?(:jruby_max_runtimes, false) + # NOTE: maybe we can guess these based on connector maxThreads? + # def guess_max_runtimes; 5 end + + def detect_threadsafe?(environment = self.environment) + if environment == 'development' || environment == 'test' + # NOTE: it's best for development/test to use the same setup as in + # production by default, thread-safe likely won't be detected as : + # + # __environments/development.rb__ + # + # # In the development environment your application's code is reloaded on + # # every request. This slows down response time but is perfect for development + # # since you don't have to restart the web server when you make code changes. + # config.cache_classes = false + # + # # Do not eager load code on boot. + # config.eager_load = false + # + # __environments/test.rb__ + # + # # The test environment is used exclusively to run your application's + # # test suite. You never need to work with it otherwise. Remember that + # # your test database is "scratch space" for the test suite and is wiped + # # and recreated between test runs. Don't rely on the data there! + # config.cache_classes = true + # + # # Do not eager load code on boot. This avoids loading your whole application + # # just for the purpose of running a single test. If you are using a tool that + # # preloads Rails for running tests, you may have to set it to true. + # config.eager_load = false + # + return self.class.threadsafe?(root_dir, 'production') end + self.class.threadsafe?(root_dir, environment) 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") end def self.threadsafe_match?(file) File.exist?(file) && ( - file_line_match?(file, /^[^#]*threadsafe!/) || ( # Rails 4.0 + file_line_match?(file, /^[^#]*threadsafe!/) || ( # Rails 4.x file_line_match?(file, /^[^#]*config\.eager_load\s?*=\s?*true/) && file_line_match?(file, /^[^#]*config\.cache_classes\s?*=\s?*true/) ) ) end @@ -749,13 +758,9 @@ end end def monitor root_dir ? File.expand_path(root_dir) : nil # the .war file itself - end - - def class_loader - @class_loader ||= nil # lifecycle will setup JRuby CL end def context_params warbler? ? super : @context_params ||= {} end