Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeffrey Damick
authored and
Jeffrey Damick
committed
Jun 5, 2008
1 parent
4e68194
commit 1762914
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
module JettyRails | ||
module Handler | ||
class PublicDirectoryHandler < JettyRails::Handler::DelegateOnErrorsHandler | ||
|
||
def initialize(config) | ||
super() | ||
@config = config | ||
@resources = Jetty::Handler::ResourceHandler.new | ||
@resources.resource_base = @config[:base] + '/public' | ||
context_capable = add_context_capability_to @resources | ||
self.handler = context_capable | ||
end | ||
|
||
def add_context_capability_to(handler) | ||
return handler if @config[:context_path].root? | ||
context_handler = Jetty::Handler::ContextHandler.new(@config[:context_path]) | ||
context_handler.handler = handler | ||
context_handler | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module JettyRails | ||
module Handler | ||
class WebAppHandler < Jetty::Handler::WebAppContext | ||
|
||
def initialize(config) | ||
super("/", config[:context_path]) | ||
@config = config | ||
|
||
self.class_loader = JRuby.runtime.jruby_class_loader | ||
self.resource_base = config[:base] | ||
|
||
adapter = adapter_for(config[:adapter]) | ||
self.init_params = adapter.init_params | ||
add_event_listener(adapter.rack_event_listener) | ||
|
||
add_filter(rack_filter, "/*", Jetty::Context::DEFAULT) | ||
end | ||
|
||
def self.add_adapter(adapter_key, adapter) | ||
adapters[adapter_key] = adapter | ||
end | ||
|
||
def self.adapters | ||
@adapters ||= { | ||
:rails => JettyRails::Adapters::RailsAdapter, | ||
:merb => JettyRails::Adapters::MerbAdapter | ||
} | ||
end | ||
|
||
def adapters | ||
self.class.adapters | ||
end | ||
|
||
protected | ||
|
||
def rack_filter | ||
Jetty::FilterHolder.new(Rack::RackFilter.new) | ||
end | ||
|
||
def adapter_for(kind) | ||
adapters[kind.to_sym].new(@config) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module JettyRails | ||
class Server | ||
attr_reader :config | ||
attr_reader :app_contexts | ||
attr_reader :server | ||
|
||
@@defaults = { | ||
:adapter => :rails, | ||
:environment => 'development', | ||
:context_path => '/', | ||
:lib_dir => 'lib/**/*.jar', | ||
:port => 8080, | ||
:jruby_initial_runtimes => 1, | ||
:jruby_max_runtimes => 5, | ||
:thread_pool_max => 20, | ||
:thread_pool_min => 1, | ||
:acceptor_size => 5 | ||
} | ||
|
||
def initialize(config = {}) | ||
@config = config.symbolize_keys!.reverse_merge!(@@defaults) | ||
add_root_method_to @config[:context_path] | ||
|
||
raise 'Basedir to be run must be provided' unless config[:base] | ||
|
||
@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) | ||
|
||
add_lib_dir_jars_to_classpath | ||
@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 | ||
|
||
def add_app(config) | ||
|
||
end | ||
|
||
def start | ||
@server.start | ||
@server.join | ||
end | ||
|
||
|
||
private | ||
def add_lib_dir_jars_to_classpath | ||
lib_dir = "#{config[:base]}/#{config[:lib_dir]}" | ||
Dir[lib_dir].each do |jar| | ||
require jar | ||
end | ||
end | ||
|
||
def add_root_method_to(target) | ||
(class << target; self; end).class_eval do | ||
def root? | ||
self == '/' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
:servers: | ||
- :context_path: /testA | ||
:base: / | ||
:adapter: :rails | ||
:environment: development | ||
:port: 3000 | ||
:lib_dir: lib/**/*.jar | ||
- :context_path: /testB | ||
:base: /testing | ||
:adapter: :merb | ||
:port: 4000 | ||
- :port: 8080 | ||
:apps: | ||
- :context_path: /testC | ||
:base: /testing | ||
:adapter: :merb | ||
- :context_path: /testD | ||
:base: /something | ||
:adapter: :rails | ||
:environment: production | ||
:jruby_initial_runtimes: 1 | ||
:jruby_max_runtimes: 2 | ||
:thread_pool_max: 40 | ||
:thread_pool_min: 1 | ||
:acceptor_size: 20 |