1

我有一个嵌入 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);
  }
}
4

1 回答 1

3

这是JavaEmbedUtils按照您的示例使用的一种方法。

您可以首先创建一个用 JRuby(在 此处src/main/ruby/ruby_servlet.rb)编写的 servlet,例如:

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

然后你可以使用 yourJRubyServlet来调用这个 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 提供了控制并发模型等的方法。

于 2012-09-10T11:21:17.077 回答