lib/tomcat-rails/web_app.rb in calavera-tomcat-rails-0.1.2 vs lib/tomcat-rails/web_app.rb in calavera-tomcat-rails-0.2.0

- old
+ new

@@ -1,11 +1,11 @@ module TomcatRails class WebApp - attr_reader :web_app, :config + attr_reader :context, :config - def initialize(web_app, config) - @web_app = web_app + def initialize(context, config) + @context = context @config = config end def add_rack_filter filter_def = TomcatRails::Tomcat::FilterDef.new @@ -15,43 +15,40 @@ pattern = @config[:context_path][-1..-1] != '/' ? @config[:context_path] : @config[:context_path][0..-2] filter_map = TomcatRails::Tomcat::FilterMap.new filter_map.setFilterName('RackFilter') filter_map.addURLPattern("#{pattern}/*") - @web_app.addFilterDef(filter_def) - @web_app.addFilterMap(filter_map) + @context.addFilterDef(filter_def) + @context.addFilterMap(filter_map) end def add_context_loader class_loader = org.jruby.util.JRubyClassLoader.new(JRuby.runtime.jruby_class_loader) add_application_libs(class_loader) add_application_classes(class_loader) loader = TomcatRails::Tomcat::WebappLoader.new(class_loader) - loader.container = @web_app - @web_app.loader = loader + loader.container = @context + @context.loader = loader end def add_init_params - [:jruby_min_runtimes, :jruby_max_runtimes].each do |param| - @web_app.addParameter(param.to_s.gsub(/_/, '.'), @config[param].to_s) - end + add_init_params_array([:jruby_min_runtimes, :jruby_max_runtimes]) - @web_app.addParameter('jruby.initial.runtimes', @config[:jruby_min_runtimes].to_s) - @web_app.addParameter('rails.env', @config[:environment].to_s) - @web_app.addParameter('rails.root', '/') - @web_app.addParameter('public.root', '/public') - + @context.addParameter('jruby.initial.runtimes', @config[:jruby_min_runtimes].to_s) + @context.addParameter('rails.env', @config[:environment].to_s) + @context.addParameter('rails.root', '/') + @context.addParameter('public.root', '/public') end def add_web_dir_resources - @web_app.setDocBase(File.join(Dir.pwd, "public/")) + @context.setDocBase(File.join(@config[:web_app_dir], "public/")) end def add_rack_context_listener - @web_app.addApplicationListener('org.jruby.rack.rails.RailsServletContextListener') + @context.addApplicationListener('org.jruby.rack.rails.RailsServletContextListener') end def add_application_libs(class_loader) resources_dir = File.join(@config[:web_app_dir], @config[:libs_dir], '**', '*.jar') @@ -61,8 +58,29 @@ end def add_application_classes(class_loader) resources_dir = File.join(@config[:web_app_dir], @config[:classes_dir]) class_loader.addURL(java.io.File.new(resources_dir).to_url) + end + + def load_default_web_xml + default_web_xml = File.expand_path(File.join(@config[:web_app_dir], @config[:default_web_xml])) + + if File.exist?(default_web_xml) + @context.setDefaultWebXml(default_web_xml) + @context.setDefaultContextXml(default_web_xml) + + context_config = TomcatRails::Tomcat::ContextConfig.new + context_config.setDefaultWebXml(default_web_xml) + + @context.addLifecycleListener(context_config) + end + end + + private + def add_init_params_array(array) + array.each do |param| + @context.addParameter(param.to_s.gsub(/_/, '.'), @config[param].to_s) + end end end end \ No newline at end of file