Class: Rack::Handler::Jetty

Inherits:
Servlet
  • Object
show all
Defined in:
lib/rack/handler/jetty.rb

Constant Summary collapse

DEFAULT_MAX_THREADS =
1000

Class Method Summary collapse

Class Method Details

.filter_holder(app) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rack/handler/jetty.rb', line 68

def self.filter_holder(app)
  context = org.jruby.rack.embed.Context.new("Jetty")
  dispatcher =
    org.jruby.rack.embed.Dispatcher.new(context, self.new(app))

  filter = org.jruby.rack.embed.Filter.new(dispatcher, context)

  org.mortbay.jetty.servlet.FilterHolder.new(filter)
end

.log(msg) ⇒ Object



64
65
66
# File 'lib/rack/handler/jetty.rb', line 64

def self.log(msg)
  @logger && @logger.info(msg)
end

.run(app, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rack/handler/jetty.rb', line 15

def self.run(app, options)

  if options.fetch(:replace_jetty_logger, true)
    # We want to set the logger ASAP to get rid of stderr logging that comes out
    @logger = Rack::Handler::JettyLogAdapter.new(options[:logger], options[:log_prefix] ||
                             'JETTY: ')
    org.mortbay.log.Log.setLog(@logger)
  end

  jetty = org.mortbay.jetty.Server.new options[:Port]

  if options.fetch(:with_jmx, true)
    m_bean_server = java.lang.management.ManagementFactory.getPlatformMBeanServer
    container = org.mortbay.management.MBeanContainer.new(m_bean_server)
    jetty.container.add_event_listener(container)
    container.start
  end

  max_threads = options[:max_threads] || DEFAULT_MAX_THREADS
  thread_pool = org.mortbay.thread.QueuedThreadPool.new(max_threads)
  thread_pool.setName("http")
  log("created thread pool #{thread_pool} with #{max_threads} max threads")
  jetty.setThreadPool(thread_pool)


  context_path = options[:context_path] || "/"
  context = org.mortbay.jetty.servlet.Context.new(nil, context_path,
    org.mortbay.jetty.servlet.Context::NO_SESSIONS)

  servlet_pattern = options[:servlet_pattern] || "/*"

  # The filter acts as the entry point in to the application
  context.add_filter(
    filter_holder(app),
    servlet_pattern,
    org.mortbay.jetty.Handler::ALL)

  # FIXME Umm, this might be wrong
  context.set_resource_base(File.dirname(__FILE__))

  # if we don't have at least one servlet, the filter gets nothing
  context.add_servlet(org.mortbay.jetty.servlet.ServletHolder.new(
    org.mortbay.jetty.servlet.DefaultServlet.new), servlet_pattern)

  jetty.set_handler(context)
  jetty.start
  jetty.join
end