java - 在嵌入式 Tomcat 中运行 Ruby 脚本

标签 java ruby tomcat jruby

我有一个嵌入了 Tomcat 的 Java Swing 应用程序。嵌入式 Tomcat 以编程方式配置;没有 xml。 Tomcat 也使用 Groovy Servlets 配置:

StandardWrapper gspWrapper = new StandardWrapper();
gspWrapper.setName("groovy");
gspWrapper.setServletName("groovy");
gspWrapper.setServletClass(GroovyServlet.class.getName());
gspWrapper.addInitParameter("fork", "false");
gspWrapper.setLoadOnStartup(2);

我也想要 Ruby 支持 (jRuby 1.7)。所以我想以与 Groovy 支持相同的方式配置它:

StandardWrapper rubyWrapper = new StandardWrapper();
rubyWrapper.setName("rb");
rubyWrapper.setServletName("rb");
rubyWrapper.setServletClass(JRubyServlet.class.getName());
rubyWrapper.addInitParameter("fork", "false");
rubyWrapper.setLoadOnStartup(2);

我尝试编写 JRubyServlet 类,但我不知道如何执行脚本并将输出写入响应。这是我当前的代码:

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jruby.Ruby;
import org.jruby.javasupport.JavaEmbedUtils;

public class JRubyServlet extends HttpServlet {

    private static final long serialVersionUID = -6913887886084787803L;
    private Ruby ruby;

  @Override public void init(ServletConfig config) throws ServletException {
      super.init(config);
      ruby = JavaEmbedUtils.initialize(new ArrayList<String> ());
  }

  @Override public void destroy() {
      JavaEmbedUtils.terminate(ruby);
      super.destroy();
  }

  @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      //How do you execute the script here?
  }

  @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      doGet(req, res);
  }
}

最佳答案

根据您的示例,这是使用 JavaEmbedUtils 执行此操作的方法。

您可以先创建一个用 JRuby 编写的 servlet(这里是 src/main/ruby/ruby_servlet.rb),例如:

require 'java'

java_import 'javax.servlet.http.HttpServlet'

class RubyServlet < HttpServlet
  def init(config)
    puts "Configuring RubyServlet..."
  end

  def service(request, response)
    response.content_type = "text/html"
    out = response.writer

    out.println("Hi from JRuby.")
  end
end

然后您可以使用您的 JRubyServlet 调用此 JRuby 脚本:

public class JRubyServlet extends HttpServlet {

    private static final long serialVersionUID = -6913887886084787803L;
    private Ruby ruby;
    private Servlet rubyServlet;

  @Override public void init(ServletConfig config) throws ServletException {
      super.init(config);
      // Add location of the script to load path
      ruby = JavaEmbedUtils.initialize(asList("src/main/ruby"));

      // Instantiate ruby servlet, and store it into instance variable rubyServlet
      rubyServlet = (Servlet)JavaEmbedUtils.rubyToJava(ruby.evalScriptlet("require 'ruby_servlet'; RubyServlet.new"));
      rubyServlet.init(config);
  }

  @Override public void destroy() {
      JavaEmbedUtils.terminate(ruby);
      super.destroy();
  }

  @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

      // Call JRuby servlet's service method!
      rubyServlet.service(req, res);
  }

  @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      doGet(req, res);
  }

另请参阅 https://github.com/jruby/jruby/wiki/RedBridge ;使用 JRuby Embed 提供了控制并发模型等的方法。

关于java - 在嵌入式 Tomcat 中运行 Ruby 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12348114/

相关文章:

java - 在 JavaFX 中对文本文件进行排序

gwt - 拦截客户端证书并将其转发到 web 服务

ruby-on-rails - Rails 3 上的 404 err 部署到 AWS...很好,除了 EB 不复制/ondeck 到/current

ruby-on-rails - Ruby:为什么实例评估类创建实例方法而不是类方法

javascript - 如何在Rails中重写简单的.js页面浏览计数器cookie函数?

mysql - AngularJs Java 和 Jersey 的 404 NullPointerException

java - 有什么方法可以在tomcat中的不同应用程序之间共享 session 状态?

java - 编写网络类的最佳方法

java - 递归 - 它做了什么

java - JSF2 : Using EL in navigation rules. 超时重定向?