Class: JettyRails::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/jetty_rails/server.rb

Defined Under Namespace

Modules: ContextPath

Constant Summary collapse

@@defaults =
{
  :adapter => :rails, 
  :environment => 'development',
  :context_path => '/',
  :lib_dir => 'lib',
  :classes_dir => 'classes',
  :web_xml => 'config/web.xml',
  :port => 8080,
  :jruby_min_runtimes => 1,
  :jruby_max_runtimes => 5,
  :thread_pool_max => 20,
  :thread_pool_min => 1,
  :acceptor_size => 5
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Server

Returns a new instance of Server.



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
# File 'lib/jetty_rails/server.rb', line 22

def initialize(config = {})
  @config = config.symbolize_keys!.reverse_merge!(@@defaults)

  @server = Jetty::Server.new
  # setup the thread pool for the server
  thread_pool = Jetty::Thread::QueuedThreadPool.new
  thread_pool.set_max_threads(config[:thread_pool_max])
  thread_pool.set_min_threads(config[:thread_pool_min])
  @server.set_thread_pool(thread_pool)

  connector = Jetty::SelectChannelConnector.new
  connector.set_acceptors(config[:acceptor_size])
  connector.port = config[:port]
  @server.add_connector(connector)

  if config[:apps].nil?
    add_app(config)
  else
    config[:apps].each do |app_config|
      app_config.reverse_merge!(config)
      app_config.delete(:apps)
      add_app(app_config)
    end
  end
end

Instance Attribute Details

#app_contextsObject (readonly)

Returns the value of attribute app_contexts.



4
5
6
# File 'lib/jetty_rails/server.rb', line 4

def app_contexts
  @app_contexts
end

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/jetty_rails/server.rb', line 3

def config
  @config
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/jetty_rails/server.rb', line 5

def server
  @server
end

Instance Method Details

#add_app(config) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/jetty_rails/server.rb', line 48

def add_app(config) 
  raise 'Base dir to be run must be provided' unless config[:base]
  config[:context_path].extend ContextPath
  
  @server.add_handler(JettyRails::Handler::PublicDirectoryHandler.new(config))
  web_app_handler = JettyRails::Handler::WebAppHandler.new(config)
  (@app_contexts ||= []) << web_app_handler
  @server.add_handler(web_app_handler)
end

#startObject



58
59
60
61
# File 'lib/jetty_rails/server.rb', line 58

def start
  @server.start
  @server.join
end