javaruby-on-railsservletsjrubyjruby-rack

How to configure custom servlet in Jruby-rack?


According to Jruby-Rack Github, we can configure to use org.jruby.rack.RackServlet instead of context listener.

Can we use another custom servlet rather than RackServlet? Or can we override org.jruby.rack.RackServlet?


Solution

  • The listener stays - you usually use a servlet instead of a filter with the Servlet API e.g.

    <servlet>
      <servlet-name>rack</servlet-name>
      <servlet-class>org.jruby.rack.RackFilter</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>rack</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
    <!-- using the above servlet instead of : -->
    <!--
    <filter>
      <filter-name>RackFilter</filter-name>
      <filter-class>org.jruby.rack.RackFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>RackFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    -->
    
    <listener>
      <listener-class>org.jruby.rack.RackServletContextListener</listener-class>
    </listener>
    

    You can override the RackFilter as well as the RackServlet you can even use a custom class as long as it plays by JRuby-Rack's rules. With the latest Trinidad (uses JRuby-Rack under the hood) it's a bit easier to configure an alternate servlet without writing a web.xml e.g. in config/trinidad.yml you might :

    ---
      address: localhost
      port: 3000  # port where Trinidad is running
      rack_servlet:
        class: org.blue-smith.rack.CoolRackServlet
    

    If you do make sure your CoolRackServlet class is on the class-path e.g. package it in a jar and put the .jar under *[APP_ROOT]/lib/java* or for quick testing simply copy the compiled .class file(s) including the package directories under lib/java/classes ...