Successfully reported this slideshow.

JRubyによるエンタープライズweb開発

3

Share

Loading in …3
×
1 of 22
1 of 22

More Related Content

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

JRubyによるエンタープライズweb開発

  1. 1. JRuby Webwith JRuby Enterprise Web Application Development 2010 12 4 Akasaka.rb Photo by Michael Pick
  2. 2. Photo by Koichiro Ohba
  3. 3. Rubyist Struts
  4. 4. Front Controller ActionServlet RequestProcessor Action processActionPerform process execute
  5. 5. Front Controller ActionServlet RequestProcessor Action processActionPerform process execute
  6. 6. JRubyRequestController Struts RequestProcessor Ruby class RubyActionRequestProcessor def process_action_form req, res, mapping # do something end def process_action_perform req, res, action, form, mapping # do something end end
  7. 7. JRubyRequestController.java public class JRubyRequestProcessor extends RequestProcessor { ... @Override public void init(ActionServlet servlet, ModuleConfig moduleConfig) throws ServletException { super.init(servlet, moduleConfig); ...
  8. 8. struts-config.xml <struts-config> ... <controller processorClass="n.r.s.JRubyRequestProcessor"> <set-property key="scriptPath" value="/WEB-INF/request-processor.rb" /> </controller> ... </struts-config>
  9. 9. JRubyRequestController#init() container = new ScriptingContainer( LocalContextScope.THREADSAFE ); • • LocalContextScope.THREADSAFE Ruby
  10. 10. JRubyRequestController#init() container.put("SERVLET_CONTEXT", servlet.getServletContext()); • ServletContext SERVLET_CONTEXT
  11. 11. JRubyRequestController#init() String path = moduleConfig.getControllerConfig() .getProperty("scriptPath"); InputStream stream = servlet.getServletContext() .getResourceAsStream(path); EmbedEvalUnit unit = container.parse(stream, path); requestProcessor = unit.run(); • scriptPath requestProcessor
  12. 12. JRubyRequestController#init() String[] names = container.callMethod(requestProcessor, "public_methods", false, String[].class); supportedMethods = new HashSet<String>(Arrays.asList(names)); • requestProcessor public_methods false
  13. 13. JRubyRequestController.java @Override protected ActionForward process...( HttpServletRequest request...) throws ...{ if (supportedMethods.contains("process...")){ return container.callMethod( requestProcessor, "process...", new Object[] { request, ... }, ActionForward.class); } else { return super.process... } ...
  14. 14. requestProcessor JRubyRequsetProcessor :IRubyObject processActionPerform process_action_perform Ruby
  15. 15. JRubyRequestProcessor
  16. 16. SinatraRequestProcessor class SinatraRequestProcessor def initialize @app = SinatraHelloAction.new end def process req, res servlet_env = ServletRackEnvironment.new req env = Rack::Handler::LazyEnv.new servlet_env status, header, body = @app.call env.to_hash res.setStatus(status) header.each do |k, v| res.setHeader(k, v) end out = res.getOutputStream out.print(body.join) end end
  17. 17. Happy Hacking! and Good Carling!

Editor's Notes

  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • ×