lib/trinidad/web_app.rb in trinidad-0.9.0 vs lib/trinidad/web_app.rb in trinidad-0.9.1

- old
+ new

@@ -1,118 +1,81 @@ module Trinidad class WebApp - attr_reader :context, :config + attr_reader :config, :app_config, :class_loader, :servlet - def self.create(context, config, app) - app.has_key?(:rackup) ? RackupWebApp.new(context, config, app) : RailsWebApp.new(context, config, app) + def self.create(config, app_config) + app_config.has_key?(:rackup) ? RackupWebApp.new(config, app_config) : RailsWebApp.new(config, app_config) end - def initialize(context, config, app) - @context = context + def initialize(config, app_config, servlet_class = 'org.jruby.rack.RackServlet', servlet_name = 'RackServlet') @config = config - @app = app + @app_config = app_config @class_loader = org.jruby.util.JRubyClassLoader.new(JRuby.runtime.jruby_class_loader) + @servlet = {:class => servlet_class, :name => servlet_name} unless rack_servlet_configured? end - def configure_rack(servlet_class = 'org.jruby.rack.RackServlet', servlet_name = 'RackServlet') - unless rack_configured? - wrapper = @context.createWrapper() - wrapper.setServletClass(servlet_class) - wrapper.setName(servlet_name) - - @context.addChild(wrapper) - @context.addServletMapping('/*', servlet_name) - end + def rack_listener + context_listener unless rack_listener_configured? end - def add_context_loader - add_application_libs(@class_loader) - add_application_classes(@class_loader) - - loader = Trinidad::Tomcat::WebappLoader.new(@class_loader) - - loader.container = @context - @context.loader = loader + def init_params + @params ||= {} + add_parameter_unless_exist 'jruby.min.runtimes', jruby_min_runtimes.to_s + add_parameter_unless_exist 'jruby.max.runtimes', jruby_max_runtimes.to_s + add_parameter_unless_exist 'jruby.initial.runtimes', jruby_min_runtimes.to_s + add_parameter_unless_exist 'public.root', File.join('/', public_root) + @params end - def add_init_params - add_parameter_unless_exist('jruby.min.runtimes', jruby_min_runtimes.to_s) - add_parameter_unless_exist('jruby.max.runtimes', jruby_max_runtimes.to_s) - add_parameter_unless_exist('jruby.initial.runtimes', jruby_min_runtimes.to_s) - add_parameter_unless_exist('public.root', File.join('/', public_root)) - end - - def add_rack_context_listener - unless rack_listener_configured? - @context.addApplicationListener(context_listener) + def default_deployment_descriptor + @deployment_descriptor ||= if default_web_xml + file = File.expand_path(File.join(web_app_dir, default_web_xml)) + File.exist?(file) ? file : nil end end - def add_application_libs(class_loader) - resources_dir = File.join(@app[:web_app_dir], libs_dir, '**', '*.jar') - - Dir[resources_dir].each do |resource| - class_loader.addURL(java.io.File.new(resource).to_url) - end + def rack_servlet_configured? + !!(web_xml && (web_xml.include?('<servlet-class>org.jruby.rack.RackServlet') || + web_xml.include?('<filter-class>org.jruby.rack.RackFilter'))) end - def add_application_classes(class_loader) - resources_dir = File.join(@app[:web_app_dir], classes_dir) - class_loader.addURL(java.io.File.new(resources_dir).to_url) - end - - def load_default_web_xml - file = File.expand_path(File.join(@app[:web_app_dir], default_web_xml)) - file = File.expand_path("../#{provided_web_xml}", __FILE__) unless File.exist?(file) - - @context.setDefaultWebXml(file) - - context_config = Trinidad::Tomcat::ContextConfig.new - context_config.setDefaultWebXml(file) - - @context.addLifecycleListener(context_config) - end - - def rack_configured? - return false if @context.getDefaultWebXml().nil? - - web_xml = IO.read(@context.getDefaultWebXml()).gsub(/\s+/, '') - - return web_xml.include?('<servlet-class>org.jruby.rack.RackServlet') || - web_xml.include?('<filter-class>org.jruby.rack.RackFilter') - end - def rack_listener_configured? - return false if @context.getDefaultWebXml().nil? - - web_xml = IO.read(@context.getDefaultWebXml()).gsub(/\s+/, '') - - return web_xml.include?("<listener-class>#{context_listener}") + !!(web_xml && web_xml.include?("<listener-class>#{context_listener}")) end def public_root - @context.findParameter('public.root') || @app[:public] || @config[:public] || 'public' + @app_config[:public] || @config[:public] || 'public' end - %w{libs_dir classes_dir default_web_xml environment jruby_min_runtimes jruby_max_runtimes}.each do |method_name| + %w{web_app_dir libs_dir classes_dir default_web_xml environment jruby_min_runtimes jruby_max_runtimes rackup}.each do |method_name| define_method method_name do sym = method_name.to_sym - @app[sym] || @config[sym] + @app_config[sym] || @config[sym] end end - def add_parameter_unless_exist(name, value) - @context.addParameter(name, value) unless @context.findParameter(name) + def extensions + @extensions ||= begin + extensions = @config[:extensions] || {} + extensions.merge!(@app_config[:extensions]) if @app_config[:extensions] + extensions + end end - def load_extensions? - @app.has_key?(:extensions) + protected + def add_parameter_unless_exist(param_name, param_value) + @params[param_name] = param_value unless web_context_param(param_name) end - def configure_extensions(tomcat) - return unless load_extensions? + private + def web_xml + @web_xml ||= File.read(default_deployment_descriptor).gsub(/\s+/, '') unless default_deployment_descriptor.nil? + end - Trinidad::Extensions.configure_webapp_extensions(@app[:extensions], tomcat, @context) - end + def web_context_param(param) + if web_xml =~ /<context-param><param-name>#{param}<\/param-name><param-value>(.+)<\/param-value>/ + return $1 + end + end end end