jetty-rails 0.5 → 0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ require 'getoptlong'
2
+ require 'jetty_rails/config/rdoc_fix'
3
+
4
+
5
+ class CommandLineReader
6
+
7
+ def default_config()
8
+ @@config ||= {
9
+ :rails => {
10
+ :base => Dir.pwd,
11
+ :port => 3000,
12
+ :config_file => "#{File.join(Dir.pwd, 'config', 'jetty_rails.yml')}",
13
+ :adapter => :rails
14
+ },
15
+ :merb => {
16
+ :base => Dir.pwd,
17
+ :port => 4000,
18
+ :config_file => "#{File.join(Dir.pwd, 'jetty_merb.yml')}",
19
+ :adapter => :merb
20
+ }
21
+ }
22
+ end
23
+
24
+ def read(default_adapter = :rails)
25
+ config = default_config[default_adapter]
26
+
27
+ opts = GetoptLong.new(
28
+ [ '--version', '-v', GetoptLong::NO_ARGUMENT ],
29
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
30
+ [ '--context-path', '-u', GetoptLong::REQUIRED_ARGUMENT ],
31
+ [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ],
32
+ [ '--environment', '-e', GetoptLong::REQUIRED_ARGUMENT ],
33
+ [ '--lib', '--jars', GetoptLong::REQUIRED_ARGUMENT ],
34
+ [ '--classes', GetoptLong::REQUIRED_ARGUMENT ],
35
+ [ '--config', '-c', GetoptLong::OPTIONAL_ARGUMENT ]
36
+ )
37
+
38
+ opts.each do |opt, arg|
39
+ case opt
40
+ when '--version'
41
+ require 'jetty_rails/version'
42
+ puts "JettyRails version #{JettyRails::VERSION::STRING} - http://jetty-rails.rubyforge.org"
43
+ exit(0)
44
+ when '--help'
45
+ RDoc::usage
46
+ when '--context-path'
47
+ config[:context_path] = arg
48
+ when '--port'
49
+ config[:port] = arg.to_i
50
+ when '--environment'
51
+ config[:environment] = arg
52
+ when '--classes'
53
+ config[:classes_dir] = arg
54
+ when '--lib'
55
+ config[:lib_dir] = arg
56
+ when '--config'
57
+ config[:config_file] = arg if !arg.nil? && arg != ""
58
+ config.merge!(YAML.load_file(config[:config_file]))
59
+ end
60
+ end
61
+
62
+ config[:base] = ARGV.shift unless ARGV.empty?
63
+ config
64
+ end
65
+
66
+ end
67
+
68
+
@@ -0,0 +1,28 @@
1
+ require 'rdoc/usage'
2
+
3
+ # fix to work with rubygems (use current file instead of main)
4
+ def RDoc.usage_no_exit(*args)
5
+ comment = File.open(File.join(File.dirname(__FILE__), %w(.. .. .. bin), File.basename($0))) do |file|
6
+ find_comment(file)
7
+ end
8
+
9
+ comment = comment.gsub(/^\s*#/, '')
10
+
11
+ markup = SM::SimpleMarkup.new
12
+ flow_convertor = SM::ToFlow.new
13
+
14
+ flow = markup.convert(comment, flow_convertor)
15
+
16
+ format = "plain"
17
+
18
+ unless args.empty?
19
+ flow = extract_sections(flow, args)
20
+ end
21
+
22
+ options = RI::Options.instance
23
+ if args = ENV["RI"]
24
+ options.parse(args.split)
25
+ end
26
+ formatter = options.formatter.new(options, "")
27
+ formatter.display_flow(flow)
28
+ end
@@ -0,0 +1,24 @@
1
+
2
+ module JettyRails
3
+ module Handler
4
+ class PublicDirectoryHandler < JettyRails::Handler::DelegateOnErrorsHandler
5
+
6
+ def initialize(config)
7
+ super()
8
+ @config = config
9
+ @resources = Jetty::Handler::ResourceHandler.new
10
+ @resources.resource_base = @config[:base] + '/public'
11
+ context_capable = add_context_capability_to @resources
12
+ self.handler = context_capable
13
+ end
14
+
15
+ def add_context_capability_to(handler)
16
+ return handler if @config[:context_path].root?
17
+ context_handler = Jetty::Handler::ContextHandler.new(@config[:context_path])
18
+ context_handler.handler = handler
19
+ context_handler
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,66 @@
1
+ module JettyRails
2
+ module Handler
3
+ class WebAppHandler < Jetty::Handler::WebAppContext
4
+ attr_reader :config, :adapter
5
+
6
+ def initialize(config)
7
+ super("/", config[:context_path])
8
+ @config = config
9
+
10
+ # create an isolated classloader per application context
11
+ self.class_loader = org.jruby.util.JRubyClassLoader.new(JRuby.runtime.jruby_class_loader)
12
+ self.resource_base = config[:base]
13
+
14
+ add_classes_dir_to_classpath(config)
15
+ add_lib_dir_jars_to_classpath(config)
16
+
17
+ @adapter = adapter_for(config[:adapter])
18
+ self.init_params = @adapter.init_params
19
+
20
+ @adapter.event_listeners.each do |listener|
21
+ add_event_listener(listener)
22
+ end
23
+
24
+ add_filter(rack_filter, "/*", Jetty::Context::DEFAULT)
25
+ end
26
+
27
+ def self.add_adapter(adapter_key, adapter)
28
+ adapters[adapter_key] = adapter
29
+ end
30
+
31
+ def self.adapters
32
+ @adapters ||= {
33
+ :rails => JettyRails::Adapters::RailsAdapter,
34
+ :merb => JettyRails::Adapters::MerbAdapter
35
+ }
36
+ end
37
+
38
+ def adapters
39
+ self.class.adapters
40
+ end
41
+
42
+ protected
43
+ def rack_filter
44
+ Jetty::FilterHolder.new(Rack::RackFilter.new)
45
+ end
46
+
47
+ def adapter_for(kind)
48
+ adapters[kind.to_sym].new(@config)
49
+ end
50
+
51
+ private
52
+ def add_lib_dir_jars_to_classpath(config)
53
+ lib_dir = "#{config[:base]}/#{config[:lib_dir]}"
54
+ Dir[lib_dir].each do |jar|
55
+ url = java.io.File.new(jar).to_url
56
+ self.class_loader.add_url(url)
57
+ end
58
+ end
59
+ def add_classes_dir_to_classpath(config)
60
+ classes_dir = "#{config[:base]}/#{config[:classes_dir]}"
61
+ url = java.io.File.new(classes_dir).to_url
62
+ self.class_loader.add_url(url)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -12,9 +12,12 @@ module JettyRails
12
12
  include_package "org.mortbay.jetty.handler"
13
13
  include_package "org.mortbay.jetty.webapp"
14
14
  end
15
+ module Thread
16
+ include_package "org.mortbay.thread"
17
+ end
15
18
  end
16
19
 
17
- require "jruby-rack-0.9"
20
+ require "jruby-rack-0.9.2"
18
21
  module Rack
19
22
  include_package "org.jruby.rack"
20
23
  include_package "org.jruby.rack.rails"
@@ -3,97 +3,44 @@ require "jruby"
3
3
  module JettyRails
4
4
 
5
5
  class Runner
6
- attr_reader :config
7
- attr_reader :server
8
- attr_reader :app_context
9
-
10
- @@defaults = {
11
- :adapter => :rails,
12
- :environment => 'development',
13
- :context_path => '/',
14
- :lib_dir => 'lib/**/*.jar',
15
- :port => 8080
16
- }
17
-
18
- @@adapters = {
19
- :rails => JettyRails::Adapters::RailsAdapter,
20
- :merb => JettyRails::Adapters::MerbAdapter
21
- }
6
+ attr_reader :servers
22
7
 
23
8
  def initialize(config = {})
24
- @config = config.symbolize_keys!.reverse_merge!(@@defaults)
25
- @config[:context_path].extend ContextPath
26
-
27
- raise 'Basedir to be run must be provided' unless config[:base]
28
-
29
- @server = Jetty::Server.new
30
- connector = Jetty::SelectChannelConnector.new
31
- connector.port = config[:port]
32
- @server.add_connector(connector)
33
-
34
- add_stuff_to_classpath
35
- add_public_dir_to server
36
- install_rack_on server
37
- end
38
-
39
- def start
40
- @server.start
41
- @server.join
42
- end
43
-
44
- private
45
- def add_stuff_to_classpath
46
- lib_dir = "#{config[:base]}/#{config[:lib_dir]}"
47
- Dir[lib_dir].each do |jar|
48
- require jar
9
+ @servers = {}
10
+ config.symbolize_keys!
11
+ if config[:servers].nil?
12
+ add_server(config)
13
+ else
14
+ config[:servers].each do |server_config|
15
+ server_config.symbolize_keys!
16
+ server_config.reverse_merge!(config)
17
+ server_config.delete(:servers)
18
+ add_server(server_config)
19
+ end
49
20
  end
50
21
  end
51
22
 
52
- def add_public_dir_to(server)
53
- @resources = Jetty::Handler::ResourceHandler.new
54
- @resources.resource_base = config[:base] + '/public'
55
- context_capable = add_context_capability_to @resources
56
- delegate_on_error = JettyRails::Handler::DelegateOnErrorsHandler.new
57
- delegate_on_error.handler = context_capable
58
- server.add_handler(delegate_on_error)
23
+ def add_server(config = {})
24
+ server = JettyRails::Server.new(config)
25
+ @servers[server.config[:port]] = server
59
26
  end
60
27
 
61
- def add_context_capability_to(handler)
62
- return handler if config[:context_path].root?
63
- context_handler = Jetty::Handler::ContextHandler.new(config[:context_path])
64
- context_handler.handler = @resources
65
- context_handler
66
- end
67
-
68
- def install_rack_on(server)
69
- @app_context = Jetty::Handler::WebAppContext.new("/", config[:context_path])
70
- @app_context.class_loader = JRuby.runtime.jruby_class_loader
71
- @app_context.resource_base = config[:base]
72
-
73
- adapter = adapter_for config[:adapter]
74
- @app_context.init_params = adapter.init_params
75
- adapter.event_listeners.each do |listener|
76
- @app_context.add_event_listener(listener)
28
+ def start
29
+ server_threads = ThreadGroup.new
30
+ @servers.each do |base, server|
31
+ log("starting #{base}")
32
+ server_threads.add(Thread.new do
33
+ server.start
34
+ end)
77
35
  end
78
36
 
79
- @app_context.add_filter(rack_filter, "/*", Jetty::Context::DEFAULT)
80
- server.add_handler(@app_context)
81
- end
82
-
83
- def rack_filter
84
- Jetty::FilterHolder.new(Rack::RackFilter.new)
37
+ server_threads.list.each {|thread| thread.join } unless server_threads.list.empty?
85
38
  end
86
39
 
87
- def adapter_for(kind)
88
- @@adapters[kind.to_sym].new(config)
89
- end
40
+ private
90
41
 
91
- end
92
-
93
- module ContextPath
94
- def root?
95
- self == '/'
42
+ def log(msg)
43
+ $stdout.puts(msg)
96
44
  end
97
45
  end
98
-
99
46
  end
@@ -0,0 +1,75 @@
1
+ module JettyRails
2
+ class Server
3
+ attr_reader :config
4
+ attr_reader :app_contexts
5
+ attr_reader :server
6
+
7
+ @@defaults = {
8
+ :adapter => :rails,
9
+ :environment => 'development',
10
+ :context_path => '/',
11
+ :lib_dir => 'lib/**/*.jar',
12
+ :classes_dir => 'classes',
13
+ :port => 8080,
14
+ :jruby_min_runtimes => 1,
15
+ :jruby_max_runtimes => 5,
16
+ :thread_pool_max => 20,
17
+ :thread_pool_min => 1,
18
+ :acceptor_size => 5
19
+ }
20
+
21
+ def initialize(config = {})
22
+ @config = config.symbolize_keys!.reverse_merge!(@@defaults)
23
+
24
+ @server = Jetty::Server.new
25
+ # setup the thread pool for the server
26
+ thread_pool = Jetty::Thread::QueuedThreadPool.new
27
+ thread_pool.set_max_threads(config[:thread_pool_max])
28
+ thread_pool.set_min_threads(config[:thread_pool_min])
29
+ @server.set_thread_pool(thread_pool)
30
+
31
+ connector = Jetty::SelectChannelConnector.new
32
+ connector.set_acceptors(config[:acceptor_size])
33
+ connector.port = config[:port]
34
+ @server.add_connector(connector)
35
+
36
+ if config[:apps].nil?
37
+ add_app(config)
38
+ else
39
+ config[:apps].each do |app_config|
40
+ app_config.reverse_merge!(config)
41
+ app_config.delete(:apps)
42
+ add_app(app_config)
43
+ end
44
+ end
45
+ end
46
+
47
+ def add_app(config)
48
+ raise 'Base dir to be run must be provided' unless config[:base]
49
+ config[:context_path].extend ContextPath
50
+
51
+ @server.add_handler(JettyRails::Handler::PublicDirectoryHandler.new(config))
52
+ web_app_handler = JettyRails::Handler::WebAppHandler.new(config)
53
+ (@app_contexts ||= []) << web_app_handler
54
+ @server.add_handler(web_app_handler)
55
+ end
56
+
57
+ def start
58
+ @server.start
59
+ @server.join
60
+ end
61
+
62
+
63
+ private
64
+ def read_warble_config
65
+ require 'warbler'
66
+ WarblerReader.new(config)
67
+ end
68
+
69
+ module ContextPath
70
+ def root?
71
+ self == '/'
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,7 +1,7 @@
1
1
  module JettyRails #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 5
4
+ MINOR = 6
5
5
 
6
6
  STRING = [MAJOR, MINOR].join('.')
7
7
  end
@@ -0,0 +1,13 @@
1
+ class WarblerReader
2
+
3
+ def initialize(config)
4
+ # TODO ignore jruby and jruby-rack
5
+ warbler_config = load("#{config[:base]}/config/warble.rb")
6
+ warbler_config.java_libs.each do |jar|
7
+ require jar
8
+ end
9
+ # TODO require custom classes
10
+ end
11
+
12
+
13
+ end
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,28 @@
1
+ ---
2
+ :servers:
3
+ - :context_path: /testA
4
+ :base: /
5
+ :adapter: :rails
6
+ :environment: development
7
+ :port: 3000
8
+ :lib_dir: lib/**/*.jar
9
+ :jruby_min_runtimes: 2
10
+ - :context_path: /testB
11
+ :base: /testing
12
+ :adapter: :merb
13
+ :port: 4000
14
+ - :port: 8080
15
+ :apps:
16
+ - :context_path: /testC
17
+ :base: /testing
18
+ :adapter: :merb
19
+ :environment: test
20
+ - :context_path: /testD
21
+ :base: /something
22
+ :adapter: :rails
23
+ :environment: production
24
+ :jruby_min_runtimes: 1
25
+ :jruby_max_runtimes: 2
26
+ :thread_pool_max: 40
27
+ :thread_pool_min: 1
28
+ :acceptor_size: 20