Skip to content

Commit

Permalink
better servlet resolving - resolve optionally by name as well
Browse files Browse the repository at this point in the history
support RackServlet naming convention for a custom class
  • Loading branch information
kares committed Sep 13, 2012
1 parent 103f83d commit b888f51
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
39 changes: 25 additions & 14 deletions lib/trinidad/web_app.rb
Expand Up @@ -96,7 +96,7 @@ def define_lifecycle
end

# Reset the hold web application state so it gets re-initialized.
# Please note that the received configuration object are not cleared.
# Please note that the configuration objects are not cleared.
def reset!
vars = instance_variables.map(&:to_sym)
vars = vars - [ :'@config', :'@default_config' ]
Expand Down Expand Up @@ -153,29 +153,40 @@ def complete_config!

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.
def web_xml_servlet?(servlet_class, servlet_name = nil)
!!( web_xml_doc && (
web_xml_doc.root.elements["/web-app/servlet[servlet-class = '#{servlet_class}']"]
)
)
return nil unless web_xml_doc
if servlet_class
servlet_xpath = "/web-app/servlet[servlet-class = '#{servlet_class}']"
return true if web_xml_doc.root.elements[servlet_xpath] # else try name
end
if servlet_name
servlet_xpath = "/web-app/servlet[servlet-name = '#{servlet_name}']"
return !! web_xml_doc.root.elements[servlet_xpath]
end

return false if servlet_class || servlet_name
raise ArgumentError, "nor servlet_class nor servlet_name given"
end

# Returns true if a filter definition with a given filter-class is found.
def web_xml_filter?(filter_class)
!!( web_xml_doc && (
web_xml_doc.root.elements["/web-app/filter[filter-class = '#{filter_class}']"]
)
)
return nil unless web_xml_doc
!! web_xml_doc.root.elements["/web-app/filter[filter-class = '#{filter_class}']"]
end

# Returns true if a listener definition with a given listener-class is found.
def web_xml_listener?(listener_class)
!!( web_xml_doc &&
web_xml_doc.root.elements["/web-app/listener[listener-class = '#{listener_class}']"]
)
return nil unless web_xml_doc
!! web_xml_doc.root.elements["/web-app/listener[listener-class = '#{listener_class}']"]
end

# Returns a param-value for a context-param with a given param-name.
def web_xml_context_param(name)
if web_xml_doc &&
param = web_xml_doc.root.elements["/web-app/context-param[param-name = '#{name}']"]
return nil unless web_xml_doc
if param = web_xml_doc.root.elements["/web-app/context-param[param-name = '#{name}']"]
param.elements['param-value'].text
end
end
Expand Down
47 changes: 47 additions & 0 deletions spec/trinidad/web_app_spec.rb
Expand Up @@ -158,6 +158,34 @@
end
end

it "ignores rack_servlet when a deployment descriptor provides a RackServlet named servlet" do
FileUtils.touch custom_web_xml = "extended-web.xml"
begin
FakeFS do
create_config_file custom_web_xml, '' +
'<?xml version="1.0" encoding="UTF-8"?>' +
'<web-app>' +
' <servlet>' +
' <servlet-class>org.kares.jruby.rack.ExtendedServlet</servlet-class>' +
' <servlet-name>RackServlet</servlet-name>' +
' <async-supported>true</async-supported>' +
' </servlet>' +
' <servlet-mapping>' +
' <url-pattern>/*</url-pattern>' +
' <servlet-name>RackServlet</servlet-name>' +
' </servlet-mapping>' +
'</web-app>'

app = Trinidad::WebApp.create({
:web_app_dir => Dir.pwd, :web_xml => custom_web_xml
})
app.rack_servlet.should be nil
end
ensure
FileUtils.rm custom_web_xml
end
end

it "ignores rack_listener when a deployment descriptor already provides it" do
FakeFS do
create_rails_web_xml
Expand Down Expand Up @@ -540,6 +568,18 @@
' <param-name>jruby.rack.logging.name</param-name>' +
' <param-value>/root</param-value>' +
' </context-param>' +
'' +
' <servlet>' +
' <load-on-startup>2</load-on-startup>' +
' <servlet-name>custom-servlet</servlet-name>' +
' <servlet-class>org.kares.jruby.CustomServlet</servlet-class>' +
' <async-supported>true</async-supported>' +
' </servlet>' +
' <servlet-mapping>' +
' <url-pattern>/_custom</url-pattern>' +
' <url-pattern>*.custom</url-pattern>' +
' <servlet-name>custom-servlet</servlet-name>' +
' </servlet-mapping>' +
'</web-app>'
web_app = Trinidad::WebApp.create({}, {
:context_path => '/',
Expand All @@ -555,6 +595,13 @@

web_app.web_xml_listener?('org.jruby.rack.rails').should be false
web_app.web_xml_listener?('org.jruby.rack.rails.RailsServletContextListener').should be true

web_app.web_xml_servlet?('org.jruby.rack.RackServlet').should be false
web_app.web_xml_servlet?(nil, 'RackServlet').should be false
web_app.web_xml_servlet?('org.kares.jruby.CustomServlet').should be true
web_app.web_xml_servlet?(nil, 'custom-servlet').should be true
# NOTE: class not found but if name given assumes there's a "replacement" servlet :
web_app.web_xml_servlet?('org.kares.missing.ServletClass', 'custom-servlet').should be true
ensure
FileUtils.rm custom_web_xml
end
Expand Down

0 comments on commit b888f51

Please sign in to comment.