lib/trinidad/web_app.rb in trinidad-1.4.4 vs lib/trinidad/web_app.rb in trinidad-1.4.5.B1

- old
+ new

@@ -11,11 +11,11 @@ war?(config, default_config) ? WarWebApp.new(config, default_config) : rackup?(config, default_config) ? RackupWebApp.new(config, default_config) : RailsWebApp.new(config, default_config) end - def initialize(config, default_config) + def initialize(config, default_config = Trinidad.configuration) @config, @default_config = config, default_config || {} complete_config! # NOTE: we should maybe @config.freeze here ?! end @@ -28,17 +28,20 @@ key = key.to_sym return true if config.has_key?(key) use_default ? default_config.key?(key) : false end - %w{ root_dir rackup log async_supported reload_strategy }.each do |method| + %w{ root_dir rackup async_supported reload_strategy host_name }.each do |method| class_eval "def #{method}; self[:'#{method}']; end" end - alias_method :web_app_dir, :root_dir # is getting deprecated soon + alias_method :web_app_dir, :root_dir # is getting deprecated soon def app_root; root_dir; end + # @deprecated use `self[:log]` instead + def log; self[:log]; end + def context_path path = self[:context_path] || self[:path] path ? path.to_s : path end @@ -85,11 +88,11 @@ end def environment; self[:environment] || @@defaults[:environment]; end # TODO check web.xml def public_dir - @public_dir ||= expand_path(public_root) + @public_dir ||= ( public_root == '/' ? root_dir : expand_path(public_root) ) end # by (a "Rails") convention use '[RAILS_ROOT]/tmp' def work_dir @work_dir ||= self[:work_dir] || File.join(root_dir, 'tmp') @@ -142,10 +145,11 @@ 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 add_context_param 'public.root', public_root add_context_param 'jruby.rack.layout_class', layout_class + 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 @@ -386,24 +390,12 @@ 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 + def layout_class + 'JRuby::Rack::FileSystemLayout' # handles Rails as well as Rack end def complete_config! config[:root_dir] ||= self.class.root_dir(config, default_config) config[:root_dir] = File.expand_path(config[:root_dir]) @@ -536,21 +528,24 @@ end false end def self.war?(config, default_config = nil) - config[:context_path] && config[:context_path].to_s[-4..-1] == '.war' + 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 self.root_dir(config, default_config) + 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] ) ) || - Dir.pwd + default_dir end def self.context_path(config, default_config = nil) path = config[:context_path] || ( default_config && default_config[:context_path] ) @@ -627,11 +622,11 @@ end end - # Rack web application (looks for a rackup config.ru file). + # 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 @@ -644,11 +639,11 @@ def context_listener; 'org.jruby.rack.RackServletContextListener'; end end - # Rails web app specifics. Supports the same versions as jruby-rack ! + # Rails web application specifics (supports same versions as JRuby-Rack). class RailsWebApp < WebApp def context_params add_context_param 'rails.root', app_root add_context_param 'rails.env', environment @@ -678,40 +673,79 @@ 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!/) + File.exist?(file) && ( + file_line_match?(file, /^[^#]*threadsafe!/) || ( # Rails 4.0 + file_line_match?(file, /^[^#]*config\.eager_load = true/) && + file_line_match?(file, /^[^#]*config\.cache_classes = true/) + ) + ) end end # A web application for deploying (java) .war files. class WarWebApp < WebApp + def root_dir + @root_dir ||= ( config[:root_dir] || begin + path = config[:context_path] + path.to_s if path.to_s[-4..-1] == '.war' + end || default_confit[:root_dir] ) + end + def context_path - super.gsub(/\.war$/, '') + @path ||= begin + path = File.basename(super) + context_name = Trinidad::Tomcat::ContextName.new(path) + context_name.path # removes .war handles ## versioning + end end - - def log_dir - @log_dir ||= self[:log_dir] || File.join(work_dir, 'log') - end - + def work_dir - @work_dir ||= File.join(root_dir.gsub(/\.war$/, ''), 'WEB-INF') + self[:work_dir] end + def log_dir + @log_dir ||= self[:log_dir] || begin + if work_dir then work_dir + else + if root_dir[-4..-1] == '.war' + parent_dir = File.dirname(root_dir) + expanded_dir = File.join(parent_dir, context_path) + File.exist?(expanded_dir) ? expanded_dir : parent_dir + else + File.join(root_dir, 'log') + end + end + end + end + def monitor - File.expand_path(root_dir) + 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 define_lifecycle - Trinidad::Lifecycle::WebApp::War.new(self) + def context_params + warbler? ? super : @context_params ||= {} end - + def layout_class 'JRuby::Rack::WebInfLayout' end + + def define_lifecycle + Trinidad::Lifecycle::WebApp::War.new(self) + end + + private + + def warbler?; nil; end # TODO detect warbler created .war ?! end end